Java 注解

来源:互联网 发布:湖南网络工程学院宿舍 编辑:程序博客网 时间:2024/06/07 09:31

注解

注解为我们在代码中添加信息提供了一种形式化的方法,使我们在稍后某个时刻非常方便地使用这些数据。
注解可以提供用来完整地描述程序所需的信息。

基本语法

Java内置的三种标准注解(定义在java.lang):

@Override,表示当前的方法定义将覆盖超类中的方法
@Deprecated(不赞成),使用了该注解的元素,编译器会发出警告信息
@SuppressWarnings,关闭不当的编译器警告信息。

Java的四种元注解(负责注解其它的注解):

(1)@Target:表示该注解可以用于什么地方。ElementType参数包括:
CONSTRUCTOR(构造器声明),FIELD(域声明),LOCAL_VARIABLE(局部变量声明),METHOD(方法声明),PACKAGE(包声明),PARAMETER(参数声明),TYPE(类、接口、enum声明)
(2)@Retention:表示需要在什么级别保存该注解信息。RetentionPolicy参数包括:
SOURCE(注解被编译器丢弃),CLASS(注解在class文件中可用。被VM丢弃),RUNTIME(VM将在运行期也保留注解,可以通过反射机制读取注解信息)
(3)@Documented:包含在Javadoc中
(4)@Inherited:允许子类继承父类的注解

注解元素:所有基本类型、String、Class、enum、Annotation、以上类型数组

使用注解首先需要定义注解:

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

使用注解:

import java.util.List;import annotations.UseCase;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<String>prePasswords,String password){        return !prePasswords.contains(password);    }}

编写简单的注解处理器

下面是利用反射机制查找注解的标记:

import java.lang.reflect.Method;import java.util.ArrayList;import java.util.Collections;import java.util.List;import annotations.UseCase;public class UseCaseTracker {    public static void    trackUseCases(List<Integer>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<Integer>useCases=new  ArrayList<>();        Collections.addAll(useCases, 47,48,49,50);        trackUseCases(useCases, PasswordUtils.class);    }}

Output:

Found Use Case:47 Passwords must contain at least one numericFound Use Case:48 no descriptionFound Use Case:49 New passwords can't equal previously used onesWarning:Missing use case-50
原创粉丝点击