Java annotation

常见注解包

java.lang.annotation.Retantion

java.lang.annotation.RetantionPolicy

java.lang.annotation.Target

java.lang.annotation.ElementType 

  • 自定义注解可以通过四个元注解@Retention,@Target,@Inherited,@Documented,分别说明它的声明周期,使用位置,是否被继承,是否被生成到API文档中。

  • Annotation 的成员在 Annotation 定义中以无参数有返回值的抽象方法的形式来声明,我们又称为配置参数。返回值类型只能是八种基本数据类型、String类型、Class类型、enum类型、Annotation类型、以上所有类型的数组

  • 可以使用 default 关键字为抽象方法指定默认返回值

  • 如果定义的注解含有抽象方法,那么使用时必须指定返回值,除非它有默认值。格式是“方法名 = 返回值”,如果只有一个抽象方法需要赋值,且方法名为value,可以省略“value=”,所以如果注解只有一个抽象方法成员,建议使用方法名value。

 

java 注解策略

RetantionPolicy:

Source  源码

Class    源码和类

Runtime  源码和类和运行时

* SOURCE:注解只在源代码中可用,编译器会丢弃这种注解。
* CLASS:注解在源代码和类文件中可用,但在运行时不可用。
* RUNTIME:注解在源代码、类文件和运行时都可用。

 @Retantion(RetantionPolicy.RUNTIME)

 @Target({ElementType.Field ...})

可以使用@Target来指定一个注解只能用于方法,或者只能用于类等。

如果注解只有一个 value字段,那么使用的时候 可以直接写 value值

@Inherited 注解子类自动继承父类的注解

@Inherited注解的自动继承特性是由Java的反射API实现的,而不是由@Inherited注解的代码直接实现的。  当你使用Class类的getAnnotation或isAnnotationPresent方法查询一个类的注解时,如果这个类没有直接声明这个注解,Java会检查这个注解是否被@Inherited标注。如果被@Inherited标注,Java会继续在这个类的父类中查找这个注解。  这个过程会一直持续到找到这个注解,或者到达类层次结构的顶部(Object类)。如果没有找到这个注解,那么getAnnotation方法会返回null,isAnnotationPresent方法会返回false。  这就是@Inherited如何实现注解的自动继承的。请注意,@Inherited只影响从超类继承的注解。对于实现的接口上的注解,或者用于标注方法、字段等元素的注解,@Inherited没有效果。

public class SonClass extends TestAnnotation {
    @Test
    public void extendAnnotationFromFather() {
        Class clazz = SonClass.class;
        System.out.println(clazz.isAnnotationPresent(MyAnnotation.class) + " 434343");
//        MyAnnotation myAnnotation = (MyAnnotation) clazz.getDeclaredAnnotation(MyAnnotation.class); //null
        MyAnnotation myAnnotation = (MyAnnotation) clazz.getAnnotation(MyAnnotation.class); //我是父类的注解
        System.out.println(myAnnotation.value());
       
    }
}
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE,ElementType.FIELD,ElementType.METHOD,ElementType.CONSTRUCTOR})
public @interface MyAnnotation {
    String value() default ""; // 带默认值的注解元素

//    int count(); // 不带默认值的注解元素
}
@MyAnnotation("我是父类的注解")
public class TestAnnotation {

用 getAnnotation 获取 父类注解

        在Java中,
         getDeclaredAnnotation和getAnnotation这两个方法都是用于获取注解的,但它们的行为有所不同。  
         getDeclaredAnnotation方法只能获取在当前类上直接声明的注解,不包括从父类继承的注解。
         也就是说,如果一个注解并未直接应用在当前类上,即使这个注解在父类上存在,
         getDeclaredAnnotation方法也无法获取到它。  
         getAnnotation方法则不同,它可以获取当前类上的注解,也包括从父类继承的注解。
         但是,这个“继承”只有在注解被@Inherited标注,并且在父类上使用时,才会生效。  
         所以,如果你想获取从父类继承的注解,你应该使用getAnnotation方法
         如果你只关心在当前类上直接声明的注解,不包括继承的注解,
         那么你应该使用getDeclaredAnnotation方法。       

public @interface Myannotation{

   String value() default "";

}

利用反射 获取  注解的信息

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE,ElementType.FIELD,ElementType.METHOD,ElementType.CONSTRUCTOR})
public @interface MyAnnotation {
    String value() default ""; // 带默认值的注解元素

//    int count(); // 不带默认值的注解元素
}
package com.gyk;

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * ClassName: TestAnnotation
 * Package: com.gyk
 * Description:
 *
 * @Author Samuel
 * @Create 2024/7/15 10:44
 * @Version 1.0
 */
@MyAnnotation2()
public class TestAnnotation {
    @MyAnnotation(value = "5dgajhsgda")
    private String str;

    @MyAnnotation("i'm m1")
    private void m1() {
        System.out.println("m1 log");
    }

    @MyAnnotation("public TestAnnotation")
    public TestAnnotation() {
        System.out.println("public TestAnnotation");
    }

    @Test
    public void getClassAnnotation() {
        Class clazz = TestAnnotation.class;
        if (clazz.isAnnotationPresent(MyAnnotation2.class)) {
            MyAnnotation2 annotation = (MyAnnotation2) clazz.getDeclaredAnnotation(MyAnnotation2.class);
            System.out.println(annotation.value());
        }
    }

    @Test
    public void getFieldAnnotation() throws NoSuchFieldException, InstantiationException, IllegalAccessException {
        Class clazz = TestAnnotation.class;
        Field field = clazz.getDeclaredField("str");
        TestAnnotation testAnnotation = (TestAnnotation) clazz.newInstance();
        System.out.println(field.isAnnotationPresent(MyAnnotation.class));
        if (field.isAnnotationPresent(MyAnnotation.class)) {
            MyAnnotation myAnnotation = (MyAnnotation) field.getDeclaredAnnotation(MyAnnotation.class);
            System.out.println(myAnnotation.value());
        }
    }

    @Test
    public void getMethodAnnotation() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Class clazz = TestAnnotation.class;
        Method method = clazz.getDeclaredMethod("m1");
        MyAnnotation myAnnotation = method.getDeclaredAnnotation(MyAnnotation.class);
        System.out.println(myAnnotation.value());
//        method.invoke(clazz.newInstance());
        Constructor constructor = clazz.getDeclaredConstructor();
        TestAnnotation testAnnotation1 = (TestAnnotation) constructor.newInstance();
        method.invoke(testAnnotation1);
    }

    @Test
    public void getConstructorAnnotation() throws NoSuchMethodException {
        Class clazz = TestAnnotation.class;
        Constructor constructor = clazz.getDeclaredConstructor();
        MyAnnotation myAnnotation = (MyAnnotation) constructor.getDeclaredAnnotation(MyAnnotation.class);
        System.out.println(myAnnotation.value());
    }


}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值