java 之annotation

来源:互联网 发布:如何查看软件源码 编辑:程序博客网 时间:2024/05/22 00:05

1.  注解定义

package com.java.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface NotNull {String  value();}

2. 应用及注解解释


package com.java.annotation;import java.lang.reflect.Field;public class ApplyMyAnnotation {@NotNull(value = "the username is not null")public String  username;public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException { Class<?> cls = Class.forName("com.java.annotation.ApplyMyAnnotation"); Field  field=cls.getField("username"); boolean  flag=field.isAnnotationPresent(NotNull.class);    if(flag){                System.out.println("属性是annotation");                NotNull  notNull=field.getAnnotation(NotNull.class);                System.out.println(notNull.value());        }}}

3. 其他人的test  case


3.1  定义注解

package com.java.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface MyAnnotation1 {        String value();}

package com.java.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface MyAnnotation2 {        String description();        boolean isAnnotation();}

3.2 使用


package com.java.annotation;@MyAnnotation1("this is annotation1")public class AnnotationDemo {        @MyAnnotation2(description="this is annotation2",isAnnotation=true)        public void sayHello(){                System.out.println("hello world!");        }}


3.3  解释


package com.java.annotation;import java.lang.reflect.Method;import org.junit.Test;public class TestAnnotation {@Testpublic void test() throws NoSuchMethodException, SecurityException, ClassNotFoundException {       Class<?> cls = Class.forName("com.java.annotation.AnnotationDemo");           boolean flag = cls.isAnnotationPresent(MyAnnotation1.class);           if(flag){                   System.out.println("判断类是annotation");                   MyAnnotation1 annotation1 = cls.getAnnotation(MyAnnotation1.class);                   System.out.println(annotation1.value());           }                      Method method = cls.getMethod("sayHello");           flag = method.isAnnotationPresent(MyAnnotation2.class) ;           if(flag){                   System.out.println("判断方法也是annotation");                   MyAnnotation2 annotation2 = method.getAnnotation(MyAnnotation2.class);                   System.out.println(annotation2.description()+"/t"+annotation2.isAnnotation());           }   }}



0 0
原创粉丝点击