博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java注解
阅读量:4229 次
发布时间:2019-05-26

本文共 2375 字,大约阅读时间需要 7 分钟。

注解(也被称为元数据)为我们在代码中添加信息提供了一种形式化的方法,使我们可以在稍后某个时刻非常方便地使用这些数据。

目前主流的框架多用注解,学好注解对阅读源码很有意义。

1.定义注解

注解的定义看起来很像接口的定义:如下:

@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface UseCase {    public int id();    public String description() default "no description";}
定义注解时,会需要一些元注解,如@Target和@Retention。@Target用来定义你的注解将应用于什么地方(例如是一个方法或者一个域)。

@Rectetion用来定义该注解在哪一个级别可用,在源代码中(SOURCE),类方法中(CLASS)或者运行时(RUNTIME)。

在注解中,一般都会包含一些元素以表示某些值。当分析处理注解时,程序或工具可以利用这些值。注解的元素看起来就像接口的方法,

但是你可以为其制定默认值。

2.使用注解

public class PasswordUtils {    @UseCase(id = 47, description = "Passwords must contain at least one numeric")    public boolean validatePassword(String password) {        return (password.matches("\\w*\\d\\w*"));    }    @UseCase(id = 48)    public String encryptPassword(String password) {        return new StringBuilder(password).reverse().toString();    }    @UseCase(id = 49, description = "New passwords can't equal previously used ones")    public boolean checkForNewPassword(List
prevPasswords, String password) { return !prevPasswords.contains(password); }}
如此,我们就使用了注解。

但是这注解本身是不起作用的,我们需要编写注解处理器解析注解,下面我们会说到如何使用,先看一下java中的标准注解。

3.标准注解

@Target 表示该注解可以用于什么地方。CONSTRUCTOR:构造器的声明 FIELD:域声明(包括enum实例) LOCAL_VARIABLE:局部变量声明

METHOD:方法声明 PACKAGE:包声明 PARAMETER:参数声明 TYPE:类、接口(包括注解类型)或enum声明

@Retention 表示需要在什么级别保存该注解信息。可选的RetentionPolicy参数包括:

SOURCE:注解将被编译器丢弃 CLASS:注解在class文件中可用,但会vm丢弃 RUNTIME:vm将在运行期也保留注解,因此可以通过反射机制读取注解的信息。

@Documented 将此注解包含在Javadoc中。

@Inherited 允许子类继承父类中的注解。

4.编写注解处理器

public class UseCaseTracker {    public static void trackUseCases(List
useCases, Class
cl) { for (Method m : cl.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
useCases = new ArrayList<>(); Collections.addAll(useCases, 47, 48, 49, 50); trackUseCases(useCases, PasswordUtils.class ); }}

getAnnoation()方法返回指定类型的注解对象,在这里就是UseCase。如果被注解的方法上没有该类型的注解,则返回null值。我们可以通过

调用id()和description()方法从返回的UseCase对象中提取元素的值。

转载地址:http://omjqi.baihongyu.com/

你可能感兴趣的文章
Kafka,它为什么速度会这么快?
查看>>
zookeeper安装启动的一些问题
查看>>
rabbitmq命令执行报错command not found
查看>>
rabbitmq基础知识介绍及总结
查看>>
StackOverFlow异常记录
查看>>
SpringMvc4.1:注解JsonView与泛型返回类
查看>>
SpringMVC+Mybatis+事务回滚+异常封装返回
查看>>
计算机网络实验报告(三):Cisco Packet Tracer 实验
查看>>
嵌入式系统基础学习笔记(九):基于 SPI 协议在 0.96 寸 OLED上【平滑显示汉字】及【温湿度数据采集显示】
查看>>
嵌入式系统基础学习笔记(十):
查看>>
网络通信编程学习笔记(七):Java与MQTT
查看>>
人工智能与机器学习学习笔记(二)
查看>>
Run Your Own Web Server Using Linux & Apache
查看>>
Java I/O
查看>>
SQL Server 2005 T-SQL Recipes: A Problem-Solution Approach
查看>>
Core Python Programming
查看>>
Creating Database Web Applications with PHP and ASP
查看>>
ASP.NET 2.0 Demystified
查看>>
Pattern-Oriented Software Architecture, Volume 2, Patterns for Concurrent and Networked Objects
查看>>
Pattern-Oriented Software Architecture, Volume 1: A System of Patterns
查看>>