Skip to content

Commit

Permalink
Allow annotation queries to happen in ANNOTATION location
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Manuel Leflet Estrada <jleflete@redhat.com>
  • Loading branch information
jmle committed Jul 30, 2024
1 parent 082f676 commit 7877f14
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public RuleEntryParams(final String commandId, final List<Object> arguments) {

this.projectName = (String) obj.get("project");
this.query = (String) obj.get("query");
this.annotationQuery = AnnotationQuery.fromMap((Map<String, Object>) obj.get("annotationQuery"));
this.location = Integer.parseInt((String) obj.get("location"));
this.annotationQuery = AnnotationQuery.fromMap((Map<String, Object>) obj.get("annotationQuery"), location);
this.analysisMode = (String) obj.get("analysisMode");
this.includedPaths = (ArrayList<String>) obj.get("includedPaths");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 <code>@BeanAnnotation(value = "value")</code>
*/
private Map<String, String> elements;

public AnnotationQuery(String type, Map<String, String> elements) {
public AnnotationQuery(String type, Map<String, String> elements, boolean isOnAnnotation) {
this.type = type;
this.elements = elements;
this.isOnAnnotation = isOnAnnotation;
}

public String getType() {
Expand All @@ -32,7 +39,23 @@ public Map<String, String> getElements() {
return elements;
}

public static AnnotationQuery fromMap(Map<String, Object> 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<String, Object> query, int location) {
if (query == null) {
return null;
}
Expand All @@ -46,6 +69,6 @@ public static AnnotationQuery fromMap(Map<String, Object> query) {
elements.put(key, value);
}

return new AnnotationQuery(typePattern, elements);
return new AnnotationQuery(typePattern, elements, location == 4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<SymbolInformation> get(SearchMatch match) throws CoreException {
List<SymbolInformation> symbols = new ArrayList<>();
Expand All @@ -25,12 +33,31 @@ public List<SymbolInformation> 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<Class<? extends SourceRefElement>> 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) {
logInfo("unable to match for annotations: " + e);
return null;
}
}

public AnnotationQuery getAnnotationQuery() {
return annotationQuery;
}

public void setAnnotationQuery(AnnotationQuery annotationQuery) {
this.annotationQuery = annotationQuery;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,7 +49,7 @@ default boolean matchesAnnotationQuery(SearchMatch match, List<Class<? extends S
for (IAnnotation annotation : annotations) {
// See if the annotation's name matches the pattern given in the query for the annotation
String fqn = getFQN(annotation);
if (Pattern.matches(getAnnotationQuery().getType(), fqn)) {
if (getAnnotationQuery().matchesAnnotation(fqn)) {
// If the query has annotation elements to check, iterate through the annotation's values and check
if (getAnnotationQuery().getElements() != null && !getAnnotationQuery().getElements().entrySet().isEmpty()) {
IMemberValuePair[] memberValuePairs = annotation.getMemberValuePairs();
Expand Down

0 comments on commit 7877f14

Please sign in to comment.