Skip to content

Commit

Permalink
Add support for finding TYPE_USE annotations via MergedAnnotations API
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Oct 4, 2023
1 parent b5928a3 commit 23033ca
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,10 +18,14 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

Expand Down Expand Up @@ -448,7 +452,22 @@ static Annotation[] getDeclaredAnnotations(AnnotatedElement source, boolean defe
cached = true;
}
else {
annotations = source.getDeclaredAnnotations();
if (source instanceof Class<?> clazz && clazz.isAnonymousClass()) {
// Find TYPE_USE annotations on an anonymous class.
List<Annotation> annotationList = new ArrayList<>();
AnnotatedType annotatedSuperclass = clazz.getAnnotatedSuperclass();
if (annotatedSuperclass != null) {
Collections.addAll(annotationList, annotatedSuperclass.getDeclaredAnnotations());
}
for (AnnotatedType annotatedInterface : clazz.getAnnotatedInterfaces()) {
Collections.addAll(annotationList, annotatedInterface.getDeclaredAnnotations());
}
annotations = annotationList.toArray(Annotation[]::new);
}
else {
// Find standard "declaration" annotations.
annotations = source.getDeclaredAnnotations();
}
if (annotations.length != 0) {
boolean allIgnored = true;
for (int i = 0; i < annotations.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,15 @@ void findAnnotationOnAnonymousClassWithSuperclass() {
assertThat(findAnnotation(new MetaMetaAnnotatedClass() {}.getClass(), TypeUseAnnotation.class)).isNull();

assertThat(findAnnotation(new @TypeUseAnnotation Object() {}.getClass(), TypeUseAnnotation.class)).isNotNull();
assertThat(findAnnotation(new @TypeUseAnnotation MetaMetaAnnotatedClass() {}.getClass(), TypeUseAnnotation.class)).isNotNull();
MetaMetaAnnotatedClass metaMetaAnnotatedClass = new @TypeUseAnnotation MetaMetaAnnotatedClass() {};
assertThat(findAnnotation(metaMetaAnnotatedClass.getClass(), TypeUseAnnotation.class)).isNotNull();

// Ensure that we can still find annotations in the class hierarchy.
assertThat(findAnnotation(metaMetaAnnotatedClass.getClass(), MetaMeta.class)).isNotNull();
assertThat(findAnnotation(metaMetaAnnotatedClass.getClass(), Meta2.class)).isNotNull();
Component component = findAnnotation(metaMetaAnnotatedClass.getClass(), Component.class);
assertThat(component).isNotNull();
assertThat(component.value()).isEqualTo("meta2");
}

@Test // gh-28896
Expand All @@ -1003,7 +1011,14 @@ void findAnnotationOnAnonymousClassWithInterface() {
assertThat(findAnnotation(new InterfaceWithMetaAnnotation() {}.getClass(), TypeUseAnnotation.class)).isNull();

assertThat(findAnnotation(new @TypeUseAnnotation Serializable() {}.getClass(), TypeUseAnnotation.class)).isNotNull();
assertThat(findAnnotation(new @TypeUseAnnotation InterfaceWithMetaAnnotation() {}.getClass(), TypeUseAnnotation.class)).isNotNull();
InterfaceWithMetaAnnotation interfaceWithMetaAnnotation = new @TypeUseAnnotation InterfaceWithMetaAnnotation() {};
assertThat(findAnnotation(interfaceWithMetaAnnotation.getClass(), TypeUseAnnotation.class)).isNotNull();

// Ensure that we can still find annotations in the interface hierarchy.
assertThat(findAnnotation(interfaceWithMetaAnnotation.getClass(), Meta1.class)).isNotNull();
Component component = findAnnotation(interfaceWithMetaAnnotation.getClass(), Component.class);
assertThat(component).isNotNull();
assertThat(component.value()).isEqualTo("meta1");
}

@SafeVarargs
Expand Down

0 comments on commit 23033ca

Please sign in to comment.