Skip to content

Commit

Permalink
improve accuracy of constructor call
Browse files Browse the repository at this point in the history
Signed-off-by: Pranav Gaikwad <pgaikwad@redhat.com>
  • Loading branch information
pranavgaikwad committed Jun 26, 2024
1 parent 25dce3b commit 63380d9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.search.MethodReferenceMatch;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.internal.core.JavaElement;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.SymbolKind;

public class ConstructorCallSymbolProvider implements SymbolProvider {
public class ConstructorCallSymbolProvider implements SymbolProvider, WithQuery {
public String query;

@Override
public List<SymbolInformation> get(SearchMatch match) throws CoreException {
List<SymbolInformation> symbols = new ArrayList<>();
Expand All @@ -32,11 +42,50 @@ public List<SymbolInformation> get(SearchMatch match) throws CoreException {
}
symbol.setContainerName(mod.getParent().getElementName());
symbol.setLocation(getLocation(mod, match));
symbols.add(symbol);
if (this.query.contains(".")) {
ICompilationUnit unit = mod.getCompilationUnit();
ASTParser astParser = ASTParser.newParser(AST.getJLSLatest());
astParser.setSource(unit);
astParser.setResolveBindings(true);
CompilationUnit cu = (CompilationUnit) astParser.createAST(null);
cu.accept(new ASTVisitor() {
// we are only doing this for MethodInvocation right now
// look into MethodDeclaration if needed
public boolean visit(MethodInvocation node) {
try {
IMethodBinding binding = node.resolveMethodBinding();
if (binding != null) {
// get fqn of the method being called
ITypeBinding declaringClass = binding.getDeclaringClass();
if (declaringClass != null) {
String fullyQualifiedName = declaringClass.getQualifiedName() + "." + binding.getName();
// match fqn with query pattern
if (fullyQualifiedName.matches(getCleanedQuery(query))) {
symbols.add(symbol);
} else {
logInfo("fqn " + fullyQualifiedName + " did not match with " + query);
}
}
}
} catch (Exception e) {
logInfo("error determining accuracy of match: " + e);
}
return true;
}
});
} else {
symbols.add(symbol);
}
} catch (Exception e) {
logInfo("unable to get constructor: " + e);
return null;
}
return symbols;
}

@Override
public void setQuery(String query) {
// TODO Auto-generated method stub
this.query = query;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public List<SymbolInformation> get(SearchMatch match) {
try {
MethodReferenceMatch m = (MethodReferenceMatch) match;
IMethod e = (IMethod) m.getElement();
SymbolInformation symbol = new SymbolInformation();
symbol.setName(e.getElementName());
symbol.setKind(convertSymbolKind(e));
symbol.setContainerName(e.getParent().getElementName());
symbol.setLocation(getLocation(e, match));
if (this.query.contains(".")) {
ICompilationUnit unit = e.getCompilationUnit();
ASTParser astParser = ASTParser.newParser(AST.getJLSLatest());
Expand All @@ -52,11 +57,6 @@ public boolean visit(MethodInvocation node) {
String fullyQualifiedName = declaringClass.getQualifiedName() + "." + binding.getName();
// match fqn with query pattern
if (fullyQualifiedName.matches(getCleanedQuery(query))) {
SymbolInformation symbol = new SymbolInformation();
symbol.setName(e.getElementName());
symbol.setKind(convertSymbolKind(e));
symbol.setContainerName(e.getParent().getElementName());
symbol.setLocation(getLocation(e, match));
symbols.add(symbol);
} else {
logInfo("fqn " + fullyQualifiedName + " did not match with " + query);
Expand All @@ -70,11 +70,6 @@ public boolean visit(MethodInvocation node) {
}
});
} else {
SymbolInformation symbol = new SymbolInformation();
symbol.setName(e.getElementName());
symbol.setKind(convertSymbolKind(e));
symbol.setContainerName(e.getParent().getElementName());
symbol.setLocation(getLocation(e, match));
symbols.add(symbol);
}
} catch (Exception e) {
Expand Down

0 comments on commit 63380d9

Please sign in to comment.