jdk8中注解的新特新:
1.可重复注解
2.类型注解
一:可重复注解
什么是重复注解?可重复写的注解。
例如:
注解:
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
public @interface MyAnnotation {
String value() default "hello";
}
使用:
@MyAnnotation(value = "hi")
@MyAnnotation(value = "abc")
class Person {
}
但是这样写还差点意思,我们还要做一些配置
在jdk8之前的写法
创建MyAnntation数组
public @interface MyAnnotations {
MyAnnotation[] value();
}
使用:
@MyAnnotations({@MyAnnotation(value = "hi"),@MyAnnotation(value = "abc")})
class Person {
}
jdk8以后
在注解MyAnntation中引入一个注解@Repeatable():可重复的
注解MyAnntation如下
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
@Inherited
@Repeatable(MyAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
public @interface MyAnnotation {
String value() default "hello";
}
在MyAnnotations注解中的声明周期以及可修饰的结构的要与MyAnnotation注解中的保持一致
注解MyAnnotations:
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
public @interface MyAnnotations {
MyAnnotation[] value();
}
使用:
@MyAnnotation(value = "hi")
@MyAnnotation(value = "abc")
class Person {
}
二:类型注解
在Java 8之前:注解只能是在声明的地方所使用,
Java8开始,注解可以应用 在任何地方。
ElementType.TYPE_PARAMETER 表示该注解能写在类型变量的声明语句中(如:泛型声明)。
ElementType.TYPE_USE 表示该注解能写在使用类型的任何语句中。
1.ElementType.TYPE_PARAMETER 使用:
class Generic<@MyAnnotation T>{
}
但是这个时候会报错,我们在MyAnnotation中可使用的类型中加入TYPE_PARAMETER
注解MyAnnotation:
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
@Inherited
@Repeatable(MyAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE,TYPE_PARAMETER})
public @interface MyAnnotation {
String value() default "hello";
}
总结: ① 在MyAnnotation上声明@Repeatable成员值为MyAnnotations.class
② MyAnnotation的Target和Retention等元注解与MyAnnotations相同
2.ElementType.TYPE_USE
同上方法同理在MyAnnotation中可使用的类型中加入ElementType.TYPE_USE
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
@Inherited
@Repeatable(MyAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE,TYPE_PARAMETER,TYPE_USE})
public @interface MyAnnotation {
String value() default "hello";
}
使用:
class Generic<@MyAnnotation T>{
public void show() throws @MyAnnotation RuntimeException{
ArrayList<@MyAnnotation String> list = new ArrayList<>();
int num = (@MyAnnotation int) 10L;
}
}
感谢观看!!!