1 4 package com.tc.aspectwerkz.reflect; 5 6 import java.lang.ref.WeakReference ; 7 import java.util.HashMap ; 8 import java.util.Map ; 9 import java.util.WeakHashMap ; 10 11 16 public class ClassInfoRepository { 17 20 private static final HashMap s_repositories = new HashMap (); 21 22 25 private final Map m_repository = new WeakHashMap (); 26 27 30 private transient final WeakReference m_loaderRef; 31 32 37 private ClassInfoRepository(final ClassLoader loader) { 38 m_loaderRef = new WeakReference (loader); 39 } 40 41 47 public static synchronized ClassInfoRepository getRepository(final ClassLoader loader) { 48 Integer hash = new Integer (loader == null ? 0 : loader.hashCode()); WeakReference repositoryRef = (WeakReference ) s_repositories.get(hash); 50 ClassInfoRepository repository = repositoryRef == null ? null : (ClassInfoRepository) repositoryRef.get(); 51 if (repository != null) { 52 return repository; 53 } else { 54 ClassInfoRepository repo = new ClassInfoRepository(loader); 55 s_repositories.put(hash, new WeakReference (repo)); 56 return repo; 57 } 58 } 59 60 65 public static void removeClassInfoFromAllClassLoaders(final String className) { 66 throw new UnsupportedOperationException ("fix algorithm"); 68 } 69 70 76 public ClassInfo getClassInfo(final String className) { 77 ClassInfo info = (ClassInfo) m_repository.get(className); 78 if (info == null) { 79 return checkParentClassRepository(className, (ClassLoader ) m_loaderRef.get()); 80 } 81 return (ClassInfo) m_repository.get(className); 82 } 83 84 89 public void addClassInfo(final ClassInfo classInfo) { 90 if (checkParentClassRepository(classInfo.getName(), (ClassLoader ) m_loaderRef.get()) == null) { 92 m_repository.put(new String (classInfo.getName()), classInfo); 94 } else { 95 } 97 } 98 99 105 public boolean hasClassInfo(final String name) { 106 return m_repository.containsKey(name); 107 } 108 109 118 public ClassInfo checkParentClassRepository(final String className, final ClassLoader loader) { 119 if (loader == null) { 120 return null; 121 } 122 ClassInfo info; 123 ClassLoader parent = loader.getParent(); 124 if (parent == null) { 125 return null; 126 } else { 127 info = ClassInfoRepository.getRepository(parent).getClassInfo(className); 128 if (info != null) { 129 return info; 130 } else { 131 return checkParentClassRepository(className, parent); 132 } 133 } 134 } 135 } | Popular Tags |