Java 8对注解处理提供了两点改进:可重复的注解及可用于类型的注解。
测试代码-重复注解
MyAnnotation.java
package java8.annotation.repeat;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// 1.1.新建注解
// 1.3.在注解上增加注解,指定注解容器:@Repeatable(Annotations.class)
@Repeatable(Annotations.class)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value() default "ITCourse";
}
Annotations.java
package java8.annotation.repeat;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// 1.2.新建注解容器,成员类型是:注解[] value();
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotations {
// 成员类型是:注解[] value();
MyAnnotation[] value();
}
AnnotationTest.java
package java8.annotation.repeat;
import java.lang.reflect.Method;
import org.junit.Test;
public class AnnotationTest {
/*
* 1.可重复注解
* 1.1.新建注解
* 1.2.新建注解容器,成员类型是:注解[] value();
* 1.3.在注解上增加注解,指定注解容器:@Repeatable(Annotations.class)
*/
@MyAnnotation("冯强")
@MyAnnotation("廖周涛")
public void show() {
System.out.println("我是show!");
}
// 通过反射获取注解参数
@Test
public void test() throws Exception {
Class<AnnotationTest> clazz = AnnotationTest.class;
Method method = clazz.getMethod("show");
MyAnnotation[] myAnnotations = method.getAnnotationsByType(MyAnnotation.class);
for (MyAnnotation myAnnotation : myAnnotations) {
System.out.println(myAnnotation.value());
}
}
}
测试代码-类型注解
MyAnnotation.java
package java8.annotation.type;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/*
* 类型注解:
* 在@Target中新增了一个参数:ElementType.TYPE_PARAMETER
*/
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE,ElementType.TYPE_PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value();
}
AnnotationTypeTest.java
package java8.annotation.type;
import org.junit.Test;
public class AnnotationTypeTest {
// 类型主注解可以用来注解类型,比如判断定义的变量是不是null。一般结合框架check framework使用
public void show(@MyAnnotation("冯强") String name) {
System.out.println("我是show方法!" + name);
}
}
其它
- 源码下载
关注下方公众号,回复:Java8.code
欢迎加入交流群:451826376
更多信息:www.itcourse.top