1 3 package jodd.introspector; 4 5 import java.util.WeakHashMap ; 6 import java.util.Iterator ; 7 import java.util.Map ; 8 9 18 public class Introspector { 19 20 public static Introspector DEFAULT = new Introspector(); 21 22 protected Map cache = new WeakHashMap (); 23 24 27 public ClassDescriptor lookup(Class type) { 28 ClassDescriptor cd = (ClassDescriptor) cache.get(type); 29 if (cd != null) { 30 cd.increaseUsage(); 31 return cd; 32 } 33 cd = describeClass(type); 34 cache.put(type, cd); 35 return cd; 36 } 37 38 43 public ClassDescriptor search(Class type) { 44 if (this.getClass() == DEFAULT.getClass()) { 45 return lookup(type); 46 } 47 ClassDescriptor cd = (ClassDescriptor) cache.get(type); 48 if (cd == null) { 49 cd = DEFAULT.lookup(type); 50 } 51 return cd; 52 } 53 54 58 public ClassDescriptor register(Class type) { 59 ClassDescriptor cd = describeClass(type); 60 cache.put(type, cd); 61 return cd; 62 } 63 64 67 protected ClassDescriptor describeClass(Class type) { 68 return new ClassDescriptor(type); 69 } 70 71 74 public void reset() { 75 cache = new WeakHashMap (); 76 } 77 78 81 public String getStatistics() { 82 StringBuffer sbuffer = new StringBuffer (); 83 sbuffer.append("Total classes: ").append(cache.size()).append('\n'); 84 Iterator i = cache.keySet().iterator(); 85 while (i.hasNext()) { 86 Class clazz = (Class ) i.next(); 87 ClassDescriptor bcd = (ClassDescriptor) cache.get(clazz); 88 sbuffer.append('\t').append(clazz.getName()).append(" ("); 89 sbuffer.append(bcd.getUsage()).append(" uses)").append('\n'); 90 } 91 return sbuffer.toString(); 92 } 93 94 } 95 | Popular Tags |