Search in sources :

Example 6 with Project

use of edu.umd.cs.findbugs.Project 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 7 with Project

use of edu.umd.cs.findbugs.Project 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 8 with Project

use of edu.umd.cs.findbugs.Project in project spotbugs by spotbugs.

the class ObfuscateBugs method main.

public static void main(String[] args) throws Exception {
    FindBugs.setNoAnalysis();
    CommandLine commandLine = new CommandLine();
    int argCount = commandLine.parse(args, 0, 2, "Usage: " + ObfuscateBugs.class.getName() + " [options] [<xml results>] ");
    SortedBugCollection bugCollection = new SortedBugCollection();
    if (argCount < args.length) {
        bugCollection.readXML(args[argCount++]);
    } else {
        bugCollection.readXML(System.in);
    }
    SortedBugCollection results = bugCollection.createEmptyCollectionWithMetadata();
    Project project = results.getProject();
    project.getSourceDirList().clear();
    project.getFileList().clear();
    project.getAuxClasspathEntryList().clear();
    results.getProjectStats().getPackageStats().clear();
    results.clearMissingClasses();
    results.clearErrors();
    for (BugInstance bug : bugCollection) {
        results.add(Obfuscate.obfuscate(bug), false);
    }
    if (argCount == args.length) {
        results.writeXML(System.out);
    } else {
        results.writeXML(args[argCount++]);
    }
}
Also used : Project(edu.umd.cs.findbugs.Project) SortedBugCollection(edu.umd.cs.findbugs.SortedBugCollection) BugInstance(edu.umd.cs.findbugs.BugInstance)

Example 9 with Project

use of edu.umd.cs.findbugs.Project in project spotbugs by spotbugs.

the class SetBugDatabaseInfo method main.

