- @Target(ElementType.TYPE,ElementType.FIELD)
- 1.Type,类,接口
- 2.FIELD,成员变量
- 3.METHOD,成员方法
- 4.PARAMETER,方法参数
- 5.CONSTRUCTOR,构造器
- 6.LOCAL_VARIABLE,局部变量
- @Retention(RetentionPolicy.RUNTIME)
- 1.SOURCE,只作用在源码阶段,字节码文件中不存在
- 2.CLASS(默认值),保留到字节码文件阶段,运行阶段存在
- 3.RUNTIME(开发常用),一直保留到运行阶段
AnnotatedElement接口提供解析注解的方法 | 说明 |
public Annotation[ ] getDeclaredAnnotations() | 获取当前对象上面的注解 |
public T getDeclaredAnnotation(Class | 获取指定注解对象 |
public boolean isAnnotationPresent(Class | 判断当前对象是否存在某个注解 |
- package com.example.Anno;
-
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
-
- @Target({ElementType.TYPE, ElementType.METHOD})
- @Retention(RetentionPolicy.RUNTIME)
- public @interface MyTest4 {
- String value();
-
- double aaa() default 100;
-
- String[] bbb();
-
- }
- package com.example.Test;
-
- import com.example.Anno.MyTest4;
-
- @MyTest4(value = "蜘蛛精", bbb = {"至尊宝", "紫霞仙子"}, aaa = 99.5)
- public class Demo {
- @MyTest4(value = "蜘蛛精1", bbb = {"至尊宝1", "紫霞仙子1"}, aaa = 199.5)
-
- public void test1() {
-
- }
- }
- package com.example.Test;
-
- import com.example.Anno.MyTest4;
- import org.junit.Test;
-
- import java.lang.annotation.Annotation;
- import java.lang.reflect.Method;
-
- public class TestAnnotation {
- @Test
-
- public void parseClass() {
- // 1.先得到Class对象
- Class c = Demo.class;
- // 2.解析类上的注解
- // 判断该类上是否包含某个注解
- if (c.isAnnotationPresent(MyTest4.class)) {
- MyTest4 myTest4 = (MyTest4) c.getDeclaredAnnotation(MyTest4.class);
- System.out.println(myTest4.value());
- System.out.println(myTest4.aaa());
- System.out.println(myTest4.bbb());
- }
- }
-
- @Test
- public void parseMethod() throws NoSuchMethodException {
- // 1.先得到Class对象
- Class c = Demo.class;
- Method method = c.getDeclaredMethod("test1");
- // 2.解析方法上的注解
- // 判断该方法上是否包含某个注解
- if (method.isAnnotationPresent(MyTest4.class)) {
- MyTest4 myTest4 = method.getDeclaredAnnotation(MyTest4.class);
- System.out.println(myTest4.value());
- System.out.println(myTest4.aaa());
- System.out.println(myTest4.bbb().toString());
- }
- }
-
- }
- package com.example.Anno;
-
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
-
- // todo 自定义注解
- @Target(ElementType.METHOD)
- @Retention(RetentionPolicy.RUNTIME)
- public @interface MyTest {
-
- }
- package com.example.Test;
-
-
- import com.example.Anno.MyTest;
-
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
-
- public class TestAnnotation {
- @MyTest
- public void test1() {
- System.out.println("test1---");
- }
-
- public void test2() {
- System.out.println("test2---");
- }
-
- public void test3() {
- System.out.println("test2---");
- }
-
- public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
- TestAnnotation testAnnotation = new TestAnnotation();
- // 启动程序
- // 1.得到Class对象
- Class c = TestAnnotation.class;
- // 2.提取类中的全部方法
- Method[] declaredMethods = c.getDeclaredMethods();
- // 3.遍历数组中的每个方法,然后查看方法是否含有@MyTest注解
- for (Method declaredMethod : declaredMethods) {
- if (declaredMethod.isAnnotationPresent(MyTest.class)) {
- declaredMethod.invoke(testAnnotation);
- }
- }
- }
- }