Search in sources :

Example 1 with AnalysisContext

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));
}
Also used : Project(edu.umd.cs.findbugs.Project) ClassAnnotation(edu.umd.cs.findbugs.ClassAnnotation) AnalysisContext(edu.umd.cs.findbugs.ba.AnalysisContext) Test(org.junit.Test)

Example 2 with AnalysisContext

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);
}
Also used : Project(edu.umd.cs.findbugs.Project) StringWriter(java.io.StringWriter) ClassPathImpl(edu.umd.cs.findbugs.classfile.impl.ClassPathImpl) IAnalysisCache(edu.umd.cs.findbugs.classfile.IAnalysisCache) AnalysisContext(edu.umd.cs.findbugs.ba.AnalysisContext) PrintWriter(java.io.PrintWriter) DetectorFactoryCollection(edu.umd.cs.findbugs.DetectorFactoryCollection) DottedClassName(edu.umd.cs.findbugs.internalAnnotations.DottedClassName) Before(org.junit.Before)

Example 3 with 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;
    }
}
Also used : ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) JavaClass(org.apache.bcel.classfile.JavaClass) XMethod(edu.umd.cs.findbugs.ba.XMethod) Method(org.apache.bcel.classfile.Method) JavaClassAndMethod(edu.umd.cs.findbugs.ba.JavaClassAndMethod) XMethod(edu.umd.cs.findbugs.ba.XMethod) AnalysisContext(edu.umd.cs.findbugs.ba.AnalysisContext) JavaClassAndMethod(edu.umd.cs.findbugs.ba.JavaClassAndMethod) MethodGen(org.apache.bcel.generic.MethodGen) CheckedAnalysisException(edu.umd.cs.findbugs.classfile.CheckedAnalysisException)

Example 4 with AnalysisContext

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;
}
Also used : INullnessAnnotationDatabase(edu.umd.cs.findbugs.ba.INullnessAnnotationDatabase) NullnessAnnotation(edu.umd.cs.findbugs.ba.NullnessAnnotation) XMethod(edu.umd.cs.findbugs.ba.XMethod) AnalysisContext(edu.umd.cs.findbugs.ba.AnalysisContext)

Example 5 with AnalysisContext

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);
}
Also used : Project(edu.umd.cs.findbugs.Project) AnalysisContext(edu.umd.cs.findbugs.ba.AnalysisContext) PrintingBugReporter(edu.umd.cs.findbugs.PrintingBugReporter) Before(org.junit.Before)

Aggregations

AnalysisContext (edu.umd.cs.findbugs.ba.AnalysisContext)13 Project (edu.umd.cs.findbugs.Project)4 SourceInfoMap (edu.umd.cs.findbugs.ba.SourceInfoMap)3 CheckedAnalysisException (edu.umd.cs.findbugs.classfile.CheckedAnalysisException)3 IAnalysisCache (edu.umd.cs.findbugs.classfile.IAnalysisCache)3 Before (org.junit.Before)3 DetectorFactoryCollection (edu.umd.cs.findbugs.DetectorFactoryCollection)2 XClass (edu.umd.cs.findbugs.ba.XClass)2 XFactory (edu.umd.cs.findbugs.ba.XFactory)2 XMethod (edu.umd.cs.findbugs.ba.XMethod)2 ClassDescriptor (edu.umd.cs.findbugs.classfile.ClassDescriptor)2 ClassPathImpl (edu.umd.cs.findbugs.classfile.impl.ClassPathImpl)2 FileInputStream (java.io.FileInputStream)2 JavaClass (org.apache.bcel.classfile.JavaClass)2 BugInstance (edu.umd.cs.findbugs.BugInstance)1 ClassAnnotation (edu.umd.cs.findbugs.ClassAnnotation)1 MethodAnnotation (edu.umd.cs.findbugs.MethodAnnotation)1 PrintingBugReporter (edu.umd.cs.findbugs.PrintingBugReporter)1 ProgramPoint (edu.umd.cs.findbugs.ProgramPoint)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1