1 package org.picocontainer.gems.monitors; 2 3 import java.lang.reflect.Constructor ; 4 5 import org.picocontainer.defaults.DelegatingComponentMonitor; 6 import org.picocontainer.gems.monitors.prefuse.ComponentDependencyListener; 7 8 15 public class ComponentDependencyMonitor extends DelegatingComponentMonitor { 16 17 private final ComponentDependencyListener listener; 18 19 public ComponentDependencyMonitor(ComponentDependencyListener listener) { 20 this.listener = listener; 21 } 22 23 public void instantiated(Constructor constructor, Object instantiated, Object [] injected, long duration) { 24 Class componentType = instantiated.getClass(); 25 int count = injected.length; 26 27 if (count == 0) { 28 listener.addDependency(new Dependency(componentType, null)); 29 } 30 31 for (int i = 0; i < count; i++) { 32 Object dependent = injected[i]; 33 Dependency dependency = new Dependency(componentType, dependent.getClass()); 34 listener.addDependency(dependency); 35 } 36 } 37 38 44 public static class Dependency { 45 46 private Class componentType; 47 48 private Class dependencyType; 49 50 public Dependency(Class componentType, Class dependencyType) { 51 this.componentType = componentType; 52 this.dependencyType = dependencyType; 53 } 54 55 public boolean dependsOn(Class type) { 56 return (type == null) ? false : type.equals(dependencyType); 57 } 58 59 public boolean equals(Object other) { 60 if (other instanceof Dependency) { 61 Dependency otherDependency = (Dependency) other; 62 return areEqualOrNull(componentType, otherDependency.componentType) 63 && areEqualOrNull(dependencyType, otherDependency.dependencyType); 64 } 65 return false; 66 } 67 68 public Class getComponentType() { 69 return componentType; 70 } 71 72 public Class getDependencyType() { 73 return dependencyType; 74 } 75 76 public String toString() { 77 return componentType + " depends on " + dependencyType; 78 } 79 80 private static boolean areEqualOrNull(Class type, Class otherType) { 81 if (type != null) { 82 return type.equals(otherType); 83 } 84 return (type == null && otherType == null); 85 } 86 } 87 } 88 | Popular Tags |