码迷,mamicode.com
首页 > 编程语言 > 详细

java annotation

时间:2018-02-17 11:07:38      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:comment   类继承   out   tor   log   忽略   多次   网络   hello   

1 什么是annotation

annotation是java编译器支持的一种标记,它可以简化我们的代码,使得我们的代码更加方便被理解。

2 元annotation

用来编写其它注解的注解。

@Retention

保留期,有三种保留期。

RetentionPolicy.SOURCE

这样的注解只在源码阶段保留,编译器在编译的时候会忽略掉它们。

RetentionPolicy.CLASS

编译的时候会用到它们,但是不会加载到虚拟机中。

RetentionPolicy.RUNTIME

保留到程序运行的时候,并且加载到虚拟机中,在程序运行的时候可以通过反射获取它们。

@Documented

将注解中的元素加载到javadoc中去。

@Target

指的是注解可以生效的地方,有8种。ElementType.ANNOTATION_TYPE、ElementType.CONSTRUCTOR、ElementType.FIELD、ElementType.LOCAL_VARIABLE、

ElementType.METHOD、ElementType.PACKAGE、ElementType.PARAMETER、ElemenType.TYPE。

@Inherited

指的是子类继承超类的注解。

@Repeatable

可以多次使用。

3 注解定义的一般形式

@Target(ElementType.TYPE)

@Retention(RetentionPolicy.RUNTIME)

public @interface TestAnnotation {

    int id();

    String msg();

}

4 注解的提取和使用

4.1 使用的技术

反射。

4.2 判断是否使用了注解

public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {}

4.3 获取指定的注解

public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {}

4.4 获取所有的注解

public Annotation[] getAnnotations() {}

4.5 使用案例

如下例子所示,我们可以获取类上的注解、方法上的注解以及类的成员变量上的注解(该例子来自于网络)。

@TestAnnotation(msg="hello")
public class Test {

    @Check(value="hi")
    int a;


    @Perform
    public void testMethod(){}


    @SuppressWarnings("deprecation")
    public void test1(){
        Hero hero = new Hero();
        hero.say();
        hero.speak();
    }


    public static void main(String[] args) {

        boolean hasAnnotation = Test.class.isAnnotationPresent(TestAnnotation.class);

        if ( hasAnnotation ) {
            TestAnnotation testAnnotation = Test.class.getAnnotation(TestAnnotation.class);
            //获取类的注解
            System.out.println("id:"+testAnnotation.id());
            System.out.println("msg:"+testAnnotation.msg());
        }


        try {
            Field a = Test.class.getDeclaredField("a");
            a.setAccessible(true);
            //获取一个成员变量上的注解
            Check check = a.getAnnotation(Check.class);

            if ( check != null ) {
                System.out.println("check value:"+check.value());
            }

            Method testMethod = Test.class.getDeclaredMethod("testMethod");

            if ( testMethod != null ) {
                // 获取方法中的注解
                Annotation[] ans = testMethod.getAnnotations();
                for( int i = 0;i < ans.length;i++) {
                    System.out.println("method testMethod annotation:"+ans[i].annotationType().getSimpleName());
                }
            }
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println(e.getMessage());
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println(e.getMessage());
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println(e.getMessage());
        }



    }

 

java annotation

标签:comment   类继承   out   tor   log   忽略   多次   网络   hello   

原文地址:https://www.cnblogs.com/hustdc/p/8451439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!