com.zusmart.basic.toolkit.AnnotationUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zusmart-basic Show documentation
Show all versions of zusmart-basic Show documentation
基础模块,提供配置,日志,SPI,图排序,路径匹配,资源扫描,包扫描,常用工具类
package com.zusmart.basic.toolkit;
import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public final class AnnotationUtils {
/**
* 判断构造函数是否包含指定注解
*
* @param constructor
* 构造函数
* @param annotation
* 指定的注解
* @return true为存在
*/
public static boolean hasAnnotation(Constructor> constructor, Class extends Annotation> annotation) {
return null != constructor.getAnnotation(annotation);
}
public static boolean hasAnnotation(AccessibleObject accessibleObject, Class extends Annotation> annotation) {
return null != accessibleObject.getAnnotation(annotation);
}
public static boolean hasAnnotation(Method method, Class extends Annotation> annotation) {
return null != method.getAnnotation(annotation);
}
public static boolean hasAnnotation(Field field, Class extends Annotation> annotation) {
return null != field.getAnnotation(annotation);
}
public static boolean hasAnnotation(Class> clazz, Class extends Annotation> annotation) {
return null != clazz.getAnnotation(annotation);
}
public static boolean hasAnnotation(Annotation[][] annotations, Class extends Annotation> annotation) {
if (null == annotations || annotations.length == 0) {
return false;
}
for (int i = 0; i < annotations.length; i++) {
Annotation[] ans = annotations[i];
if (null == ans || ans.length == 0) {
continue;
}
for (int j = 0; j < ans.length; j++) {
Annotation an = ans[j];
if (an.annotationType().equals(annotation)) {
return true;
}
}
}
return false;
}
public static T getAnnotation(AccessibleObject accessibleObject, Class annotation) {
return accessibleObject.getAnnotation(annotation);
}
public static T getAnnotation(Method method, Class annotation) {
return method.getAnnotation(annotation);
}
public static T getAnnotation(Field field, Class annotation) {
return field.getAnnotation(annotation);
}
public static T getAnnotation(Class> clazz, Class annotation) {
return clazz.getAnnotation(annotation);
}
public static Annotation[] getAnnotation(Class>[] parameterTypes, Annotation[][] annotations, Class extends Annotation> annotation) {
if (null == parameterTypes || parameterTypes.length == 0) {
return new Annotation[0];
}
Annotation[] list = new Annotation[parameterTypes.length];
if (null == parameterTypes || parameterTypes.length == 0) {
return list;
}
for (int i = 0; i < parameterTypes.length; i++) {
Annotation[] ans = annotations[i];
Annotation an = null;
for (Annotation a : ans) {
if (a.annotationType().equals(annotation)) {
an = a;
break;
}
}
list[i] = an;
}
return list;
}
}