view plaincopy to clipboardprint? package chb.test.annotation; @MyAnnotation1("this is annotation1") public class AnnotationDemo { @MyAnnotation2(description="this is annotation2",isAnnotation=true) public void sayHello(){ System.out.println("hello world!"); } } view plaincopy to clipboardprint? package chb.test.annotation; @MyAnnotation1("this is annotation1") public class AnnotationDemo { @MyAnnotation2(description="this is annotation2",isAnnotation=true) public void sayHello(){ System.out.println("hello world!"); } } package chb.test.annotation; @MyAnnotation1("this is annotation1") public class AnnotationDemo { @MyAnnotation2(description="this is annotation2",isAnnotation=true) public void sayHello(){ System.out.println("hello world!"); } }
4.Annotation测试说明类
view plaincopy to clipboardprint? package chb.test.annotation; import java.lang.reflect.Method; import org.junit.Test; public class TestAnnotation { @Test public void test() throws ClassNotFoundException, SecurityException, NoSuchMethodException{ Class<?> cls = Class.forName("chb.test.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()); }