Java注解各类声明

2021-03-29  本文已影响0人  taogan

构造函数声明

/**
 * 注解类型声明
 */
@Target(ElementType.ANNOTATION_TYPE)
public @interface AnnotationType {
}

构造函数声明

/**
 * 构造函数声明
 */
@Target(ElementType.CONSTRUCTOR)
@Retention(RetentionPolicy.RUNTIME)
public @interface Constructor {
    String value() default "";
}

字段声明(包括枚举常量)

/**
 * 类、接口(包括注释类型)或枚举声明
 */
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Fields {
    String value();
}

局部变量声明

/**
 * 局部变量声明
 * 对局部变量的注解只能在源码级别上进行处理.
 * 类文件并不描述局部变量,因此,所有局部变量注解在编译完一个类的时候就会被遗弃掉
 */
@Target(ElementType.LOCAL_VARIABLE)
@Retention(RetentionPolicy.SOURCE)
public @interface LocalVariable {
    String value();
}

方法声明

/**
* Method declaration
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Method {
  String value() default "";
}

形式参数声明

/**
 * 参数变量声明
 */
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface Parameter {
    String value();
}

类、接口(包括注释类型)或枚举声明

/**
 * 类、接口(包括注释类型)或枚举声明
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Type {
}

类型参数声明

/**
 * 参数类型声明
 */
@Target(ElementType.TYPE_PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface TypeParameter {
}

类型的使用

/**
 * 类型的使用
 */
@Target({ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
public @interface TypeUse {
}

示例

@AnnotationType
public @interface NonNull {
}

@Type
public class User<@TypeParameter V> {

    @Fields("userid")
    private String userid;

    @Constructor
    public User() {
    }

    @Method
    public User getUser(@Parameter("userid") String userid) {
        @LocalVariable("str")
        String str;
        return null;
    }

   private List<@TypeUse String> getListUser() {
        return null;
    }

}
上一篇 下一篇

猜你喜欢

热点阅读