Skip to content

Commit 392c08e

Browse files
eemcmullanDescatlesWenhao Zhang
authored
Improve false positive in METHOD_CALL and CONSTRUCTOR_CALL (#133) (#139)
* Fix several issues in method call * resolve comments * for sign off --------- Signed-off-by: Wenhao Zhang <wenhaozhang@microsoft.com> Signed-off-by: Emily McMullan <emcmulla@redhat.com> Co-authored-by: Wenhao Zhang <sjtu_zhangwh@163.com> Co-authored-by: Wenhao Zhang <wenhaozhang@microsoft.com>
1 parent 4a195ff commit 392c08e

3 files changed

Lines changed: 45 additions & 6 deletions

File tree

java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/symbol/ConstructorCallSymbolProvider.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.eclipse.jdt.core.ICompilationUnit;
1111
import org.eclipse.jdt.core.IJavaElement;
1212
import org.eclipse.jdt.core.IMethod;
13+
import org.eclipse.jdt.core.compiler.IProblem;
1314
import org.eclipse.jdt.core.dom.AST;
1415
import org.eclipse.jdt.core.dom.ASTParser;
1516
import org.eclipse.jdt.core.dom.CompilationUnit;
@@ -58,6 +59,20 @@ public List<SymbolInformation> get(SearchMatch match) throws CoreException {
5859
astParser.setResolveBindings(true);
5960
CompilationUnit cu = (CompilationUnit) astParser.createAST(null);
6061
CustomASTVisitor visitor = new CustomASTVisitor(query, match, QueryLocation.CONSTRUCTOR_CALL);
62+
// Under tests, resolveConstructorBinding will return null if there are problems
63+
IProblem[] problems = cu.getProblems();
64+
if (problems != null && problems.length > 0) {
65+
logInfo("KONVEYOR_LOG: " + "Found " + problems.length + " problems while compiling");
66+
int count = 0;
67+
for (IProblem problem : problems) {
68+
logInfo("KONVEYOR_LOG: Problem - ID: " + problem.getID() + " Message: " + problem.getMessage());
69+
count++;
70+
if (count >= SymbolProvider.MAX_PROBLEMS_TO_LOG) {
71+
logInfo("KONVEYOR_LOG: Only showing first " + SymbolProvider.MAX_PROBLEMS_TO_LOG + " problems, " + (problems.length - SymbolProvider.MAX_PROBLEMS_TO_LOG) + " more not displayed");
72+
break;
73+
}
74+
}
75+
}
6176
cu.accept(visitor);
6277
if (visitor.symbolMatches()) {
6378
symbols.add(symbol);

java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/symbol/MethodCallSymbolProvider.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.eclipse.jdt.core.IJavaElement;
1111
import org.eclipse.jdt.core.IMethod;
1212
import org.eclipse.jdt.core.ITypeRoot;
13+
import org.eclipse.jdt.core.compiler.IProblem;
1314
import org.eclipse.jdt.core.dom.AST;
1415
import org.eclipse.jdt.core.dom.ASTParser;
1516
import org.eclipse.jdt.core.dom.CompilationUnit;
@@ -52,6 +53,21 @@ public List<SymbolInformation> get(SearchMatch match) {
5253
astParser.setResolveBindings(true);
5354
CompilationUnit cu = (CompilationUnit) astParser.createAST(null);
5455
CustomASTVisitor visitor = new CustomASTVisitor(query, match, QueryLocation.METHOD_CALL);
56+
// Under tests, resolveConstructorBinding will return null if there are problems
57+
IProblem[] problems = cu.getProblems();
58+
if (problems != null && problems.length > 0) {
59+
logInfo("KONVEYOR_LOG: " + "Found " + problems.length + " problems while compiling");
60+
int count = 0;
61+
for (IProblem problem : problems) {
62+
logInfo("KONVEYOR_LOG: Problem - ID: " + problem.getID() + " Message: " + problem.getMessage());
63+
count++;
64+
if (count >= SymbolProvider.MAX_PROBLEMS_TO_LOG) {
65+
logInfo("KONVEYOR_LOG: Only showing first " + SymbolProvider.MAX_PROBLEMS_TO_LOG + " problems, " +
66+
(problems.length - SymbolProvider.MAX_PROBLEMS_TO_LOG) + " more not displayed");
67+
break;
68+
}
69+
}
70+
}
5571
cu.accept(visitor);
5672
if (visitor.symbolMatches()) {
5773
symbols.add(symbol);

java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/symbol/SymbolProvider.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.eclipse.lsp4j.SymbolInformation;
3131
import org.eclipse.lsp4j.SymbolKind;
3232
public interface SymbolProvider {
33+
public static final int MAX_PROBLEMS_TO_LOG = 10;
34+
3335
List<SymbolInformation> get(SearchMatch match) throws CoreException;
3436

3537
default SymbolKind convertSymbolKind(IJavaElement element) {
@@ -193,6 +195,10 @@ default boolean queryQualificationMatches(String query, ICompilationUnit unit, L
193195
} catch(Exception e) {
194196
logInfo("unable to make unit consistant, will still try as could be class file in a jar" + e);
195197
}
198+
// should consider parameter here
199+
// e.g. java.nio.file.Paths.get(String)/java.nio.file.Paths.get(*) -> java.nio.file.Paths.get
200+
// Remove any parentheses and their contents
201+
query = query.replaceAll("\\(.*\\)", "");
196202
query = query.replaceAll("(?<!\\.)\\*", ".*");
197203
String queryQualification = "";
198204
int dotIndex = query.lastIndexOf('.');
@@ -221,9 +227,9 @@ default boolean queryQualificationMatches(String query, ICompilationUnit unit, L
221227
for (IImportDeclaration importDecl : unit.getImports()) {
222228
String importElement = importDecl.getElementName();
223229
String importQualification = "";
224-
int importDotIndex = query.lastIndexOf('.');
230+
int importDotIndex = importElement.lastIndexOf('.');
225231
if (importDotIndex > 0) {
226-
importQualification = query.substring(0, dotIndex);
232+
importQualification = importElement.substring(0, importDotIndex);
227233
}
228234
// import can be absolute like java.io.paths.FileReader
229235
if (query.matches(importElement)) {
@@ -236,16 +242,18 @@ default boolean queryQualificationMatches(String query, ICompilationUnit unit, L
236242
if (importElement.contains("*")) {
237243
if (queryQualification != "") {
238244
// query is java.io.paths.File*, import is java.io.paths.*
239-
if (queryQualification.startsWith(importQualification)) {
245+
if (queryQualification.equals(importQualification) ||
246+
queryQualification.startsWith(importQualification + ".")) {
240247
return true;
241248
}
242-
if (importElement.replaceAll(".*", "").matches(queryQualification)) {
249+
String importWithoutWildcard = importElement.replace(".*", "");
250+
if (importWithoutWildcard.equals(queryQualification)) {
243251
return true;
244252
}
245253
}
246254
}
247-
// query can be java.io.paths.Path.checkPath()
248-
if (query.startsWith(importElement)) {
255+
//import can be java.nio.file.Path, query can be java.nio.file.Paths.get*
256+
if (query.equals(importElement) || query.startsWith(importElement + ".")) {
249257
return true;
250258
}
251259
}

0 commit comments

Comments
 (0)