美文网首页
Java注解各类声明

Java注解各类声明

作者: taogan | 来源:发表于2021-03-29 11:42 被阅读0次

构造函数声明

/**
 * 注解类型声明
 */
@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;
    }

}

相关文章

  • Java注解各类声明

    构造函数声明 构造函数声明 字段声明(包括枚举常量) 局部变量声明 方法声明 形式参数声明 类、接口(包括注释类型...

  • 1.8 Java 注解annotation

    1.1 注解声明 Java注解Annotation,有声明注解和元注解 元注解:Java提供的元注解,所谓元注解就...

  • Java中的注解&反射

    注解 注解声明 声明一个注解类型 Java中所有的注解,默认是实现Annotation接口: 注解的声明使用@in...

  • Java语言高级特性--注解与反射

    注解 注解声明 声明一个注解类型 java中的所有的注解,默认实现Annotation接口: 声明一个"class...

  • Java注解简介篇

    摘要 本文详细介绍java注解是什么,如何声明java注解,如何解析java注解。最后介绍JDK提供的几大基本注解...

  • Kotlin 注解

    Kotlin 的注解完全兼容 Java 的注解。 声明注解 可以通过向注解类添加元注解(meta-annotati...

  • Java注解笔记

    Java注解详解 Java注解是JDK1.5以后添加的特性,自定义注解需要声明为@interface。 最简单的注...

  • Java 必须掌握的 12 种 Spring 常用注解!

    Java 必须掌握的 12 种 Spring 常用注解! 1.声明bean的注解 @Component 组件,没有...

  • RESTful Service use comment

    Java RESTful 注解@ApplicationPath@Path // 声明资源路径 @Path("/bo...

  • SpringBoot技术专题-整合SpringCache和Red

    Spring基于注解的缓存 对于缓存声明,spring的缓存提供了一组java注解: @Cacheable:触发缓...

网友评论

      本文标题:Java注解各类声明

      本文链接:https://www.haomeiwen.com/subject/nsxbhltx.html