注解Annotation
自定义注解

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE,TYPE_PARAMETER,TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String[] value() default "aaa";
}
元注解
对现有的注解进行解释说明的注解
Retention:指定所修饰的Annotation的生命周期:SOURCE\CLASS(默认行为)\RUNTIME,只有声明为RUNTIME生命周期的注解,才能通过反射获取。
Target:用于指定被修饰的Annotation 能用于修饰哪些程序元素
----自定义注解通常都会指定以上两个元注解
Documented:表示所修饰的注解在被javadoc解析时保留下来
Inherited:被它修饰的Annotation 将具有继承性



类型注解
ElementType.TYPE_PARAMETER 表示该注解能写在类型变量的声明语句中,如泛型声明
ElementType.TYPE_USE 表示该注解能写在使用类型的任何语句中。
class Generic<@MyAnnotation T> {
public void show() throws @MyAnnotation RuntimeException{
ArrayList<@MyAnnotation String> list = new ArrayList();
int num = (@MyAnnotation int)10L;
}
}

浙公网安备 33010602011771号