java成神之——注释修饰符

注释修饰符

自定义注释

元注释

用来注释自定义注释的注释

@Target 限定注释允许加载的目标
    @Target(ElementType.METHOD)                     只能用于方法
    @Target({ElementType.FIELD, ElementType.TYPE})  可以用于字段和类型

    ANNOTATION_TYPE                                 注解其他的注释
    CONSTRUCTOR                                     注解构造函数
    FIELD                                           注解字段和枚举常量
    LOCAL_VARIABLE                                  注解局部变量
    PACKAGE
    METHOD                                          注解方法
    PARAMETER                                       注解方法参数
    TYPE                                            注解class,interface,enum
    TYPE_PARAMETER                                  注解泛型参数
    TYPE_USE                                        注解强转类型

@Retention 设置注解在程序runtime时期能否被反射访问到 
    @Retention(RetentionPolicy.RUNTIME)             运行反射访问

    CLASS                                           值运行在class文件中访问,而不是runtime
    RUNTIME
    SOURCE                                          能够在compiletime配访问到,但是不会添加到class文件中

@Documented 让文档生成器能够识别

@Inherited 这个注释用来改变注释查询方式,从而只用来检查父类,直到发现目标

@Repeatable 多个注释实例能够被附加到注释目标

通过反射在runtime访问注释

声明注释
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface MyAnnotation {
    String param1() default "someValue";
    boolean param2() default true;
    int[] param3() default {};
}

使用注释
@MyAnnotation
public static void Test() throws Exception {
    Annotation[] annotationsByType = MyAnnotation.class.getAnnotations();
    System.out.println("----------" + Arrays.toString(annotationsByType));
}

内置注释

@Override                           重写父类方法
@Deprecated                         废弃api 
@SuppressWarnings("unchecked")      取消系统的某些警告信息,尽量少用
@SafeVarargs                        抑制可变参数警告
    @SafeVarargs
    public static<T> void Test(T... lists) throws Exception {
    }
@FunctionalInterface                用来声明函数式接口,也就是只能包含一个方法
    @FunctionalInterface
	public interface Test<T> {
		boolean test(T t);
	}

多注释实例

错误写法

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MyAnnotation1 {
    String param();
}

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2 {
    String param() default "someValue";
}

报错
@MyAnnotation1(param="someValue")
@MyAnnotation1(param="someValue") 

使用容器改写

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface MyAnnotation1 {
    String param();
}

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2 {
    MyAnnotation1[] param();
}

@MyAnnotation2(param={
    @MyAnnotation1(param="someValue1"),
    @MyAnnotation1(param="someValue2"),
})

获取注释的值
MyAnnotation1[] MyAnnotations = TestController.class.getAnnotation(MyAnnotation2.class).param();
           
for (MyAnnotation1 item : MyAnnotations) {
    System.out.println(item.param());
}

使用@Repeatable元注释

@Repeatable(MyAnnotation2.class)
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation1 {
    String value();
}

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2 {
    MyAnnotation1[] value();
}

@MyAnnotation1(value="someValue1")
@MyAnnotation1(value="someValue2")

获取注释值

MyAnnotation1[] MyAnnotations = TestController.class.getAnnotationsByType(MyAnnotation1.class);
for (MyAnnotation1 item : MyAnnotations) {
    System.out.println(item.value());
}

注释继承

添加了@Inherited,继承的子类可以访问父类的注释

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation1 {
    String value();
}

@MyAnnotation1(value="someValue1")
class Father {

}

public class Test extends Father{
    MyAnnotation1[] MyAnnotations = Test.class.getAnnotationsByType(MyAnnotation1.class);
    for (MyAnnotation1 item : MyAnnotations) {
        System.out.println(item.value()); 
    }
}

使用反射获取注释

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation1 {
    String value();
}

获取类的注释

@MyAnnotation1(value="someValue1")
public class Test{}

MyAnnotation1 annotation = Test.class.getAnnotation(MyAnnotation1.class);
System.out.println(annotation.value());

获取方法的注释

@MyAnnotation1(value="someValue1")
public Object Test() throws Exception {}

Method method = TestController.class.getMethod("Test", null);
MyAnnotation1 annotation = (MyAnnotation1)method.getAnnotation(MyAnnotation1.class);
System.out.println(annotation.value());

结语

本文章是java成神的系列文章之一

如果你想知道,但是本文没有的,请下方留言

我会第一时间总结出来并发布填充到本文
原文地址:https://www.cnblogs.com/ye-hcj/p/9745758.html