This commit is contained in:
Looly 2022-06-30 18:40:08 +08:00
parent 6f24b9d403
commit adf23e1cf7
2 changed files with 39 additions and 39 deletions

View File

@ -33,12 +33,12 @@ public class AnnotationUtil {
* 元注解 * 元注解
*/ */
static final Set<Class<? extends Annotation>> META_ANNOTATIONS = CollUtil.newHashSet(Target.class, // static final Set<Class<? extends Annotation>> META_ANNOTATIONS = CollUtil.newHashSet(Target.class, //
Retention.class, // Retention.class, //
Inherited.class, // Inherited.class, //
Documented.class, // Documented.class, //
SuppressWarnings.class, // SuppressWarnings.class, //
Override.class, // Override.class, //
Deprecated.class// Deprecated.class//
); );
/** /**
@ -130,7 +130,7 @@ public class AnnotationUtil {
*/ */
public static <T> T[] getAnnotations(AnnotatedElement annotationEle, boolean isToCombination, Class<T> annotationType) { public static <T> T[] getAnnotations(AnnotatedElement annotationEle, boolean isToCombination, Class<T> annotationType) {
final Annotation[] annotations = getAnnotations(annotationEle, isToCombination, final Annotation[] annotations = getAnnotations(annotationEle, isToCombination,
(annotation -> null == annotationType || annotationType.isAssignableFrom(annotation.getClass()))); (annotation -> null == annotationType || annotationType.isAssignableFrom(annotation.getClass())));
final T[] result = ArrayUtil.newArray(annotationType, annotations.length); final T[] result = ArrayUtil.newArray(annotationType, annotations.length);
for (int i = 0; i < annotations.length; i++) { for (int i = 0; i < annotations.length; i++) {
@ -146,12 +146,12 @@ public class AnnotationUtil {
* @param annotationEle {@link AnnotatedElement}可以是ClassMethodFieldConstructorReflectPermission * @param annotationEle {@link AnnotatedElement}可以是ClassMethodFieldConstructorReflectPermission
* @param isToCombination 是否为转换为组合注解组合注解可以递归获取注解的注解 * @param isToCombination 是否为转换为组合注解组合注解可以递归获取注解的注解
* @param predicate 过滤器{@link Predicate#test(Object)}返回{@code true}保留否则不保留 * @param predicate 过滤器{@link Predicate#test(Object)}返回{@code true}保留否则不保留
* @return 注解对象 * @return 注解对象如果提供的{@link AnnotatedElement}{@code null}返回{@code null}
* @since 5.8.0 * @since 5.8.0
*/ */
public static Annotation[] getAnnotations(AnnotatedElement annotationEle, boolean isToCombination, Predicate<Annotation> predicate) { public static Annotation[] getAnnotations(AnnotatedElement annotationEle, boolean isToCombination, Predicate<Annotation> predicate) {
if (null == annotationEle) { if (null == annotationEle) {
return new Annotation[0]; return null;
} }
if (isToCombination) { if (isToCombination) {
@ -251,8 +251,8 @@ public class AnnotationUtil {
final String name = t.getName(); final String name = t.getName();
// 跳过自有的几个方法 // 跳过自有的几个方法
return (false == "hashCode".equals(name)) // return (false == "hashCode".equals(name)) //
&& (false == "toString".equals(name)) // && (false == "toString".equals(name)) //
&& (false == "annotationType".equals(name)); && (false == "annotationType".equals(name));
} }
return false; return false;
}); });
@ -288,13 +288,13 @@ public class AnnotationUtil {
final Target target = annotationType.getAnnotation(Target.class); final Target target = annotationType.getAnnotation(Target.class);
if (null == target) { if (null == target) {
return new ElementType[]{ElementType.TYPE, // return new ElementType[]{ElementType.TYPE, //
ElementType.FIELD, // ElementType.FIELD, //
ElementType.METHOD, // ElementType.METHOD, //
ElementType.PARAMETER, // ElementType.PARAMETER, //
ElementType.CONSTRUCTOR, // ElementType.CONSTRUCTOR, //
ElementType.LOCAL_VARIABLE, // ElementType.LOCAL_VARIABLE, //
ElementType.ANNOTATION_TYPE, // ElementType.ANNOTATION_TYPE, //
ElementType.PACKAGE// ElementType.PACKAGE//
}; };
} }
return target.value(); return target.value();
@ -376,14 +376,14 @@ public class AnnotationUtil {
* @see SyntheticAnnotation * @see SyntheticAnnotation
*/ */
public static <T extends Annotation> List<T> getAllSynthesisAnnotations(AnnotatedElement annotatedEle, Class<T> annotationType) { public static <T extends Annotation> List<T> getAllSynthesisAnnotations(AnnotatedElement annotatedEle, Class<T> annotationType) {
AnnotationScanner[] scanners = new AnnotationScanner[] { AnnotationScanner[] scanners = new AnnotationScanner[]{
new MetaAnnotationScanner(), new TypeAnnotationScanner(), new MethodAnnotationScanner(), new FieldAnnotationScanner() new MetaAnnotationScanner(), new TypeAnnotationScanner(), new MethodAnnotationScanner(), new FieldAnnotationScanner()
}; };
return AnnotationScanner.scanByAnySupported(annotatedEle, scanners).stream() return AnnotationScanner.scanByAnySupported(annotatedEle, scanners).stream()
.map(SyntheticAnnotation::of) .map(SyntheticAnnotation::of)
.map(annotation -> annotation.getAnnotation(annotationType)) .map(annotation -> annotation.getAnnotation(annotationType))
.filter(Objects::nonNull) .filter(Objects::nonNull)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
/** /**
@ -457,13 +457,13 @@ public class AnnotationUtil {
static Map<String, Method> getAttributeMethods(Class<? extends Annotation> annotationType) { static Map<String, Method> getAttributeMethods(Class<? extends Annotation> annotationType) {
// 获取全部注解属性值 // 获取全部注解属性值
Map<String, Method> attributeMethods = Stream.of(annotationType.getDeclaredMethods()) Map<String, Method> attributeMethods = Stream.of(annotationType.getDeclaredMethods())
.filter(AnnotationUtil::isAttributeMethod) .filter(AnnotationUtil::isAttributeMethod)
.collect(Collectors.toMap(Method::getName, Function.identity())); .collect(Collectors.toMap(Method::getName, Function.identity()));
// 处理别名 // 处理别名
attributeMethods.forEach((methodName, method) -> { attributeMethods.forEach((methodName, method) -> {
String alias = Opt.ofNullable(method.getAnnotation(Alias.class)) String alias = Opt.ofNullable(method.getAnnotation(Alias.class))
.map(Alias::value) .map(Alias::value)
.orElse(null); .orElse(null);
if (ObjectUtil.isNull(alias)) { if (ObjectUtil.isNull(alias)) {
return; return;
} }
@ -471,9 +471,9 @@ public class AnnotationUtil {
Assert.isTrue(attributeMethods.containsKey(alias), "No method for alias: [{}]", alias); Assert.isTrue(attributeMethods.containsKey(alias), "No method for alias: [{}]", alias);
Method aliasAttributeMethod = attributeMethods.get(alias); Method aliasAttributeMethod = attributeMethods.get(alias);
Assert.isTrue( Assert.isTrue(
ObjectUtil.isNull(aliasAttributeMethod) || ClassUtil.isAssignable(method.getReturnType(), aliasAttributeMethod.getReturnType()), ObjectUtil.isNull(aliasAttributeMethod) || ClassUtil.isAssignable(method.getReturnType(), aliasAttributeMethod.getReturnType()),
"Return type of the alias method [{}] is inconsistent with the original [{}]", "Return type of the alias method [{}] is inconsistent with the original [{}]",
aliasAttributeMethod.getClass(), method.getParameterTypes() aliasAttributeMethod.getClass(), method.getParameterTypes()
); );
attributeMethods.put(methodName, aliasAttributeMethod); attributeMethods.put(methodName, aliasAttributeMethod);
}); });

View File

@ -14,14 +14,14 @@ public class AnnotationUtilTest {
@Test @Test
public void getCombinationAnnotationsTest(){ public void getCombinationAnnotationsTest(){
Annotation[] annotations = AnnotationUtil.getAnnotations(ClassWithAnnotation.class, true); final Annotation[] annotations = AnnotationUtil.getAnnotations(ClassWithAnnotation.class, true);
Assert.assertNotNull(annotations); Assert.assertNotNull(annotations);
Assert.assertEquals(3, annotations.length); Assert.assertEquals(3, annotations.length);
} }
@Test @Test
public void getCombinationAnnotationsWithClassTest(){ public void getCombinationAnnotationsWithClassTest(){
AnnotationForTest[] annotations = AnnotationUtil.getCombinationAnnotations(ClassWithAnnotation.class, AnnotationForTest.class); final AnnotationForTest[] annotations = AnnotationUtil.getCombinationAnnotations(ClassWithAnnotation.class, AnnotationForTest.class);
Assert.assertNotNull(annotations); Assert.assertNotNull(annotations);
Assert.assertEquals(2, annotations.length); Assert.assertEquals(2, annotations.length);
Assert.assertEquals("测试", annotations[0].value()); Assert.assertEquals("测试", annotations[0].value());
@ -29,7 +29,7 @@ public class AnnotationUtilTest {
@Test @Test
public void getAnnotationValueTest() { public void getAnnotationValueTest() {
Object value = AnnotationUtil.getAnnotationValue(ClassWithAnnotation.class, AnnotationForTest.class); final Object value = AnnotationUtil.getAnnotationValue(ClassWithAnnotation.class, AnnotationForTest.class);
Assert.assertEquals("测试", value); Assert.assertEquals("测试", value);
} }
@ -40,7 +40,7 @@ public class AnnotationUtilTest {
Assert.assertEquals("", ClassWithAnnotation.class.getAnnotation(AnnotationForTest.class).retry()); Assert.assertEquals("", ClassWithAnnotation.class.getAnnotation(AnnotationForTest.class).retry());
// 加别名适配 // 加别名适配
AnnotationForTest annotation = AnnotationUtil.getAnnotationAlias(ClassWithAnnotation.class, AnnotationForTest.class); final AnnotationForTest annotation = AnnotationUtil.getAnnotationAlias(ClassWithAnnotation.class, AnnotationForTest.class);
Assert.assertEquals("测试", annotation.retry()); Assert.assertEquals("测试", annotation.retry());
} }
@ -56,7 +56,7 @@ public class AnnotationUtilTest {
public void scanMetaAnnotationTest() { public void scanMetaAnnotationTest() {
// RootAnnotation -> RootMetaAnnotation1 -> RootMetaAnnotation2 -> RootMetaAnnotation3 // RootAnnotation -> RootMetaAnnotation1 -> RootMetaAnnotation2 -> RootMetaAnnotation3
// -> RootMetaAnnotation3 // -> RootMetaAnnotation3
List<Annotation> annotations = AnnotationUtil.scanMetaAnnotation(RootAnnotation.class); final List<Annotation> annotations = AnnotationUtil.scanMetaAnnotation(RootAnnotation.class);
Assert.assertEquals(4, annotations.size()); Assert.assertEquals(4, annotations.size());
Assert.assertEquals(RootMetaAnnotation3.class, annotations.get(0).annotationType()); Assert.assertEquals(RootMetaAnnotation3.class, annotations.get(0).annotationType());
Assert.assertEquals(RootMetaAnnotation1.class, annotations.get(1).annotationType()); Assert.assertEquals(RootMetaAnnotation1.class, annotations.get(1).annotationType());
@ -68,7 +68,7 @@ public class AnnotationUtilTest {
public void scanClassTest() { public void scanClassTest() {
// TargetClass -> TargetSuperClass ----------------------------------> SuperInterface // TargetClass -> TargetSuperClass ----------------------------------> SuperInterface
// -> TargetSuperInterface -> SuperTargetSuperInterface -> SuperInterface // -> TargetSuperInterface -> SuperTargetSuperInterface -> SuperInterface
List<Annotation> annotations = AnnotationUtil.scanClass(TargetClass.class); final List<Annotation> annotations = AnnotationUtil.scanClass(TargetClass.class);
Assert.assertEquals(5, annotations.size()); Assert.assertEquals(5, annotations.size());
Assert.assertEquals("TargetClass", ((AnnotationForTest)annotations.get(0)).value()); Assert.assertEquals("TargetClass", ((AnnotationForTest)annotations.get(0)).value());
Assert.assertEquals("TargetSuperClass", ((AnnotationForTest)annotations.get(1)).value()); Assert.assertEquals("TargetSuperClass", ((AnnotationForTest)annotations.get(1)).value());
@ -81,9 +81,9 @@ public class AnnotationUtilTest {
public void scanMethodTest() { public void scanMethodTest() {
// TargetClass -> TargetSuperClass // TargetClass -> TargetSuperClass
// -> TargetSuperInterface // -> TargetSuperInterface
Method method = ReflectUtil.getMethod(TargetClass.class, "testMethod"); final Method method = ReflectUtil.getMethod(TargetClass.class, "testMethod");
Assert.assertNotNull(method); Assert.assertNotNull(method);
List<Annotation> annotations = AnnotationUtil.scanMethod(method); final List<Annotation> annotations = AnnotationUtil.scanMethod(method);
Assert.assertEquals(3, annotations.size()); Assert.assertEquals(3, annotations.size());
Assert.assertEquals("TargetClass", ((AnnotationForTest)annotations.get(0)).value()); Assert.assertEquals("TargetClass", ((AnnotationForTest)annotations.get(0)).value());
Assert.assertEquals("TargetSuperClass", ((AnnotationForTest)annotations.get(1)).value()); Assert.assertEquals("TargetSuperClass", ((AnnotationForTest)annotations.get(1)).value());