Skip to content

Commit 23857f4

Browse files
shawn-hurleyjmle
andauthored
🐛 Fixing annotation searching by getting the FQDN from the compliation. (#163)
* Fixing annotation searching by getting the FQDN from the compliation. * Falls back to trying to use the code actions to get the FQDN Signed-off-by: Shawn Hurley <[email protected]> * Couple of fixes Signed-off-by: Juan Manuel Leflet Estrada <[email protected]> * Respect annotated feature Signed-off-by: Juan Manuel Leflet Estrada <[email protected]> --------- Signed-off-by: Shawn Hurley <[email protected]> Signed-off-by: Juan Manuel Leflet Estrada <[email protected]> Co-authored-by: Juan Manuel Leflet Estrada <[email protected]>
1 parent 4ca22e2 commit 23857f4

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

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

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
import static org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin.logInfo;
44

55
import java.util.ArrayList;
6+
import java.util.Arrays;
67
import java.util.List;
78

8-
import io.konveyor.tackle.core.internal.query.AnnotationQuery;
9-
import io.konveyor.tackle.core.internal.symbol.CustomASTVisitor.QueryLocation;
10-
119
import org.eclipse.core.runtime.CoreException;
1210
import org.eclipse.jdt.core.IAnnotatable;
1311
import org.eclipse.jdt.core.IAnnotation;
1412
import org.eclipse.jdt.core.IClassFile;
1513
import org.eclipse.jdt.core.ICompilationUnit;
1614
import org.eclipse.jdt.core.IJavaElement;
15+
import org.eclipse.jdt.core.IType;
1716
import org.eclipse.jdt.core.compiler.IProblem;
1817
import org.eclipse.jdt.core.dom.AST;
1918
import org.eclipse.jdt.core.dom.ASTParser;
@@ -26,11 +25,21 @@
2625
import org.eclipse.lsp4j.Location;
2726
import org.eclipse.lsp4j.SymbolInformation;
2827

28+
import io.konveyor.tackle.core.internal.query.AnnotationQuery;
29+
import io.konveyor.tackle.core.internal.symbol.CustomASTVisitor.QueryLocation;
30+
2931
public class AnnotationSymbolProvider implements SymbolProvider, WithQuery, WithAnnotationQuery {
3032

3133
private AnnotationQuery annotationQuery;
3234
private String query;
3335

36+
private static final List<Class<? extends SourceRefElement>> ACCEPTED_CLASSES = new ArrayList<>();
37+
static {
38+
ACCEPTED_CLASSES.add(ResolvedSourceMethod.class);
39+
ACCEPTED_CLASSES.add(ResolvedSourceField.class);
40+
ACCEPTED_CLASSES.add(ResolvedSourceType.class);
41+
}
42+
3443
@Override
3544
public List<SymbolInformation> get(SearchMatch match) throws CoreException {
3645
List<SymbolInformation> symbols = new ArrayList<>();
@@ -54,6 +63,39 @@ public List<SymbolInformation> get(SearchMatch match) throws CoreException {
5463
unit = cls.getWorkingCopy(new WorkingCopyOwnerImpl(), null);
5564
}
5665
}
66+
if (unit != null) {
67+
IType t = unit.getType(annotationElement.getElementName());
68+
String fqdn = "";
69+
if (!t.isResolved()) {
70+
var elements = unit.codeSelect(match.getOffset(), match.getLength());
71+
for (IJavaElement e: Arrays.asList(elements)) {
72+
if (e instanceof IType) {
73+
var newT = (IType) e;
74+
if (newT.isResolved()) {
75+
fqdn = newT.getFullyQualifiedName('.');
76+
logInfo("FQDN from code select: " + fqdn);
77+
}
78+
}
79+
}
80+
} else {
81+
fqdn = t.getFullyQualifiedName('.');
82+
logInfo("resolved type: " + fqdn);
83+
}
84+
if (query.matches(fqdn) || fqdn.matches(query)) {
85+
if (unit.isWorkingCopy()) {
86+
unit.discardWorkingCopy();
87+
unit.close();
88+
}
89+
90+
if (matchesAnnotationQuery(match, ACCEPTED_CLASSES)) {
91+
symbols.add(symbol);
92+
}
93+
return symbols;
94+
}
95+
}
96+
97+
logInfo("falling back to resolving via AST");
98+
5799
if (this.queryQualificationMatches(this.query.replaceAll("\\(([A-Za-z_][A-Za-z0-9_]*(\\|[A-Za-z_][A-Za-z0-9_]*)*)\\)", ".*"), annotationElement, unit, location)) {
58100
ASTParser astParser = ASTParser.newParser(AST.getJLSLatest());
59101
astParser.setSource(unit);
@@ -77,11 +119,7 @@ public List<SymbolInformation> get(SearchMatch match) throws CoreException {
77119
cu.accept(visitor);
78120
if (visitor.symbolMatches()) {
79121
if (annotationQuery != null) {
80-
List<Class<? extends SourceRefElement>> classes = new ArrayList<>();
81-
classes.add(ResolvedSourceMethod.class);
82-
classes.add(ResolvedSourceField.class);
83-
classes.add(ResolvedSourceType.class);
84-
if (matchesAnnotationQuery(match, classes)) {
122+
if (matchesAnnotationQuery(match, ACCEPTED_CLASSES)) {
85123
symbols.add(symbol);
86124
}
87125
} else {
@@ -95,18 +133,13 @@ public List<SymbolInformation> get(SearchMatch match) throws CoreException {
95133
}
96134
} else {
97135
if (annotationQuery != null) {
98-
List<Class<? extends SourceRefElement>> classes = new ArrayList<>();
99-
classes.add(ResolvedSourceMethod.class);
100-
classes.add(ResolvedSourceField.class);
101-
classes.add(ResolvedSourceType.class);
102-
if (matchesAnnotationQuery(match, classes)) {
136+
if (matchesAnnotationQuery(match, ACCEPTED_CLASSES)) {
103137
symbols.add(symbol);
104138
}
105139
} else {
106140
symbols.add(symbol);
107141
}
108142
}
109-
110143
}
111144
return symbols;
112145
} catch (Exception e) {

0 commit comments

Comments
 (0)