注解

——总结自《java编程思想》
注解(元数据)为我们在代码中添加信息提供了一张格式化的方式,使我们可以稍后某个时刻非常方便地使用这些数据

java中有三个内置注解:

  • @Override,表示当前的方法定义将覆盖超类中的方法。
  • @Deprecated,如果使用了该注解,那么编译器会发出警告信息。
  • @SuppressWarings,关闭不当的编译器警告信息。
    没有元素的注解称为标记注解。
    四种元注解:
  • @Target
    表示该注解可以用于什么地方。所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。
    ElementType参数:
    1. CONSTRUCTOR:用于描述构造器
    2. FIELD:用于描述域
    3. LOCAL_VARIABLE:用于描述局部变量
    4. METHOD:用于描述方法
    5. PACKAGE:用于描述包
    6. PARAMETER:用于描述参数
    7. TYPE:用于描述类、接口(包括注解类型) 或enum声明
  • @Retention
    表示需要在什么级别保存该注解信息。被保留的时间长短:某些Annotation仅出现在源代码中,而被编译器丢弃;而另一些却被编译在class文件中;编译在class文件中的Annotation可能会被虚拟机忽略,而另一些在class被装载时将被读取(请注意并不影响class的执行,因为Annotation与class在使用上是被分离的)。使用这个meta-Annotation可以对 Annotation的“生命周期”限制。
    RetentionPoicy值:
    1. SOURCE:在源文件中有效(即源文件保留)
    2. CLASS:在class文件中有效(即class保留)
    3. RUNTIME:在运行时有效(即运行时保留)
  • @Documented
    将次注解包含在Javadoc中。可以被例如javadoc此类的工具文档化。Documented是一个标记注解,没有成员。
  • @Inherited
    允许子类继承父类中的注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。

@Inherited annotation类型是被标注过的class的子类所继承。类并不从它所实现的接口继承annotation,方法并不从它所重载的方法继承annotation。

当@Inherited annotation类型标注的annotation的Retention是RetentionPolicy.RUNTIME,则反射API增强了这种继承性。如果我们使用java.lang.reflect去查询一个@Inherited annotation类型的annotation时,反射代码检查将展开工作:检查class和其父类,直到发现指定的annotation类型被发现,或者到达类继承结构的顶层。
注解处理器

import java.lang.annotation.*;  
  
@Target(ElementType.METHOD)  
@Retention(RetentionPolicy.RUNTIME)  
public @interface UseCase{  
    public int id();  
    public String description() default “no description”;  
}  

java 提供了Annotation相关的API,结合使用java的反射机制可以实现自定义的Annotation注解处理器(JDK中也提供了使用APT,Annotationprocess tool方式处理注解。

import java.lang.reflect.*;  
import java.util.*;  
  
public class UseCaseTracker{  
    public static void traceUseCases(List<Integer> useCases, Class<?> clazz){  
        //获取指定类中所有声明的方法  
for(Method m : clazz.getDeclaredMethods()){  
    //获取方法上指定类型的注解  
            UseCase uc = m.getAnnotation(UseCase.class);  
            if(uc != null){  
    System.out.println(“Found Use Case:+ uc.id() + “ ” + uc.description());  
    useCases.remove(new Integer(uc.id()));  
}  
}  
for(int i : useCases){  
    System.out.println(“Warning: Missing use case-+ i);  
}  
}  
public static void main(String[] args){  
    List<Integer> useCases = new ArrayLis<Integer>();  
    Collections.addAll(useCases, 47, 48, 49, 50);  
    trackUseCases(useCases, PasswordUtils.class);  
}  
}  

/*
运行结果:
Found Use Case:47 Passwords must contain at least onenumeric
Found Use Case:48 no description
Found Use Case:49 New Passwords can’t equal previously usedones
Warning: Missing use case-50
*/

这个程序用到了两个反射的方法:

  • getDeclaredMethods() 返回类声明的方法
  • m.getAnnotation(UseCase.class) 返回指定类型的注解对象。如果被注解的方法上没有该类型的注解,则返回null值。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值