Search in sources :

Example 1 with PackageStats

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

use of edu.umd.cs.findbugs.PackageStats 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)

Example 3 with PackageStats

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

the class TreemapVisualization method generateTreeMap.

public void generateTreeMap(BugCollection bugCollection) {
    for (PackageStats p : bugCollection.getProjectStats().getPackageStats()) {
        if (p.getTotalBugs() > 0) {
            buggyPackages.add(p.getPackageName());
            addInteriorPackages(p.getPackageName());
        }
    }
    for (PackageStats p : bugCollection.getProjectStats().getPackageStats()) {
        if (p.getTotalBugs() == 0) {
            cleanCode(p.getPackageName(), p.size(), p.getClassStats().size());
        }
    }
    System.out.println("LOC\tTypes\tH\tHM\tDensity");
    System.out.println("INTEGER\tINTEGER\tINTEGER\tINTEGER\tFLOAT");
    for (PackageStats p : bugCollection.getProjectStats().getPackageStats()) {
        if (p.getTotalBugs() > 0) {
            int high = p.getBugsAtPriority(Priorities.HIGH_PRIORITY);
            int normal = p.getBugsAtPriority(Priorities.NORMAL_PRIORITY);
            System.out.printf("%d\t%d\t%d\t%d\t%g\t\t%s", p.size(), p.getClassStats().size(), high, high + normal, (high + normal) * 1000.0 / p.size(), p.getPackageName().substring(11).replace('.', '\t'));
            if (isInteriorPackage(p.getPackageName())) {
                System.out.print("\t*");
            }
            System.out.println();
        }
    }
    for (Map.Entry<String, Integer> e : goodCodeSize.entrySet()) {
        System.out.printf("%d\t%d\t%d\t%d\t%g\t\t%s%n", e.getValue(), goodCodeCount.getCount(e.getKey()), 0, 0, 0.0, e.getKey().substring(11).replace('.', '\t'));
    }
}
Also used : PackageStats(edu.umd.cs.findbugs.PackageStats) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 4 with PackageStats

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

the class CountByPackagePrefix method main.

