1 8 package org.codehaus.aspectwerkz.reflect.impl.asm; 9 10 import gnu.trove.TIntObjectHashMap; 11 import org.codehaus.aspectwerkz.reflect.ClassInfo; 12 import org.codehaus.aspectwerkz.exception.DefinitionException; 13 14 import java.lang.ref.WeakReference ; 15 import java.lang.ref.SoftReference ; 16 import java.lang.ref.Reference ; 17 import java.util.Properties ; 18 import java.io.InputStream ; 19 import java.io.IOException ; 20 21 26 public class AsmClassInfoRepository { 27 30 private static final TIntObjectHashMap s_repositories = new TIntObjectHashMap(); 31 32 35 private final TIntObjectHashMap m_repository = new TIntObjectHashMap(); 36 37 40 private transient final WeakReference m_loaderRef; 41 42 45 private final Properties m_annotationProperties; 46 47 52 private AsmClassInfoRepository(final ClassLoader loader) { 53 m_loaderRef = new WeakReference (loader); 54 m_annotationProperties = new Properties (); 55 if (loader != null) { 56 try { 57 InputStream stream = loader.getResourceAsStream("annotation.properties"); 58 if (stream != null) { 59 try { 60 m_annotationProperties.load(stream); 61 } finally { 62 try { 63 stream.close(); 64 } catch (Exception e) { 65 ; 66 } 67 } 68 } 69 } catch (IOException e) { 70 throw new DefinitionException("could not find resource [annotation.properties] on classpath"); 71 } 72 } 73 } 74 75 81 public static synchronized AsmClassInfoRepository getRepository(final ClassLoader loader) { 82 int hash; 83 if (loader == null) { hash = 0; 85 } else { 86 hash = loader.hashCode(); 87 } 88 Reference repositoryRef = (Reference ) s_repositories.get(hash); 89 AsmClassInfoRepository repository = ((repositoryRef == null) ? null : (AsmClassInfoRepository) repositoryRef 90 .get()); 91 if (repository != null) { 92 return repository; 93 } else { 94 AsmClassInfoRepository repo = new AsmClassInfoRepository(loader); 95 s_repositories.put(hash, new SoftReference (repo)); 96 return repo; 97 } 98 } 99 100 105 public static void removeClassInfoFromAllClassLoaders(final String className) { 106 throw new UnsupportedOperationException ("fix algorithm"); 108 } 109 110 116 public ClassInfo getClassInfo(final String className) { 117 Reference classInfoRef = ((Reference ) m_repository.get(className.hashCode())); 118 ClassInfo info = (classInfoRef == null) ? null : (ClassInfo) (classInfoRef.get()); 119 if (info == null) { 120 return checkParentClassRepository(className, (ClassLoader ) m_loaderRef.get()); 121 } 122 return info; 123 } 124 125 130 public void addClassInfo(final ClassInfo classInfo) { 131 if (checkParentClassRepository(classInfo.getName(), (ClassLoader ) m_loaderRef.get()) == null) { 133 m_repository.put(classInfo.getName().hashCode(), new SoftReference (classInfo)); 134 } else { 135 } 138 } 139 140 146 public boolean hasClassInfo(final String name) { 147 Reference classInfoRef = (Reference ) m_repository.get(name.hashCode()); 148 return (classInfoRef == null) ? false : (classInfoRef.get() != null); 149 } 150 151 156 public void removeClassInfo(final String className) { 157 m_repository.remove(className.hashCode()); 158 } 159 160 165 public Properties getAnnotationProperties() { 166 return m_annotationProperties; 167 } 168 169 178 public ClassInfo checkParentClassRepository(final String className, final ClassLoader loader) { 179 if (loader == null) { 180 return null; 181 } 182 ClassInfo info; 183 ClassLoader parent = loader.getParent(); 184 if (parent == null) { 185 return null; 186 } else { 187 info = AsmClassInfoRepository.getRepository(parent).getClassInfo(className); 188 if (info != null) { 189 return info; 190 } else { 191 return checkParentClassRepository(className, parent); 192 } 193 } 194 } 195 } | Popular Tags |