zoukankan      html  css  js  c++  java
  • [JAVA] 注解学习@interface

    一直都看框架级的代码中都是各种annotation,一起来看看到底怎么弄的

    例子1:直接定义一个annotation,并使用之:

    package com.base.annotation.example;
    
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    
    /**
     * Created by guangyi on 15/12/8.
     */
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyTarget {
        String value();
    }
    
    
    package com.base.annotation.example;
    
    import java.lang.reflect.Method;
    
    /**
     * Created by guangyi on 15/12/8.
     */
    public class MyTargetTest {
        @MyTarget("test")
        public void doSomething() {
            System.out.println("hello world");
        }
        public static void main(String[] args) throws  NoSuchMethodException{
            Method method = MyTargetTest.class.getMethod("doSomething", null);
            if(method.isAnnotationPresent(MyTarget.class)) {
                System.out.println(method.getAnnotation(MyTarget.class)); // 打印@com.base.annotation.example.MyTarget(value=test)
            }
        }
    }

    例子2:复杂类型的,各种注解类型

    package com.base.annotation.example.example2;
    
    /**
     * Created by guangyi on 15/12/8.
     */
    
    public class EnumTest {
        public static enum TrafficLamp {
            RED, GREEN, YELLOW
        }
    }
    
    package com.base.annotation.example.example2;
    
    import com.base.annotation.example.MyTarget;
    
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    
    /**
     * Created by guangyi on 15/12/8.
     */
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyAnnotation {
        String hello() default "hello";
        String world();
        int[] array() default {1, 2, 3, 4};
        EnumTest.TrafficLamp lamp();
        MyTarget myAnnotation() default @MyTarget("ddd");
        Class style() default String.class;
    }
    
    package com.base.annotation.example.example2;
    
    import com.base.annotation.example.MyTarget;
    
    import java.lang.annotation.Annotation;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    /**
     * Created by guangyi on 15/12/8.
     */
    @MyAnnotation(hello = "beijing", world = "shanghai", array = {5, 6, 7, 8}, lamp = EnumTest.TrafficLamp.GREEN, style = int.class)
    public class MyTest {
        @MyAnnotation(myAnnotation = @MyTarget("baby"), world = "shanghai", array = {10, 11, 12}, lamp = EnumTest.TrafficLamp.YELLOW)
        @Deprecated
        @SuppressWarnings("")
        public void output() {
            System.out.println("output something!");
        }
    
        public static void main(String[] args) throws  NoSuchMethodException, InvocationTargetException, IllegalAccessException{
            MyTest myTest = new MyTest();
            Class<MyTest> c = MyTest.class;
            Method method = c.getMethod("output", new Class[]{});
            if(MyTest.class.isAnnotationPresent(MyAnnotation.class)) {
                System.out.println("have annotation");
            }
            if(method.isAnnotationPresent(MyAnnotation.class)) {
                method.invoke(myTest, null);
                MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
                String hello = myAnnotation.hello();
                String world = myAnnotation.world();
                System.out.println(hello + ", " + world);
                System.out.println(myAnnotation.array().length);
                System.out.println(myAnnotation.myAnnotation().value());
                System.out.println(myAnnotation.style());
                System.out.println(myAnnotation.lamp());
            }
            Annotation[] annotations = method.getAnnotations();
            for (Annotation annotation : annotations) {
                System.out.println(annotation.annotationType().getName());
            }
        }
    }

    示例3:看原始注解的定义,可以看到也是使用注解,另外,使用value作为注解属性名,则可以省略注解名字不写,例如:@Rentention(RententionPolicy.RUNTIME):

    package java.lang;
    
    import java.lang.annotation.*;
    import static java.lang.annotation.ElementType.*;
    
    @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface SuppressWarnings {
        String[] value();
    }
    
    
    package java.lang;
    
    import java.lang.annotation.*;
    import static java.lang.annotation.ElementType.*;
    
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
    public @interface Deprecated {
    }
    
    
    package java.lang.annotation;
    
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Retention {
        RetentionPolicy value();
    }
    
    package java.lang.annotation;
    
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Target {
        ElementType[] value();
    }

    参考:http://blog.csdn.net/liuwenbo0920/article/details/7290586/

  • 相关阅读:
    Qt添加程序图标
    QTcpSocket+QTcpServer+QWebEngineView打造简易聊天室
    Qt聊天室
    QThread+QMutex多线程编程
    Qt设置窗体背景渐变
    Could not resolve host: github.com
    git clone某一分支上的内容
    git基本操作
    C++中的static关键字的总结
    C/C++中static关键字作用总结
  • 原文地址:https://www.cnblogs.com/garinzhang/p/annotation_interface.html
Copyright © 2011-2022 走看看