[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/