use of edu.umd.cs.findbugs.ba.AnalysisContext in project spotbugs by spotbugs.
the class SourceMatcherTest method testRealPathMatchWithRegexpAndAnalysisContext.
@Test
public void testRealPathMatchWithRegexpAndAnalysisContext() throws Exception {
// add this test class as the bug target
bug.addClass("SourceMatcherTest", null);
ClassAnnotation primaryClass = bug.getPrimaryClass();
// set source file
primaryClass.setSourceLines(SourceLineAnnotation.createUnknown("SourceMatcherTest", "SourceMatcherTest.java"));
// setup a testing project with source directory, as of right now the source directory should really exist!!
Project testProject = new Project();
String sourceDir = "src/test/java/edu/umd/cs/findbugs/filter";
testProject.addSourceDirs(Collections.singletonList(sourceDir));
// setup test analysis context
AnalysisContext.setCurrentAnalysisContext(new AnalysisContext(testProject));
// regexp match source folder with analysis context
SourceMatcher sm = new SourceMatcher("~.*findbugs.*.java");
assertTrue("The regex matches the source directory of the given java file", sm.match(bug));
sm = new SourceMatcher("~.*notfound.*.java");
assertFalse("The regex does not match the source directory of the given java file", sm.match(bug));
}
use of edu.umd.cs.findbugs.ba.AnalysisContext in project spotbugs by spotbugs.
the class PlaceholderTest method setup.
@Before
public void setup() {
Project project = new Project();
reporter = new SarifBugReporter(project);
writer = new StringWriter();
reporter.setWriter(new PrintWriter(writer));
reporter.setPriorityThreshold(Priorities.IGNORE_PRIORITY);
DetectorFactoryCollection.resetInstance(new DetectorFactoryCollection());
IAnalysisCache analysisCache = ClassFactory.instance().createAnalysisCache(new ClassPathImpl(), reporter);
Global.setAnalysisCacheForCurrentThread(analysisCache);
FindBugs2.registerBuiltInAnalysisEngines(analysisCache);
AnalysisContext analysisContext = new AnalysisContext(project) {
public boolean isApplicationClass(@DottedClassName String className) {
// treat all classes as application class, to report bugs in it
return true;
}
};
AnalysisContext.setCurrentAnalysisContext(analysisContext);
}
use of edu.umd.cs.findbugs.ba.AnalysisContext in project spotbugs by spotbugs.
the class MethodGenFactory method analyze.
/*
* (non-Javadoc)
*
* @see
* edu.umd.cs.findbugs.classfile.IAnalysisEngine#analyze(edu.umd.cs.findbugs
* .classfile.IAnalysisCache, java.lang.Object)
*/
@Override
public MethodGen analyze(IAnalysisCache analysisCache, MethodDescriptor descriptor) throws CheckedAnalysisException {
Method method = getMethod(analysisCache, descriptor);
if (method.getCode() == null) {
return null;
}
XMethod xmethod = XFactory.createXMethod(descriptor);
if (xmethod.usesInvokeDynamic() && false) {
AnalysisContext.currentAnalysisContext().analysisSkippedDueToInvokeDynamic(xmethod);
return null;
}
try {
AnalysisContext analysisContext = AnalysisContext.currentAnalysisContext();
JavaClass jclass = getJavaClass(analysisCache, descriptor.getClassDescriptor());
ConstantPoolGen cpg = getConstantPoolGen(analysisCache, descriptor.getClassDescriptor());
String methodName = method.getName();
int codeLength = method.getCode().getCode().length;
String superclassName = jclass.getSuperclassName();
if (codeLength > 6000 && Const.STATIC_INITIALIZER_NAME.equals(methodName) && "java.lang.Enum".equals(superclassName)) {
analysisContext.getLookupFailureCallback().reportSkippedAnalysis(new JavaClassAndMethod(jclass, method).toMethodDescriptor());
return null;
}
if (analysisContext.getBoolProperty(AnalysisFeatures.SKIP_HUGE_METHODS)) {
if (codeLength > 6000 || (Const.STATIC_INITIALIZER_NAME.equals(methodName) || "getContents".equals(methodName)) && codeLength > 2000) {
analysisContext.getLookupFailureCallback().reportSkippedAnalysis(new JavaClassAndMethod(jclass, method).toMethodDescriptor());
return null;
}
}
return new MethodGen(method, jclass.getClassName(), cpg);
} catch (Exception e) {
AnalysisContext.logError("Error constructing methodGen", e);
return null;
}
}
use of edu.umd.cs.findbugs.ba.AnalysisContext in project spotbugs by spotbugs.
the class TypeReturnNull method isExplicitlyNullable.
private boolean isExplicitlyNullable() {
AnalysisContext analysisContext = AnalysisContext.currentAnalysisContext();
INullnessAnnotationDatabase nullnessAnnotationDatabase = analysisContext.getNullnessAnnotationDatabase();
XMethod xMethod = getXMethod();
NullnessAnnotation na = nullnessAnnotationDatabase.getResolvedAnnotation(xMethod, true);
return na != null && na != NullnessAnnotation.NONNULL;
}
use of edu.umd.cs.findbugs.ba.AnalysisContext in project spotbugs by spotbugs.
the class FindNonShortCircuitTest method setup.
@Before
public void setup() {
PrintingBugReporter bugReporter = new PrintingBugReporter();
ctx = AnalysisContext.currentAnalysisContext();
if (ctx == null) {
AnalysisContext.setCurrentAnalysisContext(new AnalysisContext(new Project()));
}
check = new FindNonShortCircuit(bugReporter);
}
Aggregations