这个注解用来标注这个注解可以用在什么地方,比如说用在方法上,用在属性值上,用在类名上。可以同时指定多个范围,里面定义的是一个数组。
TYPE_PARAMETER、TYPE_USE 这两个注解是JDK1.8以后新增的注解,知道怎么用,但是不知道具体的作用是什么用途。
参考链接:
Java 8 Annotation 新特性在软件质量和开发效率方面的提升
【JDK8】Annotation 功能增強
源码
@Documented 标注可以javadoc标注
@Retention(RetentionPolicy.RUNTIME) 保留到运行阶段
@Target(ElementType.ANNOTATION_TYPE) 指定目标只能在枚举上
public @interface Target {
/**
* 定义的是一个ElementType的枚举类。
*/
ElementType[] value();
}
ElementType 枚举常量值
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
定义在类上,包括注解(注解也算类) 枚举也可以
TYPE,
/** Field declaration (includes enum constants) */
定义在字段上面,包括枚举常量
FIELD,
/** Method declaration */
定义在方法上
METHOD,
/** Formal parameter declaration */
正式参数申明(这个啥意思不懂..)
PARAMETER,
/** Constructor declaration */
定义在构造方法上
CONSTRUCTOR,
/** Local variable declaration */
定义在局部变量,没想明白局部变量和上面的字段有啥区别,
LOCAL_VARIABLE,
/** Annotation type declaration */
定义在注解上,
ANNOTATION_TYPE,
/** Package declaration */
定义在包上面,定义在package上是要新建一个package测试,不是新建一个类
PACKAGE,
/**
* Type parameter declaration
*
* @since 1.8
*/
定义在参数上
TYPE_PARAMETER,
/**
* Use of a type
* 使用类型(啥意思不懂..)
*
* @since 1.8
*/
TYPE_USE
}
测试的全部代码
@Documented
@Repeatable(APIS.class)
@Target(ElementType.TYPE)
@APIANNOTATION_TYPE
public @interface API {
String content() default "请添加描述";
}
---------------------------------------
@Documented
@Target(ElementType.ANNOTATION_TYPE)
public @interface APIANNOTATION_TYPE {
String content() default "请添加描述";
}
------------------------------------
@Documented
@Target(ElementType.CONSTRUCTOR)
public @interface APICONSTRUCTOR {
}
------------------------------------------------
@Documented
@Target(ElementType.FIELD)
public @interface APIFIELD {
}
----------------------------------------------------
@Documented
@Target(ElementType.LOCAL_VARIABLE)
public @interface APILOCAL_VARIABLE {
}
---------------------------------------
@Documented
@Target(ElementType.METHOD)
public @interface APIMETHOD {
}
-------------------------------------------------
@Documented
@Target(ElementType.PACKAGE)
public @interface APIPACKAGE {
String content() default "请添加描述";
}
-------------------------------------------
@Documented
@Target(ElementType.PARAMETER)
public @interface APIPARAMETER {
}
--------------------------------------------
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface APIS {
API[] value();
}
-----------------------------------------------
@Documented
@Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})
public @interface APITYPE_PARAMETER {
String content() default "请添加描述";
}
---------------------------------------------
@Documented
@Target(ElementType.TYPE_USE)
public @interface APITYPE_USE {
String content() default "请添加描述";
}
--------------------------------------------------
@APIPACKAGE
package com.example.demo.annotest;
import com.example.demo.anno.APIPACKAGE;
-------------------------------------
测试类
@API(content = "学生对象1")
@API(content = "学生对象2")
@API(content = "学生对象3")
@APITYPE_USE
public class Student {
@APIFIELD
@APITYPE_USE
String name;
@APITYPE_USE
String age;
@APITYPE_USE
@APICONSTRUCTOR
public Student(@APIPARAMETER String name) {
@APILOCAL_VARIABLE
@APITYPE_USE
String cc;
this.name = name;
String height = new @APITYPE_PARAMETER String("1");
}
@APIMETHOD
@APITYPE_USE
public String getName() {
return name;
}
public void setName2(@APITYPE_USE Class<? extends Student> name) {
// this.name = name;
// this.name = name;
}
}
总结
总结一:
测试类型为package类型的时候,需要新建一个package,不是新建一个类,在类的包上引用。
总结二:
先了解这个怎么用,看源码能先看懂表面意思。