public static void main(String[] args) throws IOException, DocumentException {
    DetectorFactoryCollection.instance();
    if (args.length != 1 && args.length != 2) {
        System.out.println(USAGE);
        return;
    }
    int prefixLength = Integer.parseInt(args[0]);
    BugCollection origCollection = new SortedBugCollection();
    if (args.length == 1) {
        origCollection.readXML(System.in);
    } else {
        origCollection.readXML(args[1]);
    }
    Map<String, Integer> map = new TreeMap<>();
    Map<String, Integer> ncss = new TreeMap<>();
    for (BugInstance b : origCollection.getCollection()) {
        String prefix = ClassName.extractPackagePrefix(b.getPrimaryClass().getPackageName(), prefixLength);
        Integer v = map.get(prefix);
        if (v == null) {
            map.put(prefix, 1);
        } else {
            map.put(prefix, v + 1);
        }
    }
    for (PackageStats ps : origCollection.getProjectStats().getPackageStats()) {
        String prefix = ClassName.extractPackagePrefix(ps.getPackageName(), prefixLength);
        Integer v = ncss.get(prefix);
        if (v == null) {
            ncss.put(prefix, ps.size());
        } else {
            ncss.put(prefix, v + ps.size());
        }
    }
    for (Map.Entry<String, Integer> e : map.entrySet()) {
        String prefix = e.getKey();
        int warnings = e.getValue();
        if (warnings == 0) {
            continue;
        }
        Integer v = ncss.get(prefix);
        if (v == null || v.intValue() == 0) {
            v = 1;
        }
        int density = warnings * 1000000 / v;
        if (warnings < 3 || v < 2000) {
            System.out.printf("%4s %4d %4d %s%n", " ", warnings, v / 1000, prefix);
        } else {
            System.out.printf("%4d %4d %4d %s%n", density, warnings, v / 1000, prefix);
        }
    }
}
Also used : SortedBugCollection(edu.umd.cs.findbugs.SortedBugCollection) BugCollection(edu.umd.cs.findbugs.BugCollection) SortedBugCollection(edu.umd.cs.findbugs.SortedBugCollection) PackageStats(edu.umd.cs.findbugs.PackageStats) BugInstance(edu.umd.cs.findbugs.BugInstance) TreeMap(java.util.TreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 5 with PackageStats

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

the class Filter method main.

public static void main(String[] args) throws Exception {
    FindBugs.setNoAnalysis();
    DetectorFactoryCollection.instance();
    final FilterCommandLine commandLine = new FilterCommandLine();
    int argCount = commandLine.parse(args, 0, 2, "Usage: " + Filter.class.getName() + " [options] [<orig results> [<new results>]] ");
    SortedBugCollection origCollection = new SortedBugCollection();
    if (argCount == args.length) {
        origCollection.readXML(System.in);
    } else {
        origCollection.readXML(args[argCount++]);
    }
    boolean verbose = argCount < args.length;
    SortedBugCollection resultCollection = origCollection.createEmptyCollectionWithMetadata();
    Project project = resultCollection.getProject();
    int passed = 0;
    int dropped = 0;
    resultCollection.setWithMessages(commandLine.withMessages);
    if (commandLine.hashChangedSpecified) {
        origCollection.computeBugHashes();
    }
    commandLine.adjustFilter(project, resultCollection);
    ProjectStats projectStats = resultCollection.getProjectStats();
    projectStats.clearBugCounts();
    if (commandLine.classPattern != null) {
        projectStats.purgeClassesThatDontMatch(commandLine.classPattern);
    }
    sourceSearcher = new SourceSearcher(project);
    long trimToVersion = -1;
    if (commandLine.trimToVersionAsString != null) {
        Map<String, AppVersion> versions = new HashMap<>();
        SortedMap<Long, AppVersion> timeStamps = new TreeMap<>();
        for (Iterator<AppVersion> i = origCollection.appVersionIterator(); i.hasNext(); ) {
            AppVersion v = i.next();
            versions.put(v.getReleaseName(), v);
            timeStamps.put(v.getTimestamp(), v);
        }
        // add current version to the maps
        AppVersion v = resultCollection.getCurrentAppVersion();
        versions.put(v.getReleaseName(), v);
        timeStamps.put(v.getTimestamp(), v);
        trimToVersion = edu.umd.cs.findbugs.workflow.Filter.FilterCommandLine.getVersionNum(versions, timeStamps, commandLine.trimToVersionAsString, true, v.getSequenceNumber());
        if (trimToVersion < origCollection.getSequenceNumber()) {
            String name = resultCollection.getAppVersionFromSequenceNumber(trimToVersion).getReleaseName();
            long timestamp = resultCollection.getAppVersionFromSequenceNumber(trimToVersion).getTimestamp();
            resultCollection.setReleaseName(name);
            resultCollection.setTimestamp(timestamp);
            resultCollection.trimAppVersions(trimToVersion);
        }
    }
    commandLine.getReady(origCollection);
    for (BugInstance bug : origCollection.getCollection()) {
        if (commandLine.accept(origCollection, bug)) {
            if (trimToVersion >= 0) {
                if (bug.getFirstVersion() > trimToVersion) {
                    dropped++;
                    continue;
                } else if (bug.getLastVersion() >= trimToVersion) {
                    bug.setLastVersion(-1);
                    bug.setRemovedByChangeOfPersistingClass(false);
                }
            }
            resultCollection.add(bug, false);
            passed++;
        } else {
            dropped++;
        }
    }
    if (commandLine.purgeHistorySpecified && commandLine.purgeHistory) {
        resultCollection.clearAppVersions();
        for (BugInstance bug : resultCollection.getCollection()) {
            bug.clearHistory();
        }
    }
    if (verbose) {
        System.out.println(passed + " warnings passed through, " + dropped + " warnings dropped");
    }
    if (commandLine.withSourceSpecified && commandLine.withSource && !commandLine.dontUpdateStats && projectStats.hasClassStats()) {
        for (PackageStats stats : projectStats.getPackageStats()) {
            Iterator<ClassStats> i = stats.getClassStats().iterator();
            while (i.hasNext()) {
                String className = i.next().getName();
                if (sourceSearcher.sourceNotFound.contains(className) || !sourceSearcher.sourceFound.contains(className) && !sourceSearcher.findSource(SourceLineAnnotation.createReallyUnknown(className))) {
                    i.remove();
                }
            }
        }
    }
    projectStats.recomputeFromComponents();
    if (argCount == args.length) {
        assert !verbose;
        resultCollection.writeXML(System.out);
    } else {
        resultCollection.writeXML(args[argCount++]);
    }
}
Also used : ProjectStats(edu.umd.cs.findbugs.ProjectStats) HashMap(java.util.HashMap) SortedBugCollection(edu.umd.cs.findbugs.SortedBugCollection) BugInstance(edu.umd.cs.findbugs.BugInstance) TreeMap(java.util.TreeMap) AppVersion(edu.umd.cs.findbugs.AppVersion) Project(edu.umd.cs.findbugs.Project) PackageStats(edu.umd.cs.findbugs.PackageStats) ClassStats(edu.umd.cs.findbugs.PackageStats.ClassStats)

Aggregations

PackageStats (edu.umd.cs.findbugs.PackageStats)5 SortedBugCollection (edu.umd.cs.findbugs.SortedBugCollection)4 BugInstance (edu.umd.cs.findbugs.BugInstance)3 TreeMap (java.util.TreeMap)3 BugCollection (edu.umd.cs.findbugs.BugCollection)2 ClassStats (edu.umd.cs.findbugs.PackageStats.ClassStats)2 Project (edu.umd.cs.findbugs.Project)2 ProjectStats (edu.umd.cs.findbugs.ProjectStats)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AppVersion (edu.umd.cs.findbugs.AppVersion)1 SourceLineAnnotation (edu.umd.cs.findbugs.SourceLineAnnotation)1 File (java.io.File)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1