1 4 package com.tc.aspectwerkz.reflect.impl.java; 5 6 import java.lang.ref.WeakReference ; 7 import java.util.HashMap ; 8 import java.util.Map ; 9 import java.util.WeakHashMap ; 10 11 import com.tc.aspectwerkz.reflect.ClassInfo; 12 13 19 public class JavaClassInfoRepository { 20 23 private static final HashMap s_repositories = new HashMap (); 24 25 28 private final Map m_repository = new WeakHashMap (); 29 30 33 private transient final WeakReference m_loaderRef; 34 35 40 private JavaClassInfoRepository(final ClassLoader loader) { 41 m_loaderRef = new WeakReference (loader); 42 } 43 44 50 public static synchronized JavaClassInfoRepository getRepository(final ClassLoader loader) { 51 Integer hash = new Integer (loader == null ? 0 : loader.hashCode()); 52 WeakReference repositoryRef = (WeakReference ) s_repositories.get(hash); 53 JavaClassInfoRepository repository = repositoryRef == null ? null : (JavaClassInfoRepository) repositoryRef.get(); 54 if (repository != null) { 55 return repository; 56 } else { 57 JavaClassInfoRepository repo = new JavaClassInfoRepository(loader); 58 s_repositories.put(hash, new WeakReference (repo)); 59 return repo; 60 } 61 } 62 63 68 public static void removeClassInfoFromAllClassLoaders(final String className) { 69 throw new UnsupportedOperationException ("fix algorithm"); 71 } 72 73 79 public ClassInfo getClassInfo(final String className) { 80 ClassInfo info = (ClassInfo) m_repository.get(className); 81 if (info == null) { 82 return checkParentClassRepository(className, (ClassLoader ) m_loaderRef.get()); 83 } 84 return (ClassInfo) m_repository.get(className); 85 } 86 87 92 public void addClassInfo(final ClassInfo classInfo) { 93 if (checkParentClassRepository(classInfo.getName(), (ClassLoader ) m_loaderRef.get()) == null) { 95 m_repository.put(new String (classInfo.getName()), classInfo); 96 } else { 97 } 99 } 100 101 107 public boolean hasClassInfo(final String name) { 108 return m_repository.containsKey(name); 109 } 110 111 120 public ClassInfo checkParentClassRepository(final String className, final ClassLoader loader) { 121 if (loader == null) { 122 return null; 123 } 124 ClassInfo info; 125 ClassLoader parent = loader.getParent(); 126 if (parent == null) { 127 return null; 128 } else { 129 info = JavaClassInfoRepository.getRepository(parent).getClassInfo(className); 130 if (info != null) { 131 return info; 132 } else { 133 return checkParentClassRepository(className, parent); 134 } 135 } 136 } 137 } | Popular Tags |