]> source.dussan.org Git - aspectj.git/commitdiff
SignaturePattern: Add exception for meta annotations
authorAlexander Kriegisch <Alexander@Kriegisch.name>
Fri, 12 Apr 2024 11:18:04 +0000 (13:18 +0200)
committerAlexander Kriegisch <Alexander@Kriegisch.name>
Fri, 12 Apr 2024 13:14:35 +0000 (15:14 +0200)
Upon meta annotation usage in signature patterns, lint warnings like the
following were issued during type parameter traversal:

  does not match because annotation @java.lang.annotation.Inherited
  has @Target{ElementType.ANNOTATION_TYPE} [Xlint:unmatchedTargetKind]

To avoid this, we now heuristically check if we are in a meta annotation
situation and, if so, permit it.

Signed-off-by: Alexander Kriegisch <Alexander@Kriegisch.name>
org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/SignaturePattern.java

index d04d127a4634d1bec85167f234a8a22db39ab498..750e3cbc733b33b9e1a10ae2422fdc856fc17ddf 100644 (file)
@@ -200,35 +200,35 @@ public class SignaturePattern extends PatternNode implements ISignaturePattern {
                @Override
                public Object visit(ExactAnnotationTypePattern node, Object data) {
                        ResolvedType resolvedType = node.getAnnotationType().resolve(scope.getWorld());
+                       AnnotationTargetKind[] targetKinds = resolvedType.getAnnotationTargetKinds();
+                       if (targetKinds == null)
+                               return data;
+                       boolean isMetaAnnotation = data instanceof AnnotationTypePattern || data instanceof AnyWithAnnotationTypePattern;
                        if (targetsOtherThanTypeAllowed) {
-                               AnnotationTargetKind[] targetKinds = resolvedType.getAnnotationTargetKinds();
-                               if (targetKinds == null) {
-                                       return data;
-                               }
                                List<AnnotationTargetKind> incorrectTargets = new ArrayList<>();
                                for (AnnotationTargetKind targetKind : targetKinds) {
-                                       if (targetKind.getName().equals(kind.getName())
-                                                       || (targetKind.getName().equals("PARAMETER") && node.isForParameterAnnotationMatch())) {
+                                       if (
+                                               isMetaAnnotation && targetKind.equals(AnnotationTargetKind.ANNOTATION_TYPE) ||
+                                                       targetKind.getName().equals(kind.getName()) ||
+                                                       targetKind.equals(AnnotationTargetKind.PARAMETER) && node.isForParameterAnnotationMatch()
+                                       ) {
                                                return data;
                                        }
                                        incorrectTargets.add(targetKind);
                                }
-                               if (incorrectTargets.isEmpty()) {
+                               if (incorrectTargets.isEmpty())
                                        return data;
-                               }
                                AnnotationTargetKind[] kinds = new AnnotationTargetKind[incorrectTargets.size()];
                                incorrectTargetKinds.put(node, incorrectTargets.toArray(kinds));
-                       } else if (!targetsOtherThanTypeAllowed && !resolvedType.canAnnotationTargetType()) {
-                               AnnotationTargetKind[] targetKinds = resolvedType.getAnnotationTargetKinds();
-                               if (targetKinds == null) {
-                                       return data;
-                               }
-                               // exception here is if parameter annotations are allowed
-                               if (parameterTargettingAnnotationsAllowed) {
-                                       for (AnnotationTargetKind annotationTargetKind : targetKinds) {
-                                               if (annotationTargetKind.getName().equals("PARAMETER") && node.isForParameterAnnotationMatch()) {
-                                                       return data;
-                                               }
+                       }
+                       else if (!resolvedType.canAnnotationTargetType()) {
+                               for (AnnotationTargetKind targetKind : targetKinds) {
+                                       if (
+                                               isMetaAnnotation && targetKind.equals(AnnotationTargetKind.ANNOTATION_TYPE) ||
+                                                       parameterTargettingAnnotationsAllowed &&
+                                                               targetKind.equals(AnnotationTargetKind.PARAMETER) && node.isForParameterAnnotationMatch()
+                                       ) {
+                                               return data;
                                        }
                                }
                                incorrectTargetKinds.put(node, targetKinds);