添加判断是否为jdk元注解的方法;

This commit is contained in:
huangchengxing 2022-06-14 15:17:27 +08:00
parent 84711ad790
commit ef289dc676
2 changed files with 56 additions and 14 deletions

View File

@ -1,5 +1,6 @@
package cn.hutool.core.annotation; package cn.hutool.core.annotation;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.ReflectUtil;
@ -16,6 +17,7 @@ import java.lang.reflect.Method;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.function.Predicate; import java.util.function.Predicate;
/** /**
@ -27,6 +29,58 @@ import java.util.function.Predicate;
*/ */
public class AnnotationUtil { 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//
);
/**
* 是否为Jdk自带的元注解<br />
* 包括
* <ul>
* <li>{@link Target}</li>
* <li>{@link Retention}</li>
* <li>{@link Inherited}</li>
* <li>{@link Documented}</li>
* <li>{@link SuppressWarnings}</li>
* <li>{@link Override}</li>
* <li>{@link Deprecated}</li>
* </ul>
*
* @param annotationType 注解类型
* @return 是否为Jdk自带的元注解
*/
public static boolean isJdkMateAnnotation(Class<? extends Annotation> annotationType) {
return META_ANNOTATIONS.contains(annotationType);
}
/**
* 是否不为Jdk自带的元注解<br />
* 包括
* <ul>
* <li>{@link Target}</li>
* <li>{@link Retention}</li>
* <li>{@link Inherited}</li>
* <li>{@link Documented}</li>
* <li>{@link SuppressWarnings}</li>
* <li>{@link Override}</li>
* <li>{@link Deprecated}</li>
* </ul>
*
* @param annotationType 注解类型
* @return 是否为Jdk自带的元注解
*/
public static boolean isNotJdkMateAnnotation(Class<? extends Annotation> annotationType) {
return !isJdkMateAnnotation(annotationType);
}
/** /**
* 将指定的被注解的元素转换为组合注解元素 * 将指定的被注解的元素转换为组合注解元素
* *

View File

@ -39,18 +39,6 @@ public class CombinationAnnotationElement implements AnnotatedElement, Serializa
return new CombinationAnnotationElement(element, predicate); return new CombinationAnnotationElement(element, predicate);
} }
/**
* 元注解
*/
private static final Set<Class<? extends Annotation>> META_ANNOTATIONS = CollUtil.newHashSet(Target.class, //
Retention.class, //
Inherited.class, //
Documented.class, //
SuppressWarnings.class, //
Override.class, //
Deprecated.class//
);
/** /**
* 注解类型与注解对象对应表 * 注解类型与注解对象对应表
*/ */
@ -138,7 +126,7 @@ public class CombinationAnnotationElement implements AnnotatedElement, Serializa
// 直接注解 // 直接注解
for (Annotation annotation : annotations) { for (Annotation annotation : annotations) {
annotationType = annotation.annotationType(); annotationType = annotation.annotationType();
if (false == META_ANNOTATIONS.contains(annotationType)) { if (AnnotationUtil.isNotJdkMateAnnotation(annotationType)) {
if(test(annotation)){ if(test(annotation)){
declaredAnnotationMap.put(annotationType, annotation); declaredAnnotationMap.put(annotationType, annotation);
} }
@ -157,7 +145,7 @@ public class CombinationAnnotationElement implements AnnotatedElement, Serializa
Class<? extends Annotation> annotationType; Class<? extends Annotation> annotationType;
for (Annotation annotation : annotations) { for (Annotation annotation : annotations) {
annotationType = annotation.annotationType(); annotationType = annotation.annotationType();
if (false == META_ANNOTATIONS.contains(annotationType)) { if (AnnotationUtil.isNotJdkMateAnnotation(annotationType)) {
if(test(annotation)){ if(test(annotation)){
annotationMap.put(annotationType, annotation); annotationMap.put(annotationType, annotation);
} }