Skip to content

Commit

Permalink
Refactor and cleanup
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 Aug 22, 2024
1 parent 3b99405 commit d91294a
Showing 1 changed file with 4 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,40 +50,13 @@ default boolean matchesAnnotationQuery(SearchMatch match, List<Class<? extends S
// See if the annotation's name matches the pattern given in the query for the annotation
String fqn = getFQN(annotation);
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();
Set<Map.Entry<String, String>> ruleAnnotationElems = getAnnotationQuery().getElements().entrySet();
for (IMemberValuePair member : memberValuePairs) {
for (Map.Entry<String, String> ruleAnnotationElem : ruleAnnotationElems) {
if (ruleAnnotationElem.getKey().equals(member.getMemberName())) {
// Member values can be arrays. In this case, lets iterate over it and compare:
if (member.getValue() instanceof Object[]) {
Object[] values = (Object[]) member.getValue();
// TODO: at the moment we are just toString()ing the values.
// We might want to make this more sophisticated, relying on
// member.getValueKind() to match on specific kinds.
return Arrays.stream(values).anyMatch(v -> Pattern.matches(ruleAnnotationElem.getValue(), v.toString()));
} else {
if (Pattern.matches(ruleAnnotationElem.getValue(), member.getValue().toString())) {
return true;
}
}
}
}

}
} else {
// No annotation elements, but the annotation itself matches
return true;
}
return doElementsMatch((Annotation) annotation);
} else {
// The LS doesn't seem to be able to match on annotations within annotations, but
// if the main annotation doesn't match, there might be some annotations inside.
// if the main annotation doesn't match, there might be some annotations inside:
for (IMemberValuePair member : annotation.getMemberValuePairs()) {
if (member.getValueKind() == IMemberValuePair.K_ANNOTATION) {
if (member.getValue() instanceof Object[]) {
System.out.println("Hola");
Object[] objs = (Object[]) member.getValue();
for (int i = 0; i < objs.length; i++) {
Annotation innerAnnotation = (Annotation) objs[i];
Expand Down Expand Up @@ -165,7 +138,7 @@ private boolean doElementsMatch(Annotation annotation) throws JavaModelException
Set<Map.Entry<String, String>> ruleAnnotationElems = getAnnotationQuery().getElements().entrySet();
boolean allElementsMatch = true;
boolean oneElementMatched = false;
// TODO: there is a problem with defaults: they don't appear in the memberValuePairs, even when explicitly declared
// TODO: there is a problem with defaults: they don't appear in the memberValuePairs so they cannot be matched
for (int i = 0; i < memberValuePairs.length && allElementsMatch; i++) {
IMemberValuePair member = memberValuePairs[i];
for (Map.Entry<String, String> ruleAnnotationElem : ruleAnnotationElems) {
Expand All @@ -176,7 +149,7 @@ private boolean doElementsMatch(Annotation annotation) throws JavaModelException
Object[] values = (Object[]) member.getValue();
// TODO: at the moment we are just toString()ing the values.
// We might want to make this more sophisticated, relying on
// member.getValueKind() to match on specific kinds.
// member.getValueKind() to match on specific kinds. This however can match
boolean valueMatches = Arrays.stream(values).anyMatch(v -> Pattern.matches(ruleAnnotationElem.getValue(), v.toString()));
oneElementMatched |= valueMatches;
allElementsMatch &= valueMatches;
Expand Down

0 comments on commit d91294a

Please sign in to comment.