diff --git a/java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/RuleEntryParams.java b/java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/RuleEntryParams.java index 790cccd..790cdd6 100644 --- a/java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/RuleEntryParams.java +++ b/java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/RuleEntryParams.java @@ -25,8 +25,8 @@ public RuleEntryParams(final String commandId, final List arguments) { this.projectName = (String) obj.get("project"); this.query = (String) obj.get("query"); - this.annotationQuery = AnnotationQuery.fromMap((Map) obj.get("annotationQuery")); this.location = Integer.parseInt((String) obj.get("location")); + this.annotationQuery = AnnotationQuery.fromMap((Map) obj.get("annotationQuery"), location); this.analysisMode = (String) obj.get("analysisMode"); this.includedPaths = (ArrayList) obj.get("includedPaths"); } diff --git a/java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/query/AnnotationQuery.java b/java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/query/AnnotationQuery.java index c9e9b62..c6318ee 100644 --- a/java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/query/AnnotationQuery.java +++ b/java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/query/AnnotationQuery.java @@ -3,6 +3,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.regex.Pattern; /** * Represents additional query information to inspect annotations in annotated symbols. @@ -14,14 +15,20 @@ public class AnnotationQuery { */ private String type; + /** + * Indicates whether this AnnotationQuery is done on an annotation (location == ANNOTATION) + */ + private boolean isOnAnnotation; + /** * The elements within the annotation, ie, "value" in @BeanAnnotation(value = "value") */ private Map elements; - public AnnotationQuery(String type, Map elements) { + public AnnotationQuery(String type, Map elements, boolean isOnAnnotation) { this.type = type; this.elements = elements; + this.isOnAnnotation = isOnAnnotation; } public String getType() { @@ -32,7 +39,23 @@ public Map getElements() { return elements; } - public static AnnotationQuery fromMap(Map query) { + public boolean isOnAnnotation() { + return this.isOnAnnotation; + } + + /** + * Checks whether the query matches against a given annotation + */ + public boolean matchesAnnotation(String annotation) { + // If the annotation query is happening on an annotation, the annotation field in the annotation query can be null + if (isOnAnnotation() && getType() == null) { + return true; + } else { + return Pattern.matches(getType(), annotation); + } + } + + public static AnnotationQuery fromMap(Map query, int location) { if (query == null) { return null; } @@ -46,6 +69,6 @@ public static AnnotationQuery fromMap(Map query) { elements.put(key, value); } - return new AnnotationQuery(typePattern, elements); + return new AnnotationQuery(typePattern, elements, location == 4); } } diff --git a/java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/symbol/AnnotationSymbolProvider.java b/java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/symbol/AnnotationSymbolProvider.java index 6e3eef8..4101aa6 100644 --- a/java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/symbol/AnnotationSymbolProvider.java +++ b/java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/symbol/AnnotationSymbolProvider.java @@ -5,14 +5,22 @@ import java.util.ArrayList; import java.util.List; +import io.konveyor.tackle.core.internal.query.AnnotationQuery; import org.eclipse.core.runtime.CoreException; import org.eclipse.jdt.core.IAnnotatable; import org.eclipse.jdt.core.IAnnotation; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.search.SearchMatch; +import org.eclipse.jdt.internal.core.ResolvedSourceField; +import org.eclipse.jdt.internal.core.ResolvedSourceMethod; +import org.eclipse.jdt.internal.core.ResolvedSourceType; +import org.eclipse.jdt.internal.core.SourceRefElement; import org.eclipse.lsp4j.SymbolInformation; -public class AnnotationSymbolProvider implements SymbolProvider { +public class AnnotationSymbolProvider implements SymbolProvider, WithAnnotationQuery { + + private AnnotationQuery annotationQuery; + @Override public List get(SearchMatch match) throws CoreException { List symbols = new ArrayList<>(); @@ -25,7 +33,18 @@ public List get(SearchMatch match) throws CoreException { symbol.setKind(convertSymbolKind(element)); symbol.setContainerName(annotation.getParent().getElementName()); symbol.setLocation(getLocation(element, match)); - symbols.add(symbol); + + if (annotationQuery != null) { + List> classes = new ArrayList<>(); + classes.add(ResolvedSourceMethod.class); + classes.add(ResolvedSourceField.class); + classes.add(ResolvedSourceType.class); + if (matchesAnnotationQuery(match, classes)) { + symbols.add(symbol); + } + } else { + symbols.add(symbol); + } } return symbols; } catch (Exception e) { @@ -33,4 +52,12 @@ public List get(SearchMatch match) throws CoreException { return null; } } + + public AnnotationQuery getAnnotationQuery() { + return annotationQuery; + } + + public void setAnnotationQuery(AnnotationQuery annotationQuery) { + this.annotationQuery = annotationQuery; + } } diff --git a/java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/symbol/WithAnnotationQuery.java b/java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/symbol/WithAnnotationQuery.java index c557b1f..8664eed 100644 --- a/java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/symbol/WithAnnotationQuery.java +++ b/java-analyzer-bundle.core/src/main/java/io/konveyor/tackle/core/internal/symbol/WithAnnotationQuery.java @@ -2,13 +2,11 @@ import io.konveyor.tackle.core.internal.query.AnnotationQuery; import org.eclipse.jdt.core.IAnnotation; -import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IImportDeclaration; import org.eclipse.jdt.core.IMemberValuePair; import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.search.SearchMatch; import org.eclipse.jdt.internal.core.Annotation; -import org.eclipse.jdt.internal.core.CompilationUnit; import org.eclipse.jdt.internal.core.SourceRefElement; import java.util.Arrays; @@ -51,7 +49,7 @@ default boolean matchesAnnotationQuery(SearchMatch match, List