📄 bughistory.java
字号:
*/ private static void replaceBugInstances(Set<BugInstance> dest, Collection<BugInstance> source) { dest.clear(); dest.addAll(source); } /** * Remove bug instances from Set. * * @param result the Set * @param toRemove Collection of BugInstances to remove */ private static void removeBugInstances(Set<BugInstance> result, Collection<BugInstance> toRemove) { for (BugInstance aToRemove : toRemove) { result.remove(aToRemove); } } private static final int VERSION_INSENSITIVE_COMPARATOR = 0; private static final int FUZZY_COMPARATOR = 1; private static final int SLOPPY_COMPARATOR = 2; private static class BugHistoryCommandLine extends CommandLine { private int comparatorType = VERSION_INSENSITIVE_COMPARATOR; private boolean count; private String opName; private SetOperation setOp; private String listFile; private String outputDir; private boolean verbose; public BugHistoryCommandLine() { addSwitch("-fuzzy", "use fuzzy warning matching"); addSwitch("-sloppy", "use sloppy warning matching"); addSwitch("-added", "compute added warnings"); addSwitch("-new", "same as \"-added\" switch"); addSwitch("-removed", "compute removed warnings"); addSwitch("-fixed", "same as \"-removed\" switch"); addSwitch("-retained", "compute retained warnings"); addSwitch("-count", "just print warning count"); addOption("-bulk", "file of csv xml file pairs", "bulk mode, output written to v2-OP.xml"); addOption("-outputDir", "output dir", "output directory for bulk mode (optional)"); addSwitch("-verbose", "verbose output for bulk mode"); } /* (non-Javadoc) * @see edu.umd.cs.findbugs.config.CommandLine#handleOption(java.lang.String, java.lang.String) */ @Override protected void handleOption(String option, String optionExtraPart) throws IOException { if (option.equals("-fuzzy")) { comparatorType = FUZZY_COMPARATOR; } else if (option.equals("-sloppy")) { comparatorType = SLOPPY_COMPARATOR; } else if (option.equals("-added") || option.equals("-new")) { opName = option; setOp = ADDED_WARNINGS; } else if (option.equals("-removed") || option.equals("-fixed")) { opName = option; setOp = REMOVED_WARNINGS; } else if (option.equals("-retained")) { opName = option; setOp = RETAINED_WARNINGS; } else if (option.equals("-count")) { count = true; } else if (option.equals("-verbose")) { verbose = true; } else { throw new IllegalArgumentException("Unknown option: " + option); } } /* (non-Javadoc) * @see edu.umd.cs.findbugs.config.CommandLine#handleOptionWithArgument(java.lang.String, java.lang.String) */ @Override protected void handleOptionWithArgument(String option, String argument) throws IOException { if (option.equals("-bulk")) { listFile = argument; } else if (option.equals("-outputDir")) { outputDir = argument; } else { throw new IllegalArgumentException("Unknown option: " + option); } } /** * @return Returns the comparatorType. */ public int getComparatorType() { return comparatorType; } /** * @return true if we should just output the delta */ public boolean isCount() { return count; } /** * @return Returns the opName. */ public String getOpName() { return opName; } /** * @return Returns the set operation to apply. */ public SetOperation getSetOp() { return setOp; } /** * @return Returns the listFile. */ public String getListFile() { return listFile; } /** * @return Returns the outputDir. */ public String getOutputDir() { return outputDir; } /** * @return Returns the verbose. */ public boolean isVerbose() { return verbose; } public void configure(BugHistory bugHistory, SortedBugCollection origCollection, SortedBugCollection newCollection) { // Create comparator WarningComparator comparator; switch (getComparatorType()) { case VERSION_INSENSITIVE_COMPARATOR: comparator = new VersionInsensitiveBugComparator(); break; case FUZZY_COMPARATOR: FuzzyBugComparator fuzzy = new FuzzyBugComparator(); fuzzy.registerBugCollection(origCollection); fuzzy.registerBugCollection(newCollection); comparator = fuzzy; break; case SLOPPY_COMPARATOR: comparator = new SloppyBugComparator(); break; default: throw new IllegalStateException(); } // Handle renamed classes MovedClassMap classNameRewriter = new MovedClassMap(origCollection, newCollection).execute(); comparator.setClassNameRewriter(classNameRewriter); bugHistory.setComparator(comparator); } public BugHistory createAndExecute( String origFile, String newFile, Project origProject, Project newProject) throws IOException, DocumentException { SortedBugCollection origCollection = readCollection(origFile, origProject); SortedBugCollection newCollection = readCollection(newFile, newProject); return createAndExecute(origCollection, newCollection, origProject, newProject); } public BugHistory createAndExecute( SortedBugCollection origCollection, SortedBugCollection newCollection, Project origProject, Project newProject) { BugHistory bugHistory = new BugHistory(origCollection, newCollection); configure(bugHistory, origCollection, newCollection); // We can ignore the return value because it will be accessible by calling getResult() bugHistory.performSetOperation(getSetOp()); return bugHistory; } public String getBulkOutputFileName(String fileName) { File file = new File(fileName); String filePart = file.getName(); int ext = filePart.lastIndexOf('.'); if (ext < 0 ) { filePart = filePart + getOpName(); } else { filePart = filePart.substring(0, ext) + getOpName() + filePart.substring(ext); } String dirPart = (getOutputDir() != null) ? getOutputDir() : file.getParent(); File outputFile = new File(dirPart, filePart); return outputFile.getPath(); } } private static SortedBugCollection readCollection(String fileName, Project project) throws IOException, DocumentException { SortedBugCollection result = new SortedBugCollection(); result.readXML(fileName, project); return result; } public static void main(String[] argv) throws Exception { BugHistoryCommandLine commandLine = new BugHistoryCommandLine(); int argCount = commandLine.parse(argv); if (commandLine.getSetOp() == null) { System.err.println("No set operation specified"); printUsage(); System.exit(1); } if (commandLine.getListFile() != null) { if (argv.length != argCount) { printUsage(); } runBulk(commandLine); } else{ if (argv.length - argCount != 2) { printUsage(); } String origFile = argv[argCount++]; String newFile = argv[argCount++]; runSinglePair(commandLine, origFile, newFile); } } private static void runBulk(BugHistoryCommandLine commandLine) throws FileNotFoundException, IOException, DocumentException { BufferedReader reader; if (commandLine.getListFile().equals("-")) { reader = new BufferedReader(new InputStreamReader(System.in)); } else { reader = new BufferedReader(Util.getFileReader(commandLine.getListFile())); } int missing = 0; try { BugCollectionAndProjectCache cache = new BugCollectionAndProjectCache(); String csvRecord; while ((csvRecord = reader.readLine()) != null) { csvRecord = csvRecord.trim(); String[] tuple = csvRecord.split(","); if (tuple.length < 2) continue; String origFile = tuple[0]; String newFile = tuple[1]; BugCollectionAndProject orig; BugCollectionAndProject next; try { orig = cache.fetch(origFile); next = cache.fetch(newFile); } catch (RuntimeException e) { throw e; } catch (Exception e ) { System.err.println("Warning: error reading bug collection: " + e.toString()); ++missing; continue; } if (commandLine.isVerbose()) { System.out.print("Computing delta from " + origFile + " to " + newFile + "..."); System.out.flush(); } BugHistory bugHistory = commandLine.createAndExecute( orig.getBugCollection(), next.getBugCollection(), orig.getProject(), next.getProject()); String outputFile = commandLine.getBulkOutputFileName(newFile); if (commandLine.isVerbose()) { System.out.print("Writing " + outputFile + "..."); System.out.flush(); } bugHistory.writeResultCollection(orig.getProject(), next.getProject(), new BufferedOutputStream(new FileOutputStream(outputFile))); if (commandLine.isVerbose()) { System.out.println("done"); } } } finally { reader.close(); } if (missing > 0) { System.err.println(missing + " pairs skipped because of missing files"); } } private static void runSinglePair(BugHistoryCommandLine commandLine, String origFile, String newFile) throws IOException, DocumentException { Project origProject = new Project(); Project newProject = new Project(); BugHistory bugHistory = commandLine.createAndExecute(origFile, newFile, origProject, newProject); if (commandLine.isCount()) { System.out.println(bugHistory.getResultCollection().getCollection().size()); } else { OutputStream outputStream = System.out; bugHistory.writeResultCollection(origProject, newProject, outputStream); } } /** * Print usage and exit. */ private static void printUsage() { System.err.println("Usage: " + BugHistory.class.getName() + " [options] <operation> <old results> <new results>"); new BugHistoryCommandLine().printUsage(System.err); System.exit(1); }}// vim:ts=4
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -