猿来绘Java-44-自定义注解和元注解

自定义注解

定义新的 Annotation 类型使用 @interface 关键字,自定义注解是,可以参照Java中现有的注解,例如:@SuppressWarnings

自定义注解自动继承了java.lang.annotation.Annotation接口

Annotation 的成员变量在 Annotation 定义中以无参数方法的形式来声明。 其方法名和返回值定义了该成员的名字和类型。 我们称为配置参数。 类型只能是八种基本数据类型、 String类型、 Class类型、 enum类型、 Annotation类型、以上所有类型的数组。

可以在定义 Annotation 的成员变量时为其指定初始值, 指定成员变量的初始值可使用 default 关键字

如果只有一个参数成员, 建议使用参数名为value

如果定义的注解含有配置参数, 那么使用时必须指定参数值, 除非它有默认值。 格式是“参数名 = 参数值” , 如果只有一个参数成员, 且名称为value,可以省略“value=”

没有成员定义的 Annotation 称为标记; 包含成员变量的 Annotation 称为元数据 Annotation

注意: 自定义注解必须配上注解的信息处理流程才有意义。

package com.ylaihui.annotations;

public @interface MyAnnotations {
    String[]  value() default "myanno";
}

//MyAnnotationTest.java
package com.ylaihui.annotations;

//@MyAnnotations("myanno")
@MyAnnotations
interface Info{
    public void show();
}

public class MyAnnotationTest {
    public static void main(String[] args) {

    }
}

4种元注解的使用

比如新购买的华为手机,有一本说明书,说明书中的前一页有一段说明,是对说明书说明

 类比元注解,就是对现有注解的注解,对现有的注解的解释说明。

JDK5.0提供了4个标准的meta-annotation类型, 分别是:

Retention

Target

Documented

Inherited

Retention

Retention:指定所修饰的 Annotation 的生命周期:SOURCE\CLASS(默认行为)\RUNTIME 只有声明为RUNTIME生命周期的注解,才能通过反射获取。

SOURCE: Annotations are to be discarded by the compiler. 编译后的class文件不会有注解信息

CLASS: Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time. class文件有注解信息,但是运行时无法得到注解信息

RUNTIME: Annotations are to be recorded in the class file by the compiler and retained by the VM at run time,运行时可以获取注解信息,反射的时候用到

代码示例:

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
   
   

Target

@Target: 用于修饰 Annotation 定义, 用于指定被修饰的 Annotation 能用于修饰哪些程序元素。 @Target 也包含一个名为 value 的成员变量。

Target:用于指定所修饰的 Annotation 能用于修饰哪些程序元素

用法举例

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
...
...
...

注解 SuppressWarnings 上面用Target修饰, 表示 SuppressWarnings 可以修饰 类型、属性、方法、参数、构造器、局部变量, 如果 某个注解声明为 @Target({TYPE}), 那么只可以修饰类型,不可以修饰属性方法等

Documented

Documented:表示所修饰的注解在被javadoc解析时,保留下来。

@Documented: 用于指定被该元 Annotation 修饰的 Annotation 类将被javadoc 工具提取成文档。 默认情况下, javadoc是不包括注解的。

定义为Documented的注解必须设置Retention值为RUNTIME。

用法举例:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}

Inherited

@Inherited: 被它修饰的 Annotation 将具有继承性。如果某个类使用了被@Inherited 修饰的 Annotation, 则其子类将自动具有该注解。

比如:如果把标有@Inherited注解的自定义的注解标注在类级别上,子类则可以继承父类类级别的注解,实际应用中,使用较少

Inherited:被它修饰的 Annotation 将具有继承性。

某个类使用了注解 MyAnnotations,如果注解MyAnnotations有继承性,也就是被 @Inherited修饰,那么子类也继承了父类的注解。

package com.ylaihui.annotation1;

import java.lang.annotation.*;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.TYPE;

@Documented  // 所修饰的注解 MyAnnotation 在被javadoc解析时,保留下来
@Retention(RetentionPolicy.RUNTIME)  // MyAnnotation 注解信息 保留到运行时,反射可以获取注解信息
@Target({TYPE, FIELD})   // MyAnnotation 注解可以修饰 类型、属性
@Inherited  // 被它修饰 MyAnnotation 将具有继承性,父类是 MyAnnotation,其子类也是 MyAnnotation
public @interface MyAnnotation {
    String[] value() default "ylaihui";
}
//AnnotationTest.java
package com.ylaihui.annotation1;

import java.lang.annotation.Annotation;

@MyAnnotation
class Person{
    @MyAnnotation
    String name;
    int age;

    // '@MyAnnotation' not applicable to method
    //@MyAnnotation
    public void show(){
        System.out.println();
    }
}

class Student extends Person{

}

public class AnnotationTest {
    public static void main(String[] args) {
        // 子类 Student 继承了父类 Person的注解 MyAnnotation,
        // 因为 MyAnnotation 具有继承性是声明为 Inherited 的
        Annotation[] annotations = Student.class.getAnnotations();
        for (int i = 0; i < annotations.length; i++) {
            System.out.println(annotations[i]);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/asdfjklingok/article/details/118100204