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));
}
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);
}
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++]);
}
}
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);
}
}
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;
}
Aggregations