All Downloads are FREE. Search and download functionalities are using the official Maven repository.

shz.core.AnnotationHelp Maven / Gradle / Ivy

There is a newer version: 2024.0.2
Show newest version
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 c) {
        return a != null && c != null && a.isAnnotationPresent(c);
    }

    public static boolean nonPresent(AnnotatedElement a, Class c) {
        return !isPresent(a, c);
    }

    public static boolean isAnyPresent(AnnotatedElement a, Class c1, Class c2) {
        return isPresent(a, c1) || isPresent(a, c2);
    }

    public static boolean nonAnyPresent(AnnotatedElement a, Class c1, Class c2) {
        return !isAnyPresent(a, c1, c2);
    }

    @SafeVarargs
    public static boolean isAnyPresent(AnnotatedElement a, Class... cs) {
        for (Class c : cs) if (isPresent(a, c)) return true;
        return false;
    }

    @SafeVarargs
    public static boolean nonAnyPresent(AnnotatedElement a, Class... cs) {
        return !isAnyPresent(a, cs);
    }

    public static boolean isAllPresent(AnnotatedElement a, Class c1, Class c2) {
        return isPresent(a, c1) && isPresent(a, c2);
    }

    public static boolean nonAllPresent(AnnotatedElement a, Class c1, Class c2) {
        return !isAllPresent(a, c1, c2);
    }

    @SafeVarargs
    public static boolean isAllPresent(AnnotatedElement a, Class... cs) {
        for (Class c : cs) if (nonPresent(a, c)) return false;
        return true;
    }

    @SafeVarargs
    public static boolean nonAllPresent(AnnotatedElement a, Class... cs) {
        return !isAllPresent(a, cs);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy