//============================================================================== // * Letsch Informatik * www.LetsInfo.ch CH-8636 Wald // Beratung, Ausbildung und Realisation in Software-Engineering //============================================================================== // Project : Master of Advanced Studies in Software-Engineering / 2008 // Modul : Java - Advanced Concepts // Title : Übung 2 / "Annotations und Reflection" // Author : Thomas Letsch // Tab-Width : 2 /*///=========================================================================== * Description: Einsatz von Annotation und Reflection am Beispiel von Traceabilty. * History : 17.03.07: Initial Version. * : 18.01.08: bin-Dir, Kosmetik. * : 21.01.08: filenames sortiert (es ist keine Ordnung spezifiziert * für File.list()). * CVS : $Revision: 1.6 $ $Date: 2008/01/24 13:51:17 $ /*///=========================================================================== // 1 2 3 4 5 6 7 8 //345678901234567890123456789012345678901234567890123456789012345678901234567890 //============================================================================== import static java.lang.System.out; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; // Annotations: @Retention(RetentionPolicy.RUNTIME) @interface Tracing { Ident ident(); Ident[] refs(); String contact() default "-"; } @interface Ident { IdentType type(); int nr(); } enum IdentType {UC, DE, IM} // Test-Sceanario: @Tracing(ident=@Ident(type=IdentType.DE, nr=1), refs={@Ident(type=IdentType.UC, nr=1), @Ident(type=IdentType.UC, nr=2)}) class DesignClass1 { } @Tracing(ident=@Ident(type=IdentType.DE, nr=2), refs={@Ident(type=IdentType.UC, nr=2)}, contact="John Smith") class DesignClass2 { } @Tracing(ident=@Ident(type=IdentType.DE, nr=3), refs={@Ident(type=IdentType.UC, nr=1)}) class DesignClass3 { } @Tracing(ident=@Ident(type=IdentType.IM, nr=1), refs={@Ident(type=IdentType.DE, nr=1)}) class ImplClass1 { } @Tracing(ident=@Ident(type=IdentType.IM, nr=2), refs={@Ident(type=IdentType.DE, nr=1), @Ident(type=IdentType.DE, nr=2)}) class ImplClass2 { } @Tracing(ident=@Ident(type=IdentType.IM, nr=3), refs={@Ident(type=IdentType.DE, nr=2)}) class ImplClass3 { } @Tracing(ident=@Ident(type=IdentType.IM, nr=9), refs={@Ident(type=IdentType.IM, nr=3)}, contact="Tom Miller") class ImplClass9 { } class TracingTest { private static void printAnnotations(List> pTracingTypes) { for (Class actualClass: pTracingTypes) { print("", actualClass); } } private static void print(String pPrefix, Class pActualClass) { out.print(pPrefix); out.printf("%-13s: ", pActualClass.getName()); Tracing t = pActualClass.getAnnotation(Tracing.class); out.printf("Ident: %s:%-4d: ", t.ident().type(), t.ident().nr()); String allIdentsStr = ""; for (Ident ident: t.refs()) { allIdentsStr = allIdentsStr + getIdentString(ident)+","; } allIdentsStr = allIdentsStr.substring(0, allIdentsStr.length()-1); out.printf("Refs: %-12s: ", allIdentsStr); out.println("Contact: " + t.contact()); } private static String getIdentString(Ident pIdent) { return String.format("%s:%d", pIdent.type(), pIdent.nr()); } private static void searchFor(List> pTracingTypes, IdentType pIT, int pNr, String pPrefix) { for (Class actualClass: pTracingTypes) { Tracing t = (Tracing) actualClass.getAnnotation(Tracing.class); for (Ident id: t.refs()) { if ((id.type() == pIT) && (id.nr() == pNr)) { print(pPrefix, actualClass); searchFor(pTracingTypes, t.ident().type(), t.ident().nr(), pPrefix+" "); } } } } public static void main(String[] pArgs) { if (pArgs.length == 0) { System.err.println("ERROR: no Argument ! (e.g. \"UC:2\")"); System.exit(1); } File binDir = new File("bin"); // Class-Directory List> tracingTypes = new ArrayList>(); out.print("\nTesting all Files in the bin-Directory: "); String[] filenames = binDir.list(); Arrays.sort(filenames); for(String filename: filenames) { out.print("\nFile: "+filename); try { if ( ! filename.matches(".*\\.class$")) // ".class" at End of Filename ? continue; // this is not a Classfile filename = filename.substring(0, filename.lastIndexOf(".class")); out.print(" - loading..."); Class c = Class.forName(filename); Tracing t = (Tracing) c.getAnnotation(Tracing.class); if (t != null) { out.print(" found Tracing-Annotation!"); tracingTypes.add(c); } } catch(ClassNotFoundException e){ e.printStackTrace(); } } out.println("\n\nPrinting all Tracing-Annotations:"); printAnnotations(tracingTypes); out.println("\n\nSearching for: \""+pArgs[0]+"\""); String[] x = pArgs[0].split(":"); String type = x[0]; IdentType identType = IdentType.valueOf(type); int nr= Integer.valueOf(x[1]); searchFor(tracingTypes, identType, nr, ""); } // End of main() } // End of class TracingTest /* Session-Log: $ java -cp bin TracingTest UC:2 Testing all Files in the bin-Directory: File: DesignClass1.class - loading... found Tracing-Annotation! File: DesignClass2.class - loading... found Tracing-Annotation! File: DesignClass3.class - loading... found Tracing-Annotation! File: Ident.class - loading... File: IdentType.class - loading... File: ImplClass1.class - loading... found Tracing-Annotation! File: ImplClass2.class - loading... found Tracing-Annotation! File: ImplClass3.class - loading... found Tracing-Annotation! File: ImplClass9.class - loading... found Tracing-Annotation! File: Tracing.class - loading... File: TracingTest.class - loading... Printing all Tracing-Annotations: DesignClass1 : Ident: DE:1 : Refs: UC:1,UC:2 : Contact: - DesignClass2 : Ident: DE:2 : Refs: UC:2 : Contact: John Smith DesignClass3 : Ident: DE:3 : Refs: UC:1 : Contact: - ImplClass1 : Ident: IM:1 : Refs: DE:1 : Contact: - ImplClass2 : Ident: IM:2 : Refs: DE:1,DE:2 : Contact: - ImplClass3 : Ident: IM:3 : Refs: DE:2 : Contact: - ImplClass9 : Ident: IM:9 : Refs: IM:3 : Contact: Tom Miller Searching for: "UC:2" DesignClass1 : Ident: DE:1 : Refs: UC:1,UC:2 : Contact: - ImplClass1 : Ident: IM:1 : Refs: DE:1 : Contact: - ImplClass2 : Ident: IM:2 : Refs: DE:1,DE:2 : Contact: - DesignClass2 : Ident: DE:2 : Refs: UC:2 : Contact: John Smith ImplClass2 : Ident: IM:2 : Refs: DE:1,DE:2 : Contact: - ImplClass3 : Ident: IM:3 : Refs: DE:2 : Contact: - ImplClass9 : Ident: IM:9 : Refs: IM:3 : Contact: Tom Miller */