1 19 20 package edu.umd.cs.findbugs.ba; 21 22 import java.io.IOException ; 23 24 import org.apache.bcel.Repository; 25 import org.apache.bcel.classfile.JavaClass; 26 27 import edu.umd.cs.findbugs.annotations.NonNull; 28 import edu.umd.cs.findbugs.ba.ch.Subtypes; 29 import edu.umd.cs.findbugs.ba.npe.ParameterNullnessPropertyDatabase; 30 import edu.umd.cs.findbugs.ba.type.FieldStoreTypeDatabase; 31 import edu.umd.cs.findbugs.util.MapCache; 32 33 40 public class LegacyAnalysisContext extends AnalysisContext { 41 42 private RepositoryLookupFailureCallback lookupFailureCallback; 43 private SourceFinder sourceFinder; 44 private MapCache<JavaClass, ClassContext> classContextCache; 45 private Subtypes subtypes; 46 private final SourceInfoMap sourceInfoMap; 47 private InnerClassAccessMap innerClassAccessMap; 48 49 private FieldStoreTypeDatabase fieldStoreTypeDatabase; 52 private ParameterNullnessPropertyDatabase unconditionalDerefParamDatabase; 53 54 55 private NullnessAnnotationDatabase nullnessAnnotationDatabase; 57 @Override 58 public NullnessAnnotationDatabase getNullnessAnnotationDatabase() { 59 return nullnessAnnotationDatabase; 60 } 61 62 private CheckReturnAnnotationDatabase checkReturnAnnotationDatabase; 63 64 @Override 65 public CheckReturnAnnotationDatabase getCheckReturnAnnotationDatabase() { 66 return checkReturnAnnotationDatabase; 67 } 68 69 private AnnotationRetentionDatabase annotationRetentionDatabase; 70 71 @Override 72 public AnnotationRetentionDatabase getAnnotationRetentionDatabase() { 73 return annotationRetentionDatabase; 74 } 75 76 private JCIPAnnotationDatabase jcipAnnotationDatabase; 77 78 @Override 79 public JCIPAnnotationDatabase getJCIPAnnotationDatabase() { 80 return jcipAnnotationDatabase; 81 } 82 83 86 private static final org.apache.bcel.util.Repository originalRepository = 87 Repository.getRepository(); 89 93 private static final int DEFAULT_CACHE_SIZE = 6; 94 95 101 LegacyAnalysisContext(RepositoryLookupFailureCallback lookupFailureCallback) { 102 this.lookupFailureCallback = lookupFailureCallback; 103 this.sourceFinder = new SourceFinder(); 104 this.subtypes = new Subtypes(); 105 this.sourceInfoMap = new SourceInfoMap(); 106 107 if (originalRepository instanceof URLClassPathRepository) { 108 getLookupFailureCallback().logError( 109 "originalRepository is a URLClassPathRepository, which may cause problems"); 110 } 111 112 115 130 } 131 132 @Override 133 public void initDatabases() { 134 checkReturnAnnotationDatabase = new CheckReturnAnnotationDatabase(); 135 annotationRetentionDatabase = new AnnotationRetentionDatabase(); 136 jcipAnnotationDatabase = new JCIPAnnotationDatabase(); 137 nullnessAnnotationDatabase = new NullnessAnnotationDatabase(); 138 } 139 140 141 @Override 142 public void updateDatabases(int pass) { 143 if (pass == 0) { 144 checkReturnAnnotationDatabase.loadAuxiliaryAnnotations(); 145 nullnessAnnotationDatabase.loadAuxiliaryAnnotations(); 146 } 147 } 148 @Override 149 public RepositoryLookupFailureCallback getLookupFailureCallback() { 150 return lookupFailureCallback; 151 } 152 153 @Override 154 public SourceFinder getSourceFinder() { 155 return sourceFinder; 156 } 157 158 @Override 159 public Subtypes getSubtypes() { 160 return subtypes; 161 } 162 163 @Override 164 public void clearRepository() { 165 org.apache.bcel.util.Repository repos = Repository.getRepository(); 169 if (repos instanceof URLClassPathRepository) { 170 ((URLClassPathRepository) repos).destroy(); 171 } 172 173 Repository.clearCache(); 175 176 clearClassContextCache(); 178 179 getInnerClassAccessMap().clearCache(); 181 182 URLClassPathRepository repository = new URLClassPathRepository(); 184 Repository.setRepository(repository); 185 } 186 187 @Override 188 public void clearClassContextCache() { 189 if (classContextCache != null) 190 classContextCache.clear(); 191 } 192 193 @Override 194 public void addClasspathEntry(String url) throws IOException { 195 URLClassPathRepository repos = (URLClassPathRepository) Repository.getRepository(); 196 repos.addURL(url); 197 } 198 199 @Override 200 public void addApplicationClassToRepository(JavaClass appClass) { 201 Repository.addClass(appClass); 202 subtypes.addApplicationClass(appClass); 203 } 204 205 @Override 206 public JavaClass lookupClass(@NonNull String className) throws ClassNotFoundException { 207 if (className == null) throw new IllegalArgumentException ("className is null"); 209 return Repository.lookupClass(className); 210 } 212 213 219 int hits = 0; 220 int misses = 0; 221 222 @Override 223 public ClassContext getClassContext(JavaClass javaClass) { 224 if (classContextCache == null) { 225 int cacheSize = getBoolProperty(AnalysisFeatures.CONSERVE_SPACE) ? 3 : DEFAULT_CACHE_SIZE; 226 classContextCache = new MapCache<JavaClass,ClassContext>(cacheSize); 227 } 228 ClassContext classContext = classContextCache.get(javaClass); 229 if (classContext == null) { 230 classContext = new ClassContext(javaClass, this); 231 classContextCache.put(javaClass, classContext); 232 misses++; 233 } else hits++; 234 return classContext; 235 } 236 237 @Override 238 public String getClassContextStats() { 239 if (classContextCache == null) return null; 240 return hits + "/" + misses + ":" + classContextCache.getStatistics(); 241 } 242 243 @Override 244 public SourceInfoMap getSourceInfoMap() { 245 return sourceInfoMap; 246 } 247 248 @Override 249 public FieldStoreTypeDatabase getFieldStoreTypeDatabase() { 250 if (fieldStoreTypeDatabase == null) { 251 fieldStoreTypeDatabase = new FieldStoreTypeDatabase(); 252 } 253 return fieldStoreTypeDatabase; 254 } 255 256 @Override 257 public ParameterNullnessPropertyDatabase getUnconditionalDerefParamDatabase() { 258 if (unconditionalDerefParamDatabase == null) { 259 unconditionalDerefParamDatabase = new ParameterNullnessPropertyDatabase(); 260 } 261 return unconditionalDerefParamDatabase; 262 } 263 264 267 @Override 268 public InnerClassAccessMap getInnerClassAccessMap() { 269 if (innerClassAccessMap == null) { 270 innerClassAccessMap = InnerClassAccessMap.create(); 271 } 272 return innerClassAccessMap; 273 } 274 275 276 277 } 278 | Popular Tags |