import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; public class AnnotationsReader { public AnnotationsReader() {} public void readAnnotation(String classname) { try { Class cls = Class.forName(classname); System.out.print(cls.getName() + " is "); if (cls.isAnnotation()) { System.out.println("Annotation.\n"); } else { System.out.println("not Annotation.\n"); } System.out.print(cls.getName() + " is "); readAnnotations(cls); readAnnotation(cls.getDeclaredFields(), "Field"); readAnnotation(cls.getDeclaredConstructors(), "Constructor"); readAnnotation(cls.getDeclaredMethods(), "Method"); } catch (ClassNotFoundException ex) { System.err.println(classname + " が見つかりません"); } } public void readAnnotation(AnnotatedElement[] elements, String message) { for (AnnotatedElement element : elements) { System.out.print(message + "[" + element +"] is "); readAnnotations(element); } } private void readAnnotations(AnnotatedElement element) { Annotation[] annotations = element.getAnnotations(); if (annotations.length != 0) { System.out.print("Annotated.\nAnnotation:\n"); for (Annotation annotation : annotations) { System.out.println(" " + annotation); } System.out.println(); } else { System.out.println("not Annotated.\n"); } } public static void main(String[] args) { if (args.length == 0) { System.err.println("Usage:"); System.err.println(" java AnnotationsReader [classname] [classname] ..."); } AnnotationsReader reader = new AnnotationsReader(); for (String classname : args) { reader.readAnnotation(classname); } } }