Search in sources :

Example 1 with SortedBugCollection

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

the class BugLoader method loadBugs.

@CheckForNull
public static SortedBugCollection loadBugs(MainFrame mainFrame, Project project, URL url) {
    SortedBugCollection col = new SortedBugCollection(project);
    try {
        if (MainFrame.GUI2_DEBUG) {
            System.out.println("loading from: " + url);
            JOptionPane.showMessageDialog(mainFrame, "loading from: " + url);
        }
        col.readXML(url);
        if (MainFrame.GUI2_DEBUG) {
            System.out.println("finished reading: " + url);
            JOptionPane.showMessageDialog(mainFrame, "loaded: " + url);
        }
        addDeadBugMatcher(col);
    } catch (Throwable e) {
        String msg = SystemProperties.getOSDependentProperty("findbugs.unableToLoadViaURL");
        if (msg == null) {
            msg = e.getMessage();
        } else {
            try {
                msg = String.format(msg, url);
            } catch (Exception e2) {
                msg = e.getMessage();
            }
        }
        JOptionPane.showMessageDialog(mainFrame, "Could not read " + url + "\n" + msg);
        if (SystemProperties.getBoolean("findbugs.failIfUnableToLoadViaURL")) {
            System.exit(1);
        }
    }
    MainFrame.getInstance().setProjectAndBugCollectionInSwingThread(project, col);
    return col;
}
Also used : SortedBugCollection(edu.umd.cs.findbugs.SortedBugCollection) DocumentException(org.dom4j.DocumentException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) CheckForNull(javax.annotation.CheckForNull)

Example 2 with SortedBugCollection

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

the class BugLoader method loadBugs.

@CheckForNull
public static SortedBugCollection loadBugs(MainFrame mainFrame, Project project, File source) {
    if (!source.isFile() || !source.canRead()) {
        JOptionPane.showMessageDialog(mainFrame, "Unable to read " + source);
        return null;
    }
    SortedBugCollection col = new SortedBugCollection(project);
    try {
        col.readXML(source);
        if (col.hasDeadBugs()) {
            addDeadBugMatcher(col);
        }
    } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(mainFrame, "Could not read " + source + "; " + e.getMessage());
    }
    MainFrame.getInstance().setProjectAndBugCollectionInSwingThread(project, col);
    return col;
}
Also used : SortedBugCollection(edu.umd.cs.findbugs.SortedBugCollection) DocumentException(org.dom4j.DocumentException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) CheckForNull(javax.annotation.CheckForNull)

Example 3 with SortedBugCollection

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

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

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

the class DefectDensity method main.

public static void main(String[] args) throws Exception {
    if (args.length > 1 || (args.length > 0 && "-help".equals(args[0]))) {
        System.err.println("Usage: " + DefectDensity.class.getName() + " [<infile>]");
        System.exit(1);
    }
    FindBugs.setNoAnalysis();
    BugCollection origCollection = new SortedBugCollection();
    int argCount = 0;
    if (argCount == args.length) {
        origCollection.readXML(System.in);
    } else {
        origCollection.readXML(args[argCount]);
    }
    ProjectStats stats = origCollection.getProjectStats();
    printRow("kind", "name", "density/KNCSS", "bugs", "NCSS");
    double projectDensity = density(stats.getTotalBugs(), stats.getCodeSize());
    printRow("project", origCollection.getCurrentAppVersion().getReleaseName(), projectDensity, stats.getTotalBugs(), stats.getCodeSize());
    for (PackageStats p : stats.getPackageStats()) {
        if (p.getTotalBugs() > 4) {
            double packageDensity = density(p.getTotalBugs(), p.size());
            if (Double.isNaN(packageDensity) || packageDensity < projectDensity) {
                continue;
            }
            printRow("package", p.getPackageName(), packageDensity, p.getTotalBugs(), p.size());
            for (ClassStats c : p.getSortedClassStats()) {
                if (c.getTotalBugs() > 4) {
                    double density = density(c.getTotalBugs(), c.size());
                    if (Double.isNaN(density) || density < packageDensity) {
                        continue;
                    }
                    printRow("class", c.getName(), density, c.getTotalBugs(), c.size());
                }
            }
        }
    }
}
Also used : SortedBugCollection(edu.umd.cs.findbugs.SortedBugCollection) BugCollection(edu.umd.cs.findbugs.BugCollection) ProjectStats(edu.umd.cs.findbugs.ProjectStats) SortedBugCollection(edu.umd.cs.findbugs.SortedBugCollection) PackageStats(edu.umd.cs.findbugs.PackageStats) ClassStats(edu.umd.cs.findbugs.PackageStats.ClassStats)

Aggregations

SortedBugCollection (edu.umd.cs.findbugs.SortedBugCollection)40 BugInstance (edu.umd.cs.findbugs.BugInstance)9 IOException (java.io.IOException)8 Project (edu.umd.cs.findbugs.Project)7 DocumentException (org.dom4j.DocumentException)7 BugCollection (edu.umd.cs.findbugs.BugCollection)5 ProjectStats (edu.umd.cs.findbugs.ProjectStats)5 Test (org.junit.Test)5 AbstractIntegrationTest (edu.umd.cs.findbugs.AbstractIntegrationTest)4 PackageStats (edu.umd.cs.findbugs.PackageStats)4 File (java.io.File)4 BugInstanceMatcher (edu.umd.cs.findbugs.test.matcher.BugInstanceMatcher)3 BugInstanceMatcherBuilder (edu.umd.cs.findbugs.test.matcher.BugInstanceMatcherBuilder)3 ArrayList (java.util.ArrayList)3 AppVersion (edu.umd.cs.findbugs.AppVersion)2 ClassStats (edu.umd.cs.findbugs.PackageStats.ClassStats)2 UserPreferences (edu.umd.cs.findbugs.config.UserPreferences)2 Update (edu.umd.cs.findbugs.workflow.Update)2 FileOutputStream (java.io.FileOutputStream)2 PrintStream (java.io.PrintStream)2