Java Code Examples for edu.umd.cs.findbugs.Project#addSourceDirs()

The following examples show how to use edu.umd.cs.findbugs.Project#addSourceDirs() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SourceMatcherTest.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void testRealPathMatchWithRegexpAndProject() 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));

    // add test project to SourceLineAnnotation
    SourceLineAnnotation.generateRelativeSource(new File(sourceDir), testProject);

    // regexp match source folder with project
    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));
}
 
Example 2
Source File: SourceMatcherTest.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@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));
}
 
Example 3
Source File: MergeSummarizeAndView.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
static SortedBugCollection createPreconfiguredBugCollection(List<String> workingDirList, List<String> srcDirList,
        IGuiCallback guiCallback) {
    Project project = new Project();
    for (String cwd : workingDirList) {
        project.addWorkingDir(cwd);
    }
    project.addSourceDirs(srcDirList);
    project.setGuiCallback(guiCallback);
    return new SortedBugCollection(project);
}
 
Example 4
Source File: FindBugsParser.java    From analysis-model with MIT License 5 votes vote down vote up
private Report convertBugsToIssues(final Collection<String> sources, final IssueBuilder builder,
        final Map<String, String> hashToMessageMapping, final Map<String, String> categories,
        final SortedBugCollection collection, final Project project) {
    project.addSourceDirs(sources);

    try (SourceFinder sourceFinder = new SourceFinder(project)) {
        if (StringUtils.isNotBlank(project.getProjectName())) {
            builder.setModuleName(project.getProjectName());
        }

        Collection<BugInstance> bugs = collection.getCollection();

        Report report = new Report();
        for (BugInstance warning : bugs) {
            SourceLineAnnotation sourceLine = warning.getPrimarySourceLineAnnotation();

            String message = warning.getMessage();
            String type = warning.getType();
            String category = categories.get(type);
            if (category == null) { // alternately, only if warning.getBugPattern().getType().equals("UNKNOWN")
                category = warning.getBugPattern().getCategory();
            }
            builder.setSeverity(getPriority(warning))
                    .setMessage(createMessage(hashToMessageMapping, warning, message))
                    .setCategory(category)
                    .setType(type)
                    .setLineStart(sourceLine.getStartLine())
                    .setLineEnd(sourceLine.getEndLine())
                    .setFileName(findSourceFile(sourceFinder, sourceLine))
                    .setPackageName(warning.getPrimaryClass().getPackageName())
                    .setFingerprint(warning.getInstanceHash());
            setAffectedLines(warning, builder,
                    new LineRange(sourceLine.getStartLine(), sourceLine.getEndLine()));

            report.add(builder.build());
        }
        return report;
    }
}
 
Example 5
Source File: SpotBugsProcessor.java    From sputnik with Apache License 2.0 5 votes vote down vote up
@NotNull
private Project createProject(@NotNull Review review) {
    Project project = new Project();
    for (String buildDir : BuildDirLocatorFactory.create(config).getBuildDirs(review)) {
        project.addFile(buildDir);
    }
    project.addSourceDirs(review.getSourceDirs());
    return project;
}
 
Example 6
Source File: FindBugsWorker.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void configureSourceDirectories(Project findBugsProject, Map<IPath, IPath> outLocations) {
    Set<IPath> srcDirs = outLocations.keySet();
    findBugsProject.addSourceDirs(srcDirs.stream().map(IPath::toOSString).collect(Collectors.toList()));
}