shz.core.AnnotationHelp Maven / Gradle / Ivy
package shz.core;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
/**
* 标准注解
* Deprecated 标记为过时
* SuppressWarnings 阻止某个给定类型的警告信息
* SafeVarargs 断言varargs参数可安全使用
* Override 检查是否覆盖某个超类方法
* FunctionalInterface 将接口标记为只有一个抽象方法的函数式接口
* PostConstruct 构造之后执行
* PreDestroy 销毁之前执行
* Resource 在类或接口上:标记为在其他地方要用到的资源,在方法或域上:为"注入"而标记
* Resources 一个资源数组
* Generated 供代码生成工具来使用,任何生成的源代码都可以被注解,从而与程序员提供的代码区分开
* 元注解
* Target 指明可以应用这个注解的那些项,一条没有@Target限制的注解可以应用于任何项上
* Retention 指明这个注解可以保留多久
* Documented 为像Javadoc这样的归档工具提供了一些提示
* Inherited 如果一个类具有继承注解,那么它的所有子类都自动具有同样的注解
*/
public final class AnnotationHelp {
private AnnotationHelp() {
throw new IllegalStateException();
}
public static boolean isPresent(AnnotatedElement a, Class extends Annotation> c) {
return a != null && c != null && a.isAnnotationPresent(c);
}
public static boolean nonPresent(AnnotatedElement a, Class extends Annotation> c) {
return !isPresent(a, c);
}
public static boolean isAnyPresent(AnnotatedElement a, Class extends Annotation> c1, Class extends Annotation> c2) {
return isPresent(a, c1) || isPresent(a, c2);
}
public static boolean nonAnyPresent(AnnotatedElement a, Class extends Annotation> c1, Class extends Annotation> c2) {
return !isAnyPresent(a, c1, c2);
}
@SafeVarargs
public static boolean isAnyPresent(AnnotatedElement a, Class extends Annotation>... cs) {
for (Class extends Annotation> c : cs) if (isPresent(a, c)) return true;
return false;
}
@SafeVarargs
public static boolean nonAnyPresent(AnnotatedElement a, Class extends Annotation>... cs) {
return !isAnyPresent(a, cs);
}
public static boolean isAllPresent(AnnotatedElement a, Class extends Annotation> c1, Class extends Annotation> c2) {
return isPresent(a, c1) && isPresent(a, c2);
}
public static boolean nonAllPresent(AnnotatedElement a, Class extends Annotation> c1, Class extends Annotation> c2) {
return !isAllPresent(a, c1, c2);
}
@SafeVarargs
public static boolean isAllPresent(AnnotatedElement a, Class extends Annotation>... cs) {
for (Class extends Annotation> c : cs) if (nonPresent(a, c)) return false;
return true;
}
@SafeVarargs
public static boolean nonAllPresent(AnnotatedElement a, Class extends Annotation>... cs) {
return !isAllPresent(a, cs);
}
}