Skip to content

Commit c2acbdf

Browse files
committed
SignaturePattern: Add exception for meta annotations
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 <[email protected]>
1 parent b5ff094 commit c2acbdf

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

org.aspectj.matcher/src/main/java/org/aspectj/weaver/patterns/SignaturePattern.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -200,35 +200,35 @@ public Object visit(WildAnnotationTypePattern node, Object data) {
200200
@Override
201201
public Object visit(ExactAnnotationTypePattern node, Object data) {
202202
ResolvedType resolvedType = node.getAnnotationType().resolve(scope.getWorld());
203+
AnnotationTargetKind[] targetKinds = resolvedType.getAnnotationTargetKinds();
204+
if (targetKinds == null)
205+
return data;
206+
boolean isMetaAnnotation = data instanceof AnnotationTypePattern || data instanceof AnyWithAnnotationTypePattern;
203207
if (targetsOtherThanTypeAllowed) {
204-
AnnotationTargetKind[] targetKinds = resolvedType.getAnnotationTargetKinds();
205-
if (targetKinds == null) {
206-
return data;
207-
}
208208
List<AnnotationTargetKind> incorrectTargets = new ArrayList<>();
209209
for (AnnotationTargetKind targetKind : targetKinds) {
210-
if (targetKind.getName().equals(kind.getName())
211-
|| (targetKind.getName().equals("PARAMETER") && node.isForParameterAnnotationMatch())) {
210+
if (
211+
isMetaAnnotation && targetKind.equals(AnnotationTargetKind.ANNOTATION_TYPE) ||
212+
targetKind.getName().equals(kind.getName()) ||
213+
targetKind.equals(AnnotationTargetKind.PARAMETER) && node.isForParameterAnnotationMatch()
214+
) {
212215
return data;
213216
}
214217
incorrectTargets.add(targetKind);
215218
}
216-
if (incorrectTargets.isEmpty()) {
219+
if (incorrectTargets.isEmpty())
217220
return data;
218-
}
219221
AnnotationTargetKind[] kinds = new AnnotationTargetKind[incorrectTargets.size()];
220222
incorrectTargetKinds.put(node, incorrectTargets.toArray(kinds));
221-
} else if (!targetsOtherThanTypeAllowed && !resolvedType.canAnnotationTargetType()) {
222-
AnnotationTargetKind[] targetKinds = resolvedType.getAnnotationTargetKinds();
223-
if (targetKinds == null) {
224-
return data;
225-
}
226-
// exception here is if parameter annotations are allowed
227-
if (parameterTargettingAnnotationsAllowed) {
228-
for (AnnotationTargetKind annotationTargetKind : targetKinds) {
229-
if (annotationTargetKind.getName().equals("PARAMETER") && node.isForParameterAnnotationMatch()) {
230-
return data;
231-
}
223+
}
224+
else if (!resolvedType.canAnnotationTargetType()) {
225+
for (AnnotationTargetKind targetKind : targetKinds) {
226+
if (
227+
isMetaAnnotation && targetKind.equals(AnnotationTargetKind.ANNOTATION_TYPE) ||
228+
parameterTargettingAnnotationsAllowed &&
229+
targetKind.equals(AnnotationTargetKind.PARAMETER) && node.isForParameterAnnotationMatch()
230+
) {
231+
return data;
232232
}
233233
}
234234
incorrectTargetKinds.put(node, targetKinds);

0 commit comments

Comments
 (0)