📄 annotationreader.java
字号:
import java.lang.annotation.*;import java.lang.reflect.*;/** * Read annotation information from a class at runtime using the new * JDK1.5 reflection features **/public class AnnotationReader { AnnotatedClass ac; /** * Constructor **/ public AnnotationReader() { ac = new AnnotatedClass(); } /** * Print out runtime annotation information **/ public void printAnnotations() { Class c = ac.getClass(); Annotation[] annotations = c.getAnnotations(); int numberOfAnnotations = annotations.length; System.out.println("Class " + c.getName() + " has " + numberOfAnnotations + " annotations"); for (int i = 0 ; i < numberOfAnnotations; i++) { System.out.println("Annotation " + i + ": " + annotations[i] + ", type" + annotations[i].annotationType().getName()); } // use reflection method to get methods defined in this class. Method[] ms = c.getMethods(); // iterate the method list for(Method m: ms) { // use reflection method to get the declared annotations for this method. Annotation[] as = m.getDeclaredAnnotations(); for (Annotation a:as) { String annotationName = a.annotationType().getName(); System.out.println("for method:" + m.getName() + " has annotation:" + annotationName); if (a instanceof Accessor) { // get the attritues for this annotation. Accessor accAnnotation = (Accessor)a; System.out.println("Accessor's variable name is:" + accAnnotation.variableName()); } } } } /** * Main entry point * * @param args Command line arguments **/ public static void main(String[] args) { AnnotationReader ar = new AnnotationReader(); ar.printAnnotations(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -