自定义Annotation

来源:互联网 发布:windows 8的记事本 编辑:程序博客网 时间:2024/05/16 02:31

1.编写注解类的规则

       (1)Annotation类型定义为@interface

                 所有的Annotation会自动集成java.lang.Annotation这一接口,并且不能再去继承别的类或者接口

       (2)Annotation成员只有public或者默认这两个修饰符

       (3)参数成员只能用8种原生数据类型和String、Enum、Class、Annotation等数据类型,以及这些类型的数组

       (4)要获取注解信息,只能通过反射


2.指定注解类的目标、生命周期、是否子类继承父类注解

       (1)指定注解的目标

                 @Target表示注解的目标,可以选取的值有以下几种

ElemenetType.CONSTRUCTOR     构造器声明ElemenetType.FIELD           属性声明(包括 enum 实例) ElemenetType.LOCAL_VARIABLE  局部变量声明 ElemenetType.METHOD          方法声明 ElemenetType.PACKAGE         包声明 ElemenetType.PARAMETER       参数声明 ElemenetType.TYPE            类,接口(包括注解类型)或enum声明

       (2)指定注解的生命周期

                 @Retention表示注解的生命周期,可选的值有以下几种

RetentionPolicy.SOURCE        注解将被编译器丢弃 RetentionPolicy.CLASS         注解在class文件中可用,但会被VM丢弃 RetentionPolicy.RUNTIME       VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息

       (3)@Document表示将这个注解包含在javadoc中

       (4)@Inherited表示允许子类继承父类中的注解

2.注解使用

       (1)类注解

@Retention (RetentionPolicy.RUNTIME)@Target (ElementType.TYPE)               //表示类注解public @interface MyClassAnnotation {String uri();String desc();}

       (2)默认构造方法注解
@Retention (RetentionPolicy.RUNTIME)@Target (ElementType.CONSTRUCTOR)        //表示构造方法注解public @interface MyConstructorAnnotation {String uri();String desc();}

       (3)方法注解

@Retention (RetentionPolicy.RUNTIME)@Target (ElementType.METHOD)public @interface MyMethodAnnotation {String uri();String desc();}

       (4)属性注解

@Retention (RetentionPolicy.RUNTIME)@Target (ElementType.FIELD)public @interface MyFieldAnnotation {String uri();String desc();}

       (5)测试

@MyClassAnnotation (uri="mysample",desc="the class annotation")public class Zhujie {@MyFieldAnnotation (uri="mysample",desc="the field annotation")private String id;@MyConstructorAnnotation (uri="mysample",desc="the constructor annotation")public Zhujie() {}@MyMethodAnnotation (uri="mysample",desc="the method annotation")public String getId(){return id;}}

@org.junit.Testpublic void test() throws SecurityException, NoSuchMethodException, NoSuchFieldException {Class<Zhujie> clazz = Zhujie.class;MyClassAnnotation classAnnotation = clazz.getAnnotation(MyClassAnnotation.class);System.out.println("class's uri = "+classAnnotation.uri()+"; desc = "+classAnnotation.desc());Constructor<Zhujie> constructor = clazz.getConstructor();MyConstructorAnnotation constructorAnnotation = constructor.getAnnotation(MyConstructorAnnotation.class);System.out.println("constructor's uri = "+constructorAnnotation.uri()+"; desc = "+constructorAnnotation.desc());Method method = clazz.getMethod("getId");MyMethodAnnotation methodAnnotation = method.getAnnotation(MyMethodAnnotation.class);System.out.println("method's uri = "+methodAnnotation.uri()+"; desc = "+methodAnnotation.desc());Field field = clazz.getDeclaredField("id");MyFieldAnnotation fieldAnnotation = field.getAnnotation(MyFieldAnnotation.class);System.out.println("field's uri = "+fieldAnnotation.uri()+"; desc = "+fieldAnnotation.desc());}
               结果为

class's uri = mysample; desc = the class annotationconstructor's uri = mysample; desc = the constructor annotationmethod's uri = mysample; desc = the method annotationfield's uri = mysample; desc = the field annotation

0 0
原创粉丝点击