元注解:用于描述注解的注解

  @Target :描述注解能够作用的位置

  ElementType取值

  type :可以作用于类上

  METHOD :可以作用于方法上

  FIELD : 可以用于成员变量上

@Retention :描述注解被保留的阶段

@Retention(RetentionPolicy.RUNTIME)︰当前被描述的注解,会保留到class字节码文件中,并被Jvm读取到

@Documented :描述注解是否被抽取到api文档中

Inherited:描述注解是否被子类继承

 

import java.lang.annotation.*;

@Target(value = {ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MyAnno3 {


}

 

 

 

 

 

 

注解_解析注解

在程序使用(解析)注解:获取注解中定义的属性值

1。获取注解定义的位的对象(class,Method,Field)

2.获取指定的注解

@Pro(className = "Itc.annotation.Demo1",methodName = "show")
public class ReflectDemo5 {

    public static void main(String[] args){

        Class<ReflectTest> reflectTestClass = ReflectTest.class;
        Pro annotation = reflectTestClass.getAnnotation(Pro.class);

        String s = annotation.className();
        String s1 = annotation.methodName();
        System.out.println(s);
        System.out.println(s1);
    }

}

 

public class Demo1 {

    public void show(){
        System.out.println("demo1.....show");
    }
}

 

public class Demo1 {

    public void show(){
        System.out.println("demo1.....show");
    }
}

 

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Pro {

    String className();
    String  methodName();

}

 

posted on 2022-07-22 15:27  淤泥不染  阅读(21)  评论(0编辑  收藏  举报