Class AnalysisCache
- java.lang.Object
-
- edu.umd.cs.findbugs.classfile.impl.AnalysisCache
-
- All Implemented Interfaces:
IAnalysisCache
public class AnalysisCache extends java.lang.Object implements IAnalysisCache
Implementation of IAnalysisCache. This object is responsible for registering class and method analysis engines and caching analysis results.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
AnalysisCache.AbnormalAnalysisResult
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<?,?>
analysisLocals
private BugReporter
bugReporter
private java.util.Map<java.lang.Class<?>,IClassAnalysisEngine<?>>
classAnalysisEngineMap
private java.util.Map<java.lang.Class<?>,java.util.Map<ClassDescriptor,java.lang.Object>>
classAnalysisMap
private IClassPath
classPath
private java.util.Map<java.lang.Class<?>,IDatabaseFactory<?>>
databaseFactoryMap
private java.util.Map<java.lang.Class<?>,java.lang.Object>
databaseMap
private static int
DEFAULT_RESULTS_TO_CACHE
Maximum number of results to cache.private static int
MAX_CLASS_RESULTS_TO_CACHE
Maximum number of class analysis results to cache.private static int
MAX_CONSTANT_POOL_GEN_RESULTS_TO_CACHE
private static int
MAX_FBCLASSREADER_RESULTS_TO_CACHE
private static int
MAX_JAVACLASS_RESULTS_TO_CACHE
private java.util.Map<java.lang.Class<?>,IMethodAnalysisEngine<?>>
methodAnalysisEngineMap
(package private) static AnalysisCache.AbnormalAnalysisResult
NULL_ANALYSIS_RESULT
-
Constructor Summary
Constructors Constructor Description AnalysisCache(IClassPath classPath, BugReporter errorLogger)
Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private <E> E
analyzeMethod(ClassContext classContext, java.lang.Class<E> analysisClass, MethodDescriptor methodDescriptor)
Analyze a method.(package private) static <E> E
checkedCast(java.lang.Class<E> analysisClass, java.lang.Object o)
private static <DescriptorType>
java.util.Map<DescriptorType,java.lang.Object>createMap(java.util.Map<java.lang.Class<?>,? extends IAnalysisEngine<DescriptorType,?>> engineMap, java.lang.Class<?> analysisClass)
void
dispose()
Cleans up all cached data<E> void
eagerlyPutDatabase(java.lang.Class<E> databaseClass, E database)
Eagerly install a database.<E> void
eagerlyPutMethodAnalysis(java.lang.Class<E> analysisClass, MethodDescriptor methodDescriptor, E analysisObject)
Eagerly put a method analysis object in the cache.private static <DescriptorType>
java.util.Map<DescriptorType,java.lang.Object>findOrCreateDescriptorMap(java.util.Map<java.lang.Class<?>,java.util.Map<DescriptorType,java.lang.Object>> analysisClassToDescriptorMapMap, java.util.Map<java.lang.Class<?>,? extends IAnalysisEngine<DescriptorType,?>> engineMap, java.lang.Class<?> analysisClass)
Find or create a descriptor to analysis object map.private <E> java.util.Map<ClassDescriptor,E>
getAllClassAnalysis(java.lang.Class<E> analysisClass)
java.util.Map<?,?>
getAnalysisLocals()
Get map of analysis-local objects.java.util.Map<ClassDescriptor,java.lang.Object>
getClassAnalysis(java.lang.Class<?> analysisClass)
<E> E
getClassAnalysis(java.lang.Class<E> analysisClass, ClassDescriptor classDescriptor)
Get an analysis of the given class.IClassPath
getClassPath()
Get the classpath from which classes are loaded.<E> E
getDatabase(java.lang.Class<E> databaseClass)
Get a database.<E> E
getDatabase(java.lang.Class<E> databaseClass, boolean optional)
IErrorLogger
getErrorLogger()
Get the error logger.<E> E
getMethodAnalysis(java.lang.Class<E> analysisClass, MethodDescriptor methodDescriptor)
Get an analysis of the given method.<E> E
getOptionalDatabase(java.lang.Class<E> databaseClass)
Profiler
getProfiler()
Get the analysis profiler instance, never null(package private) java.lang.String
hex(java.lang.Object o)
<E> E
probeClassAnalysis(java.lang.Class<E> analysisClass, ClassDescriptor classDescriptor)
See if the cache contains a cached class analysis result for given class descriptor.void
purgeAllMethodAnalysis()
Purge all method analysis results for all methods.void
purgeClassAnalysis(java.lang.Class<?> analysisClass)
Purge all class analysis results of a particular kindvoid
purgeMethodAnalyses(MethodDescriptor methodDescriptor)
Purge all analysis results for given method.<E> void
registerClassAnalysisEngine(java.lang.Class<E> analysisResultType, IClassAnalysisEngine<E> classAnalysisEngine)
Register the given class analysis engine as producing the analysis result type whose Class is given.<E> void
registerDatabaseFactory(java.lang.Class<E> databaseClass, IDatabaseFactory<E> databaseFactory)
Register a database factory.<E> void
registerMethodAnalysisEngine(java.lang.Class<E> analysisResultType, IMethodAnalysisEngine<E> methodAnalysisEngine)
Register the given method analysis engine as producing the analysis result type whose Class is given.<E> void
reuseClassAnalysis(java.lang.Class<E> analysisClass, java.util.Map<ClassDescriptor,java.lang.Object> map)
Adds the data for given analysis type from given map to the cache
-
-
-
Field Detail
-
MAX_JAVACLASS_RESULTS_TO_CACHE
private static final int MAX_JAVACLASS_RESULTS_TO_CACHE
- See Also:
- Constant Field Values
-
MAX_FBCLASSREADER_RESULTS_TO_CACHE
private static final int MAX_FBCLASSREADER_RESULTS_TO_CACHE
- See Also:
- Constant Field Values
-
MAX_CONSTANT_POOL_GEN_RESULTS_TO_CACHE
private static final int MAX_CONSTANT_POOL_GEN_RESULTS_TO_CACHE
- See Also:
- Constant Field Values
-
MAX_CLASS_RESULTS_TO_CACHE
private static final int MAX_CLASS_RESULTS_TO_CACHE
Maximum number of class analysis results to cache.- See Also:
- Constant Field Values
-
DEFAULT_RESULTS_TO_CACHE
private static final int DEFAULT_RESULTS_TO_CACHE
Maximum number of results to cache. Plugins such as FindSecBugs are using the cache and using an unbounded map can lead to OOM errors on large projects- See Also:
- Constant Field Values
-
classPath
private final IClassPath classPath
-
bugReporter
private final BugReporter bugReporter
-
classAnalysisEngineMap
private final java.util.Map<java.lang.Class<?>,IClassAnalysisEngine<?>> classAnalysisEngineMap
-
methodAnalysisEngineMap
private final java.util.Map<java.lang.Class<?>,IMethodAnalysisEngine<?>> methodAnalysisEngineMap
-
databaseFactoryMap
private final java.util.Map<java.lang.Class<?>,IDatabaseFactory<?>> databaseFactoryMap
-
classAnalysisMap
private final java.util.Map<java.lang.Class<?>,java.util.Map<ClassDescriptor,java.lang.Object>> classAnalysisMap
-
databaseMap
private final java.util.Map<java.lang.Class<?>,java.lang.Object> databaseMap
-
analysisLocals
private final java.util.Map<?,?> analysisLocals
-
NULL_ANALYSIS_RESULT
static final AnalysisCache.AbnormalAnalysisResult NULL_ANALYSIS_RESULT
-
-
Constructor Detail
-
AnalysisCache
AnalysisCache(IClassPath classPath, BugReporter errorLogger)
Constructor.- Parameters:
classPath
- the IClassPath to load resources fromerrorLogger
- the IErrorLogger
-
-
Method Detail
-
getAnalysisLocals
public final java.util.Map<?,?> getAnalysisLocals()
Description copied from interface:IAnalysisCache
Get map of analysis-local objects.- Specified by:
getAnalysisLocals
in interfaceIAnalysisCache
-
checkedCast
static <E> E checkedCast(java.lang.Class<E> analysisClass, java.lang.Object o)
-
getClassPath
public IClassPath getClassPath()
Description copied from interface:IAnalysisCache
Get the classpath from which classes are loaded.- Specified by:
getClassPath
in interfaceIAnalysisCache
- Returns:
- the classpath
-
purgeAllMethodAnalysis
public void purgeAllMethodAnalysis()
Description copied from interface:IAnalysisCache
Purge all method analysis results for all methods.- Specified by:
purgeAllMethodAnalysis
in interfaceIAnalysisCache
-
getAllClassAnalysis
private <E> java.util.Map<ClassDescriptor,E> getAllClassAnalysis(java.lang.Class<E> analysisClass)
-
purgeClassAnalysis
public void purgeClassAnalysis(java.lang.Class<?> analysisClass)
Description copied from interface:IAnalysisCache
Purge all class analysis results of a particular kind- Specified by:
purgeClassAnalysis
in interfaceIAnalysisCache
-
dispose
public void dispose()
Cleans up all cached data
-
getClassAnalysis
@CheckForNull public java.util.Map<ClassDescriptor,java.lang.Object> getClassAnalysis(java.lang.Class<?> analysisClass)
- Parameters:
analysisClass
- non null analysis type- Returns:
- map with analysis data for given type, can be null
-
reuseClassAnalysis
public <E> void reuseClassAnalysis(java.lang.Class<E> analysisClass, java.util.Map<ClassDescriptor,java.lang.Object> map)
Adds the data for given analysis type from given map to the cache- Parameters:
analysisClass
- non null analysis typemap
- non null, pre-filled map with analysis data for given type
-
getClassAnalysis
public <E> E getClassAnalysis(java.lang.Class<E> analysisClass, @Nonnull ClassDescriptor classDescriptor) throws CheckedAnalysisException
Description copied from interface:IAnalysisCache
Get an analysis of the given class.- Specified by:
getClassAnalysis
in interfaceIAnalysisCache
- Type Parameters:
E
- the type of the analysis (e.g., FoobarAnalysis)- Parameters:
analysisClass
- the analysis class object (e.g., FoobarAnalysis.class)classDescriptor
- the descriptor of the class to analyze- Returns:
- the analysis object (e.g., instance of FoobarAnalysis for the class)
- Throws:
CheckedAnalysisException
- if an error occurs performing the analysis
-
probeClassAnalysis
public <E> E probeClassAnalysis(java.lang.Class<E> analysisClass, @Nonnull ClassDescriptor classDescriptor)
Description copied from interface:IAnalysisCache
See if the cache contains a cached class analysis result for given class descriptor.- Specified by:
probeClassAnalysis
in interfaceIAnalysisCache
- Parameters:
analysisClass
- analysis result classclassDescriptor
- the class descriptor- Returns:
- a cached analysis result, or null if there is no cached analysis result
-
hex
java.lang.String hex(java.lang.Object o)
-
getMethodAnalysis
public <E> E getMethodAnalysis(java.lang.Class<E> analysisClass, @Nonnull MethodDescriptor methodDescriptor) throws CheckedAnalysisException
Description copied from interface:IAnalysisCache
Get an analysis of the given method.- Specified by:
getMethodAnalysis
in interfaceIAnalysisCache
- Type Parameters:
E
- the type of the analysis (e.g., FoobarAnalysis)- Parameters:
analysisClass
- the analysis class object (e.g., FoobarAnalysis.class)methodDescriptor
- the descriptor of the method to analyze- Returns:
- the analysis object (e.g., instance of FoobarAnalysis for the method)
- Throws:
CheckedAnalysisException
- if an error occurs performing the analysis
-
analyzeMethod
private <E> E analyzeMethod(ClassContext classContext, java.lang.Class<E> analysisClass, MethodDescriptor methodDescriptor) throws CheckedAnalysisException
Analyze a method.- Parameters:
classContext
- ClassContext storing method analysis objects for method's classanalysisClass
- class the method analysis object should belong tomethodDescriptor
- method descriptor identifying the method to analyze- Returns:
- the computed analysis object for the method
- Throws:
CheckedAnalysisException
-
eagerlyPutMethodAnalysis
public <E> void eagerlyPutMethodAnalysis(java.lang.Class<E> analysisClass, @Nonnull MethodDescriptor methodDescriptor, E analysisObject)
Description copied from interface:IAnalysisCache
Eagerly put a method analysis object in the cache. This can be necessary if an method analysis engine invokes other analysis engines that might recursively require the analysis being produced.- Specified by:
eagerlyPutMethodAnalysis
in interfaceIAnalysisCache
- Type Parameters:
E
- the type of the analysis (e.g., FoobarAnalysis)- Parameters:
analysisClass
- the analysis class object (e.g., FoobarAnalysis.class)methodDescriptor
- the descriptor of the method to analyze
-
purgeMethodAnalyses
public void purgeMethodAnalyses(@Nonnull MethodDescriptor methodDescriptor)
Description copied from interface:IAnalysisCache
Purge all analysis results for given method. This can be called when a CFG is pruned and we want to compute more accurate analysis results on the new CFG.- Specified by:
purgeMethodAnalyses
in interfaceIAnalysisCache
- Parameters:
methodDescriptor
- method whose analysis results should be purged
-
findOrCreateDescriptorMap
private static <DescriptorType> java.util.Map<DescriptorType,java.lang.Object> findOrCreateDescriptorMap(java.util.Map<java.lang.Class<?>,java.util.Map<DescriptorType,java.lang.Object>> analysisClassToDescriptorMapMap, java.util.Map<java.lang.Class<?>,? extends IAnalysisEngine<DescriptorType,?>> engineMap, java.lang.Class<?> analysisClass)
Find or create a descriptor to analysis object map.- Type Parameters:
DescriptorType
- type of descriptor used as the map's key type (ClassDescriptor or MethodDescriptor)- Parameters:
analysisClassToDescriptorMapMap
- analysis class to descriptor map mapengineMap
- analysis class to analysis engine mapanalysisClass
- the analysis map- Returns:
- the descriptor to analysis object map
-
createMap
private static <DescriptorType> java.util.Map<DescriptorType,java.lang.Object> createMap(java.util.Map<java.lang.Class<?>,? extends IAnalysisEngine<DescriptorType,?>> engineMap, java.lang.Class<?> analysisClass)
-
registerClassAnalysisEngine
public <E> void registerClassAnalysisEngine(java.lang.Class<E> analysisResultType, IClassAnalysisEngine<E> classAnalysisEngine)
Description copied from interface:IAnalysisCache
Register the given class analysis engine as producing the analysis result type whose Class is given.- Specified by:
registerClassAnalysisEngine
in interfaceIAnalysisCache
- Type Parameters:
E
- analysis result type- Parameters:
analysisResultType
- analysis result type Class objectclassAnalysisEngine
- the class analysis engine to register
-
registerMethodAnalysisEngine
public <E> void registerMethodAnalysisEngine(java.lang.Class<E> analysisResultType, IMethodAnalysisEngine<E> methodAnalysisEngine)
Description copied from interface:IAnalysisCache
Register the given method analysis engine as producing the analysis result type whose Class is given.- Specified by:
registerMethodAnalysisEngine
in interfaceIAnalysisCache
- Type Parameters:
E
- analysis result type- Parameters:
analysisResultType
- analysis result type Class objectmethodAnalysisEngine
- the method analysis engine to register
-
registerDatabaseFactory
public <E> void registerDatabaseFactory(java.lang.Class<E> databaseClass, IDatabaseFactory<E> databaseFactory)
Description copied from interface:IAnalysisCache
Register a database factory.- Specified by:
registerDatabaseFactory
in interfaceIAnalysisCache
- Type Parameters:
E
- type of database- Parameters:
databaseClass
- Class of databasedatabaseFactory
- the database factory
-
getDatabase
public <E> E getDatabase(java.lang.Class<E> databaseClass)
Description copied from interface:IAnalysisCache
Get a database. Note: an unchecked analysis exception will be thrown if the database cannot be instantiated. Since instantiation of most kinds of databases simply involves creating an object (and not opening a file or other failure-prone operation), throwing a CheckedAnalysisException creates too great of an exception-handling burden on analyses and detectors which use databases.- Specified by:
getDatabase
in interfaceIAnalysisCache
- Type Parameters:
E
- type of database- Parameters:
databaseClass
- Class of database- Returns:
- the database (which is created by a database factory if required)
-
getOptionalDatabase
@CheckForNull public <E> E getOptionalDatabase(java.lang.Class<E> databaseClass)
- Specified by:
getOptionalDatabase
in interfaceIAnalysisCache
-
getDatabase
public <E> E getDatabase(java.lang.Class<E> databaseClass, boolean optional)
-
eagerlyPutDatabase
public <E> void eagerlyPutDatabase(java.lang.Class<E> databaseClass, E database)
Description copied from interface:IAnalysisCache
Eagerly install a database. This avoids the need to register a database factory.- Specified by:
eagerlyPutDatabase
in interfaceIAnalysisCache
- Type Parameters:
E
- type of database- Parameters:
databaseClass
- Class of databasedatabase
- database object
-
getErrorLogger
public IErrorLogger getErrorLogger()
Description copied from interface:IAnalysisCache
Get the error logger.- Specified by:
getErrorLogger
in interfaceIAnalysisCache
- Returns:
- the error logger
-
getProfiler
public Profiler getProfiler()
Description copied from interface:IAnalysisCache
Get the analysis profiler instance, never null- Specified by:
getProfiler
in interfaceIAnalysisCache
-
-