1 8 package org.codehaus.aspectwerkz.reflect; 9 10 import gnu.trove.TIntObjectHashMap; 11 12 import java.lang.ref.WeakReference ; 13 import java.util.Map ; 14 import java.util.WeakHashMap ; 15 16 21 public class ClassInfoRepository { 22 25 private static final TIntObjectHashMap s_repositories = new TIntObjectHashMap(); 26 27 30 private final Map m_repository = new WeakHashMap (); 31 32 35 private transient final WeakReference m_loaderRef; 36 37 42 private ClassInfoRepository(final ClassLoader loader) { 43 m_loaderRef = new WeakReference (loader); 44 } 45 46 52 public static synchronized ClassInfoRepository getRepository(final ClassLoader loader) { 53 int hash; 54 if (loader == null) { hash = 0; 56 } else { 57 hash = loader.hashCode(); 58 } 59 WeakReference repositoryRef = (WeakReference ) s_repositories.get(hash); 60 ClassInfoRepository repository = ((repositoryRef == null) ? null : (ClassInfoRepository) repositoryRef.get()); 61 if (repository != null) { 62 return repository; 63 } else { 64 ClassInfoRepository repo = new ClassInfoRepository(loader); 65 s_repositories.put(hash, new WeakReference (repo)); 66 return repo; 67 } 68 } 69 70 75 public static void removeClassInfoFromAllClassLoaders(final String className) { 76 throw new UnsupportedOperationException ("fix algorithm"); 78 } 79 80 86 public ClassInfo getClassInfo(final String className) { 87 ClassInfo info = (ClassInfo) m_repository.get(className); 88 if (info == null) { 89 return checkParentClassRepository(className, (ClassLoader ) m_loaderRef.get()); 90 } 91 return (ClassInfo) m_repository.get(className); 92 } 93 94 99 public void addClassInfo(final ClassInfo classInfo) { 100 if (checkParentClassRepository(classInfo.getName(), (ClassLoader ) m_loaderRef.get()) == null) { 102 m_repository.put(new String (classInfo.getName()), classInfo); 104 } else { 105 } 107 } 108 109 115 public boolean hasClassInfo(final String name) { 116 return m_repository.containsKey(name); 117 } 118 119 128 public ClassInfo checkParentClassRepository(final String className, final ClassLoader loader) { 129 if (loader == null) { 130 return null; 131 } 132 ClassInfo info; 133 ClassLoader parent = loader.getParent(); 134 if (parent == null) { 135 return null; 136 } else { 137 info = ClassInfoRepository.getRepository(parent).getClassInfo(className); 138 if (info != null) { 139 return info; 140 } else { 141 return checkParentClassRepository(className, parent); 142 } 143 } 144 } 145 } | Popular Tags |