import java.lang.annotation.Annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Target; import java.lang.reflect.AnnotatedElement; import jp.gr.java_conf.skrb.annotation.Log; import jp.gr.java_conf.skrb.annotation.LogLevel; public class LogAnnotationReader { public LogAnnotationReader() {} public void readAnnotation(String classname) { try { Class cls = Class.forName(classname); System.out.println("Checking " + cls + " ...\n"); 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 "); readAnnotation(element); } } private void readAnnotation(AnnotatedElement element) { Log log = element.getAnnotation(Log.class); if (log != null) { LogLevel level = log.value(); System.out.println("annotated.\n value: " + level); } 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 LogAnnotationReader [classname] [classname] ..."); } LogAnnotationReader reader = new LogAnnotationReader(); for (String classname : args) { reader.readAnnotation(classname); } } }