Annotation

Annotation对程序运行没有影响,它的目的在于对编译器或分析工具说明程序的某些信息,可以在包、类、方法、域成员等加上Annotation。每一个Annotation对应于一个实际的Annotation类型。J2SE 5.0中标准的常用Annotation如下:

  • 限定Override父类方法 @Override
  • 标示方法为Deprecated @Deprecated
  • 抑制编译器警告 @SuppressWarnings

meta-annotation

所谓meta-annotation就是Annotation类型的数据,也就是Annotation类型的Annotation,这样可以为处理Annotation类型的分析工具提供更多的信息。

告知编译器如何处理annotation @Retention

package java.lang.annotation;
public enum RetentionPolicy{
    SOURCE, //编译器处理完Annotation信息就没事了
    CLASS,    //编译器将Annotation存储于class文件中,默认
    RUNTIME //编译器将Annotation存储于class中,可有VM读取
}

限定annotation使用对象 @Target

package java.lang.annotation;
public enum ElementType{
    TYPE,    //适用 class, interface, enum
    FIELD,    //适用 field
    METHOD,   //适用method
    PARAMETER, //适用method上的parameter
    CONSTRUCTOR,    //适用constructor
    LOCAL_VARIABLE,  //适用局部变量
    ANNOTATION_TYPE, //适用annotation类型
    PACKAGE //适用package
}

要求为API文件的一部分 @Documented

在自定义Annotation时,添加上 @Documented 批注,则调用java doc时,会把它一起包含在API文件中。

子类是否继承父类的annotation @Inherited

在自定义Annotation时,添加上 @Inherited 批注,可以使自定义的Annotation类型在被继承后仍保留至子类中,默认是不会被继承到子类的。

 

自己定义一个 @ProcessAnnotation 并使用它

第一步,定义标签

package com.xiaofan.demo.annotation;

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

@Retention( RetentionPolicy.RUNTIME)
public @interface ProcessAnnotation {
    public enum Current{ NONE, REQUIRE, ANALYSIS, DESIGN, SYSTEM };
    
    Current current() default Current.NONE;
    String tester();
    boolean ok();
}

第二步,创建使用标签的方法

package com.xiaofan.demo.annotation;

public class Application {

    @ProcessAnnotation(
        current = ProcessAnnotation.Current.ANALYSIS,
        tester = "xiaofan",
        ok = true 
    )
    public void doSomething(){
        
    }
}

第三步,在类中获取标签的属性

 

package com.xiaofan.demo.annotation;

import java.lang.reflect.Method;

public class AnalysisApp {
    public static void main(String[] args) throws SecurityException, NoSuchMethodException {
        Class<Application> clazz = Application.class;
        
        //因为要取得doSomething()方法的上的标签,故要先取得方法实例
        Method method = clazz.getMethod("doSomething");
        
        if( method.isAnnotationPresent( ProcessAnnotation.class )){
            System.out.println("找到 @Process 标签");
            ProcessAnnotation annotation = method.getAnnotation( ProcessAnnotation.class );
            //取得成员值
            System.out.println("\tcurrent = " + annotation.current() );
            System.out.println("\ttester = " + annotation.tester() );
            System.out.println("\tcurrent = " + annotation.ok() );
        }
    }
}
这样,标签的使用就顺理成章,不显得那么神秘了。对自定义的标签愿意做什么事情,就随你的愿啦。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XiaoFan012

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值