1 8 package org.codehaus.aspectwerkz.reflect.impl.java; 9 10 import gnu.trove.TIntObjectHashMap; 11 import org.codehaus.aspectwerkz.reflect.ClassInfo; 12 13 import java.lang.ref.WeakReference ; 14 import java.util.Map ; 15 import java.util.WeakHashMap ; 16 17 23 public class JavaClassInfoRepository { 24 27 private static final TIntObjectHashMap s_repositories = new TIntObjectHashMap(); 28 29 32 private final Map m_repository = new WeakHashMap (); 33 34 37 private transient final WeakReference m_loaderRef; 38 39 44 private JavaClassInfoRepository(final ClassLoader loader) { 45 m_loaderRef = new WeakReference (loader); 46 } 47 48 54 public static synchronized JavaClassInfoRepository getRepository(final ClassLoader loader) { 55 int hash; 56 if (loader == null) { hash = 0; 58 } else { 59 hash = loader.hashCode(); 60 } 61 WeakReference repositoryRef = (WeakReference ) s_repositories.get(hash); 62 JavaClassInfoRepository repository = ((repositoryRef == null) ? null : (JavaClassInfoRepository) repositoryRef 63 .get()); 64 if (repository != null) { 65 return repository; 66 } else { 67 JavaClassInfoRepository repo = new JavaClassInfoRepository(loader); 68 s_repositories.put(hash, new WeakReference (repo)); 69 return repo; 70 } 71 } 72 73 78 public static void removeClassInfoFromAllClassLoaders(final String className) { 79 throw new UnsupportedOperationException ("fix algorithm"); 81 } 82 83 89 public ClassInfo getClassInfo(final String className) { 90 ClassInfo info = (ClassInfo) m_repository.get(className); 91 if (info == null) { 92 return checkParentClassRepository(className, (ClassLoader ) m_loaderRef.get()); 93 } 94 return (ClassInfo) m_repository.get(className); 95 } 96 97 102 public void addClassInfo(final ClassInfo classInfo) { 103 if (checkParentClassRepository(classInfo.getName(), (ClassLoader ) m_loaderRef.get()) == null) { 105 m_repository.put(new String (classInfo.getName()), classInfo); 106 } else { 107 } 109 } 110 111 117 public boolean hasClassInfo(final String name) { 118 return m_repository.containsKey(name); 119 } 120 121 130 public ClassInfo checkParentClassRepository(final String className, final ClassLoader loader) { 131 if (loader == null) { 132 return null; 133 } 134 ClassInfo info; 135 ClassLoader parent = loader.getParent(); 136 if (parent == null) { 137 return null; 138 } else { 139 info = JavaClassInfoRepository.getRepository(parent).getClassInfo(className); 140 if (info != null) { 141 return info; 142 } else { 143 return checkParentClassRepository(className, parent); 144 } 145 } 146 } 147 } | Popular Tags |