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

View File

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