免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1648 | 回复: 0
打印 上一主题 下一主题

Sample - 注解 - Annotation [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-29 18:52 |只看该作者 |倒序浏览
J2SE 5 注解 - annotation
作用:为包、注解类、类、构造函数、字段、方法、参数和局部变量添加额外的辅助信息。所以,如果仅仅为
某个类增加了注解描述,而不启用相应注解的解析器,则不会对原有代码有任何影响。

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) {
        ClassMyClass> 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()])
*/   
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/25218/showart_2012394.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP