Chinaunix首页 | 论坛 | 博客
  • 博客访问: 516882
  • 博文数量: 135
  • 博客积分: 3568
  • 博客等级: 中校
  • 技术积分: 1942
  • 用 户 组: 普通用户
  • 注册时间: 2006-10-19 17:52
文章分类

全部博文(135)

文章存档

2012年(29)

2011年(41)

2010年(26)

2009年(12)

2008年(9)

2007年(12)

2006年(6)

分类: Java

2009-07-29 18:52:17

J2SE 5 注解 - annotation
作用:为包、注解类、类、构造函数、字段、方法、参数和局部变量添加额外的辅助信息。所以,如果仅仅为
某个类增加了注解描述,而不启用相应注解的解析器,则不会对原有代码有任何影响。

 注意:注解类(比如下面我写的 @MeTest)在编译时是需要的,但是在运行时,可有可无。如果又,就能获得该注解配置的信息,如果没有,就获取不到。

JDK自带的注解类型
 
java.lang (JDK 1.5+)
@Deprecated
@Override
@SuppressWarnings
 
java.lang.annotation.* (JDK 1.5+)
@Documented
    将某个注解类型文档化,使之出现在Javadoc编译后的文档中。
@Inherited
    如果子类没有找到相关注解,则逆向从父类查询。(对接口使用相关注解无效)
@Retention
    RetentionPolicy.CLASS 默认值,即虽然注解信息会在编译在class里,但不会被VM加载。
    RetentionPolicy.RUNTIME 注解信息既会编译在class里,也会被VM加载。
    RetentionPolicy.SOURCE 注解信息在编译时会被舍弃,如@Override和@SuppressWarnings
@Target
    用于限定某个注解类型的使用范围(※ 数组类型)
    ElementType.ANNOTATION_TYPE
    ElementType.CONSTRUCTOR
    ElementType.FIELD
    ElementType.LOCAL_VARIABLE
    ElementType.METHOD
    ElementType.PACKAGE
    ElementType.PARAMETER
    ElementType.TYPE
            
JDK 1.5 中有以上几个注解类型。但是,java.lang.annotation包下的均为元注解类型,即它们可以自己
修饰自己。此外它们只能用于修饰其他的注解类型,而不能用于修饰变量,方法等。
而JDK 1.6 又追加了N多注解类型。

以下给个例子:

MyTest.java 自定义注解类型

package me.test.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

// Annotation不能显式够继承任何interface和Annotation
// (包括java.lang.annotation.Annotation)
// 所有Annotation类型都自动隐式继承自java.lang.annotation.Annotation
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTest {
    // 注解类型只有属性,但其声明方式有点像无参的函数
    // 注解类型可以没有属性,但若只有一个属性时,其名称必须为"value"
    int id();
    String name();
    String mail() default "XXX@XXX.XX"; // 有默认值
}


MyClass.java 使用自定义注解的一个类

package me.test.annotation;

@MyTest(id = 1, name = "[class:MyClass]")
public class MyClass {
    @MyTest(id = 2, name = "[property:a]", mail="btpka3@163.com")
    private int a;

    private int b;

    @MyTest(id = 3, name = "[method:add()]")
    public int add() {
        return a + b;
    }
    public int minus() {
        return a - b;
    }
}


AnnotationParser.java 使用解析类,解析相关类的注解信息。

package me.test.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class AnnotationParser {
    public static void main(String[] args) {
        Class<MyClass> c = MyClass.class;
        // 类
        if (c.isAnnotationPresent(MyTest.class)) {
            Annotation a = c.getAnnotation(MyTest.class);
            System.out.println("class[" + c.getName() + "] : " + a);
        }
        // 字段
        for (Field field : c.getDeclaredFields()) {
            if (field.isAnnotationPresent(MyTest.class)) {
                Annotation a = field.getAnnotation(MyTest.class);
                System.out.println("field[" + field.getName() + "] : " + a);
            }
        }
        // 方法
        for (Method method : c.getDeclaredMethods()) {
            if (method.isAnnotationPresent(MyTest.class)) {
                Annotation a = method.getAnnotation(MyTest.class);
                System.out.println(method.getName() + ":" + "field["
                        + method.getName() + "] : " + a);
            }
        }
    }
   
/* 运行结果
class[me.test.annotation.MyClass] : @me.test.annotation.MyTest(mail=XXX@XXX.XX, id=1, name=[class:MyClass])
field[a] : @me.test.annotation.MyTest(mail=btpka3@163.com, id=2, name=[property:a])
add:field[add] : @me.test.annotation.MyTest(mail=XXX@XXX.XX, id=3, name=[method:add()])
 */
    

}

阅读(1070) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~