1 10 11 package org.picocontainer.gems.monitors; 12 13 import org.picocontainer.ComponentMonitor; 14 import org.picocontainer.defaults.DelegatingComponentMonitor; 15 16 import java.lang.reflect.Constructor ; 17 import java.util.*; 18 19 public class DotDependencyGraphComponentMonitor extends DelegatingComponentMonitor implements ComponentMonitor { 20 21 ArrayList allInstantiated = new ArrayList(); 22 23 public DotDependencyGraphComponentMonitor(ComponentMonitor delegate) { 24 super(delegate); 25 } 26 27 public DotDependencyGraphComponentMonitor() { 28 } 29 30 public void instantiated(Constructor constructor, Object instantiated, Object [] injected, long duration) { 31 32 this.allInstantiated.add(new Instantiation(constructor, instantiated, injected, duration)); 33 34 super.instantiated(constructor, instantiated, injected, duration); 35 } 36 37 38 public String getClassDependencyGraph() { 39 40 HashSet lines = new HashSet(); 41 42 for (int i = 0; i < allInstantiated.size(); i++) { 43 Instantiation instantiation = (Instantiation) allInstantiated.get(i); 44 for (int j = 0; j < instantiation.getInjected().length; j++) { 45 Object instantiated = instantiation.getInstantiated(); 46 Object injected = instantiation.getInjected()[j]; 47 lines.add(" '" + instantiated.getClass().getName() + "' -> '" + injected.getClass().getName() + "';\n"); 48 } 49 } 50 51 return sortLines(lines); 52 } 53 54 private String sortLines(HashSet lines) { 55 ArrayList list = new ArrayList(lines); 56 Collections.sort(list); 57 58 String dependencies = ""; 59 for (Iterator iterator = list.iterator(); iterator.hasNext();) { 60 String dep = (String ) iterator.next(); 61 dependencies = dependencies + dep; 62 } 63 return dependencies.replaceAll("'","\""); 64 } 65 66 public String getInterfaceDependencyGraph() { 67 HashSet lines = new HashSet(); 68 69 for (int i = 0; i < allInstantiated.size(); i++) { 70 Instantiation instantiation = (Instantiation) allInstantiated.get(i); 71 for (int j = 0; j < instantiation.getInjected().length; j++) { 72 Object injected = instantiation.getInjected()[j]; 73 Class injectedType = instantiation.getConstructor().getParameterTypes()[j]; 74 Object instantiated = instantiation.getInstantiated(); 75 if (injected.getClass() != injectedType) { 76 lines.add(" '" + instantiated.getClass().getName() + "' -> '" + injectedType.getName() + "' [style=dotted,label='needs'];\n"); 77 lines.add(" '" + injected.getClass().getName() + "' -> '" + injectedType.getName() + "' [style=dotted, color=red,label='isA'];\n"); 78 lines.add(" '" + injectedType.getName() + "' [shape=box, label=" + printClassName(injectedType) + "];\n"); 79 } else { 80 lines.add(" '" + instantiated.getClass().getName() + "' -> '" + injected.getClass().getName() + "' [label='needs'];\n"); 81 } 82 lines.add(" '" + instantiated.getClass().getName() + "' [label=" + printClassName(instantiated.getClass()) + "];\n"); 83 84 } 85 } 86 87 return sortLines(lines); 88 } 89 90 private String printClassName(Class clazz) { 91 String className = clazz.getName(); 92 return "'" + className.substring(className.lastIndexOf(".")+1) + "\\n" + clazz.getPackage().getName() + "'"; 93 94 } 95 96 private static class Instantiation { 97 Constructor constructor; 98 Object instantiated; 99 Object [] injected; 100 long duration; 101 public Instantiation(Constructor constructor, Object instantiated, Object [] injected, long duration) { 102 this.constructor = constructor; 103 this.instantiated = instantiated; 104 this.injected = injected; 105 this.duration = duration; 106 } 107 108 public Constructor getConstructor() { 109 return constructor; 110 } 111 112 public Object getInstantiated() { 113 return instantiated; 114 } 115 public Object [] getInjected() { 116 return injected; 117 } 118 } 119 } 120 | Popular Tags |