java(21) - 注解详解(二)

2014-11-24 02:55:46 · 作者: · 浏览: 1
s; //获取指定的方法 Method method = c.getMethod("output",new Class[]{}); //如果指定类型的注释存在于此元素上,则返回 true,否则返回 false if(method.isAnnotationPresent(MyAnnotation.class)){ //执行output方法 method.invoke(new MyTest(),new Object[]{}); MyAnnotation ma = method.getAnnotation(MyAnnotation.class); //返回字符串 String str = ma.hello(); String str2 = ma.world(); System.out.println(str+" , "+str2); } //获得方法上存在的注解类型 Annotation[] annotations = method.getAnnotations(); for(Annotation annotation : annotations){ System.out.println(annotation.annotationType().getName()); } } }


打印:

output !
hello , java
java.lang.Deprecated
lzr.annotation.MyAnnotation