public static void main(String[] args) throws IOException, DocumentException {
    FindBugs.setNoAnalysis();
    DetectorFactoryCollection.instance();
    SetInfoCommandLine commandLine = new SetInfoCommandLine();
    int argCount = commandLine.parse(args, 0, 2, USAGE);
    SortedBugCollection origCollection = new SortedBugCollection();
    if (argCount < args.length) {
        origCollection.readXML(args[argCount++]);
    } else {
        origCollection.readXML(System.in);
    }
    Project project = origCollection.getProject();
    if (commandLine.revisionName != null) {
        origCollection.setReleaseName(commandLine.revisionName);
    }
    if (commandLine.projectName != null) {
        origCollection.getProject().setProjectName(commandLine.projectName);
    }
    if (commandLine.revisionTimestamp != 0) {
        origCollection.setTimestamp(commandLine.revisionTimestamp);
    }
    origCollection.setWithMessages(commandLine.withMessages);
    if (commandLine.exclusionFilterFile != null) {
        project.setSuppressionFilter(Filter.parseFilter(commandLine.exclusionFilterFile));
    }
    if (commandLine.resetProject) {
        project.getSourceDirList().clear();
        project.getFileList().clear();
        project.getAuxClasspathEntryList().clear();
    }
    if (commandLine.resetSource) {
        project.getSourceDirList().clear();
    }
    project.addSourceDirs(commandLine.sourcePaths);
    if (commandLine.purgeStats) {
        origCollection.getProjectStats().getPackageStats().clear();
    }
    if (commandLine.purgeClassStats) {
        for (PackageStats ps : origCollection.getProjectStats().getPackageStats()) {
            ps.getClassStats().clear();
        }
    }
    if (commandLine.purgeMissingClasses) {
        origCollection.clearMissingClasses();
    }
    if (commandLine.lastVersion != null) {
        long last = edu.umd.cs.findbugs.workflow.Filter.FilterCommandLine.getVersionNum(origCollection, commandLine.lastVersion, true);
        if (last < origCollection.getSequenceNumber()) {
            String name = origCollection.getAppVersionFromSequenceNumber(last).getReleaseName();
            long timestamp = origCollection.getAppVersionFromSequenceNumber(last).getTimestamp();
            origCollection.setReleaseName(name);
            origCollection.setTimestamp(timestamp);
            origCollection.trimAppVersions(last);
        }
    }
    Map<String, Set<String>> missingFiles = new HashMap<>();
    if (!commandLine.searchSourcePaths.isEmpty()) {
        sourceSearcher = new SourceSearcher(project);
        for (BugInstance bug : origCollection.getCollection()) {
            SourceLineAnnotation src = bug.getPrimarySourceLineAnnotation();
            if (!sourceSearcher.sourceNotFound.contains(src.getClassName()) && !sourceSearcher.findSource(src)) {
                Set<String> paths = missingFiles.computeIfAbsent(src.getSourceFile(), k -> new HashSet<>());
                String fullPath = fullPath(src);
                // System.out.println("Missing " + fullPath);
                paths.add(fullPath);
            }
        }
        Set<String> foundPaths = new HashSet<>();
        for (String f : commandLine.searchSourcePaths) {
            for (File javaFile : RecursiveSearchForJavaFiles.search(new File(f))) {
                Set<String> matchingMissingClasses = missingFiles.get(javaFile.getName());
                if (matchingMissingClasses == null) {
                // System.out.println("Nothing for " + javaFile);
                } else {
                    for (String sourcePath : matchingMissingClasses) {
                        String path = javaFile.getAbsolutePath();
                        if (path.endsWith(sourcePath)) {
                            String dir = path.substring(0, path.length() - sourcePath.length());
                            foundPaths.add(dir);
                        }
                    }
                }
            }
        }
        Set<String> toRemove = new HashSet<>();
        for (String p1 : foundPaths) {
            for (String p2 : foundPaths) {
                if (!p1.equals(p2) && p1.startsWith(p2)) {
                    toRemove.add(p1);
                    break;
                }
            }
        }
        foundPaths.removeAll(toRemove);
        project.addSourceDirs(foundPaths);
        for (String dir : foundPaths) {
            if (argCount < args.length) {
                System.out.println("Found " + dir);
            }
        }
    }
    if (argCount < args.length) {
        origCollection.writeXML(args[argCount++]);
    } else {
        origCollection.writeXML(System.out);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) SortedBugCollection(edu.umd.cs.findbugs.SortedBugCollection) BugInstance(edu.umd.cs.findbugs.BugInstance) Project(edu.umd.cs.findbugs.Project) SourceLineAnnotation(edu.umd.cs.findbugs.SourceLineAnnotation) PackageStats(edu.umd.cs.findbugs.PackageStats) File(java.io.File) HashSet(java.util.HashSet)

Example 10 with Project

use of edu.umd.cs.findbugs.Project in project spotbugs by spotbugs.

the class FindbugsPlugin method createDefaultEmptyBugCollection.

private static SortedBugCollection createDefaultEmptyBugCollection(IProject project) throws CoreException {
    SortedBugCollection bugCollection = new SortedBugCollection();
    Project fbProject = bugCollection.getProject();
    UserPreferences userPrefs = getUserPreferences(project);
    cacheBugCollectionAndProject(project, bugCollection, fbProject);
    return bugCollection;
}
Also used : UserPreferences(edu.umd.cs.findbugs.config.UserPreferences) IProject(org.eclipse.core.resources.IProject) Project(edu.umd.cs.findbugs.Project) SortedBugCollection(edu.umd.cs.findbugs.SortedBugCollection)

Aggregations

Project (edu.umd.cs.findbugs.Project)35 SortedBugCollection (edu.umd.cs.findbugs.SortedBugCollection)8 File (java.io.File)8 BugInstance (edu.umd.cs.findbugs.BugInstance)6 AnalysisContext (edu.umd.cs.findbugs.ba.AnalysisContext)4 Test (org.junit.jupiter.api.Test)4 DetectorFactoryCollection (edu.umd.cs.findbugs.DetectorFactoryCollection)3 FindBugs2 (edu.umd.cs.findbugs.FindBugs2)3 Plugin (edu.umd.cs.findbugs.Plugin)3 ProjectStats (edu.umd.cs.findbugs.ProjectStats)3 UserPreferences (edu.umd.cs.findbugs.config.UserPreferences)3 Filter (edu.umd.cs.findbugs.filter.Filter)3 GridBagConstraints (java.awt.GridBagConstraints)3 Insets (java.awt.Insets)3 IOException (java.io.IOException)3 Reporter (de.tobject.findbugs.reporter.Reporter)2 StopTimer (de.tobject.findbugs.util.Util.StopTimer)2 ClassAnnotation (edu.umd.cs.findbugs.ClassAnnotation)2 PackageStats (edu.umd.cs.findbugs.PackageStats)2 PluginException (edu.umd.cs.findbugs.PluginException)2