• JAVA进阶-注解


    注解元数据分为4部分分别为Target,Documented,Inherited,Retention:


    Target>指定被注解的注解仅仅能使用在某个类型上;ElementType指定其类型:能够为方法
    字段,类,返回值等等;

    声明:
    /**
     * 	@author Lean  @date:2014-10-13  
     */
    @Target(ElementType.METHOD)
    public @interface WorkInProgress {}

    应用:
    /**
     * 	@author Lean  @date:2014-10-13  
     */
    public class AnnotationSample {
    	
    	//当在字段中使用时:The annotation @WorkInProgress is disallowed for this location
    	//@WorkInProgress
    	private int age;
    	
    	@WorkInProgress
    	public static boolean doSomeThing() {
    		// TODO Auto-generated method stub
    		return false;
    	}
    	
    }

    Retention>设置注解可见性;使用到RetentionPolicy枚举
    RetentionPolicy.SOURCE>>编译器可见,但对.class文件和执行时不可见;
    RetentionPolicy.CLASS>>默认工具可见,对.class文件可见,但执行不可见;
    RetentionPolicy.RUNTIME>>执行时可见;不会被.class文件所知,在执行时告诉JVM的值;
    下面样例为执行时内省检查,当一个元注解须要多个限定值的时,必须使用{}和逗号隔开,
    如@Target({ElementType.METHOD,ElementType.TYPE})

    /**
     * 	@author Lean  @date:2014-10-13  
     */
    @WorkInProgress
    public class AnnotationSample {
    	
    	private int age;
    	
    	@WorkInProgress
    	public static boolean doSomeThing() {
    		// TODO Auto-generated method stub
    		return false;
    	}
    	
    	public static void main(String[] args) {
    		AnnotationSample obj=new AnnotationSample();
    		Class clazz=obj.getClass();
    		WorkInProgress progress=(WorkInProgress) clazz.getAnnotation(WorkInProgress.class);
    		System.out.println(clazz.getName());
    		if (clazz.isAnnotationPresent(WorkInProgress.class)) {
    			System.out.println("class Annotationed WorkInProgress!");
    		}
    		Method[] methods=clazz.getMethods();
    		for (Method method : methods) {
    			if (method.isAnnotationPresent(WorkInProgress.class)) {
    				System.out.println("method Annotationed WorkInProgress!");
    			}	
    		}
    	}
    	
    }
    
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD,ElementType.TYPE})
    @interface WorkInProgress {}


    Documented>为加入注解的类书写文档,编译执行后执行javadoc的dos命令.就可以在该命令行

    位置看到所生成的文档.

    Inherited>当对某个类进行注解的时候,希望对继承他的子类也进行注解.默认情况下
    没有使用该@Inherited注解方式,系统会觉得子类不须要继承该功能,例如以下:

    /**
     * 	@author Lean  @date:2014-10-13  
     */
    @WorkInProgress
    public class AnnotationSample {
    	
    	public static void main(String[] args) throws IllegalAccessException {
    		AnnotationSample obj=new AnnotationSample();
    		Class clazz=obj.getClass();
    		if (clazz.isAnnotationPresent(WorkInProgress.class)) {
    			System.out.println("class Annotationed WorkInProgress!");
    		}
    		Class childClass=AnnotationChildClass.class;
    		if (childClass.isAnnotationPresent(WorkInProgress.class)) {
    			System.out.println("child class Annotationed WorkInProgress!");
    		}
    		
    	}
    	
    }
    
    
    @Inherited
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD,ElementType.TYPE})
    @interface WorkInProgress {}
    
    
    class AnnotationChildClass extends AnnotationSample{
    	
    }

  • 相关阅读:
    前端安全-XSS攻击
    leetcode-0003 无重复字符的最长子串
    leetcode-0002 两数相加
    leetcode-0001 两数之和
    数据结构篇-数组(TypeScript版+Java版)
    前端性能优化(一)-- 文件的压缩与合并
    《深入浅出RxJS》读书笔记
    python工具函数
    [其他]Ubuntu安装genymotion后unable to load VirtualBox engine
    [linux]CentOS无法使用epel源
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4337322.html
Copyright © 2020-2023  润新知