1 18 package edu.umd.cs.findbugs.workflow; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.util.Date ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 31 import org.dom4j.DocumentException; 32 33 import edu.umd.cs.findbugs.BugCollection; 34 import edu.umd.cs.findbugs.BugInstance; 35 import edu.umd.cs.findbugs.DetectorFactoryCollection; 36 import edu.umd.cs.findbugs.Project; 37 import edu.umd.cs.findbugs.SortedBugCollection; 38 import edu.umd.cs.findbugs.SourceLineAnnotation; 39 import edu.umd.cs.findbugs.ba.SourceFinder; 40 import edu.umd.cs.findbugs.config.CommandLine; 41 42 48 49 public class SetBugDatabaseInfo { 50 51 54 private static final String USAGE = "Usage: <cmd> " 55 + " [options] [<oldData> [<newData>]]"; 56 57 58 static class SetInfoCommandLine extends CommandLine { 59 String revisionName; 60 61 long revisionTimestamp = 0L; 62 public List <String > sourcePaths = new LinkedList <String >(); 63 public List <String > searchSourcePaths = new LinkedList <String >(); 64 65 SetInfoCommandLine() { 66 addOption("-name", "name", "set name for (last) revision"); 67 addOption("-timestamp", "when", "set timestamp for (last) revision"); 68 addOption("-source", "directory", "Add this directory to the source search path"); 69 addOption("-findSource", "directory", "Find and add all relevant source directions contained within this directory"); 70 } 71 72 @Override 73 protected void handleOption(String option, String optionExtraPart) 74 throws IOException { 75 throw new IllegalArgumentException ("no option " + option); 76 77 } 78 79 @Override 80 protected void handleOptionWithArgument(String option, String argument) 81 throws IOException { 82 if (option.equals("-name")) 83 revisionName = argument; 84 else if (option.equals("-timestamp")) 85 revisionTimestamp = Date.parse(argument); 86 else if (option.equals("-source")) 87 sourcePaths.add(argument); 88 else if (option.equals("-findSource")) 89 searchSourcePaths.add(argument); 90 else 91 throw new IllegalArgumentException ("Can't handle option " 92 + option); 93 94 } 95 96 } 97 98 public static void main(String [] args) throws IOException , 99 DocumentException { 100 101 DetectorFactoryCollection.instance(); 102 SetInfoCommandLine commandLine = new SetInfoCommandLine(); 103 int argCount = commandLine.parse(args, 0, 2, USAGE); 104 105 Project project = new Project(); 106 BugCollection origCollection; 107 origCollection = new SortedBugCollection(); 108 109 if (argCount < args.length) 110 origCollection.readXML(args[argCount++], project); 111 else 112 origCollection.readXML(System.in, project); 113 114 115 if (commandLine.revisionName != null) 116 origCollection.setReleaseName(commandLine.revisionName); 117 if (commandLine.revisionTimestamp != 0) 118 origCollection.setTimestamp(commandLine.revisionTimestamp); 119 for(String source : commandLine.sourcePaths) 120 project.addSourceDir(source); 121 122 Map <String ,Set <String >> missingFiles = new HashMap <String ,Set <String >>(); 123 if (!commandLine.searchSourcePaths.isEmpty()) { 124 sourceSearcher = new SourceSearcher(project); 125 for(BugInstance bug : origCollection.getCollection()) { 126 SourceLineAnnotation src = bug.getPrimarySourceLineAnnotation(); 127 if (!sourceSearcher.sourceNotFound.contains(src.getClassName()) 128 && !sourceSearcher.findSource(src)) { 129 Set <String > paths = missingFiles.get(src.getSourceFile()); 130 if (paths == null) { 131 paths = new HashSet <String >(); 132 missingFiles.put(src.getSourceFile(), paths); 133 } 134 String fullPath = fullPath(src); 135 paths.add(fullPath); 137 } 138 } 139 Set <String > foundPaths = new HashSet <String >(); 140 for(String f : commandLine.searchSourcePaths) 141 for(File javaFile : RecursiveSearchForJavaFiles.search(new File (f))) { 142 Set <String > matchingMissingClasses = missingFiles.get(javaFile.getName()); 143 if (matchingMissingClasses == null) { 144 } else for(String sourcePath : matchingMissingClasses) { 146 String path = javaFile.getAbsolutePath(); 147 if (path.endsWith(sourcePath)) { 148 String dir = path.substring(0,path.length() - sourcePath.length()); 149 if (foundPaths.add(dir)) { 150 project.addSourceDir(dir); 151 if (argCount < args.length) 152 System.out.println("Found " + dir); 153 } 154 } 155 } 156 157 158 } 159 160 161 } 162 163 166 167 168 169 170 if (argCount < args.length) 171 origCollection.writeXML(args[argCount++], project); 172 else 173 origCollection.writeXML(System.out, project); 174 175 } 176 static String fullPath(SourceLineAnnotation src) { 177 return src.getPackageName().replace('.', File.separatorChar) 178 + File.separatorChar + src.getSourceFile(); 179 } 180 static SourceSearcher sourceSearcher; 181 182 183 } 184 | Popular Tags |