KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > workflow > SetBugDatabaseInfo


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003-2005 William Pugh
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package edu.umd.cs.findbugs.workflow;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
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 /**
43  * Java main application to compute update a historical bug collection with
44  * results from another build/analysis.
45  *
46  * @author William Pugh
47  */

48
49 public class SetBugDatabaseInfo {
50
51     /**
52      *
53      */

54     private static final String JavaDoc USAGE = "Usage: <cmd> "
55             + " [options] [<oldData> [<newData>]]";
56
57     
58     static class SetInfoCommandLine extends CommandLine {
59         String JavaDoc revisionName;
60         
61         long revisionTimestamp = 0L;
62         public List JavaDoc<String JavaDoc> sourcePaths = new LinkedList JavaDoc<String JavaDoc>();
63         public List JavaDoc<String JavaDoc> searchSourcePaths = new LinkedList JavaDoc<String JavaDoc>();
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 JavaDoc
73         protected void handleOption(String JavaDoc option, String JavaDoc optionExtraPart)
74                 throws IOException JavaDoc {
75             throw new IllegalArgumentException JavaDoc("no option " + option);
76
77         }
78
79         @Override JavaDoc
80         protected void handleOptionWithArgument(String JavaDoc option, String JavaDoc argument)
81                 throws IOException JavaDoc {
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 JavaDoc("Can't handle option "
92                         + option);
93
94         }
95
96     }
97
98     public static void main(String JavaDoc[] args) throws IOException JavaDoc,
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 JavaDoc source : commandLine.sourcePaths)
120             project.addSourceDir(source);
121
122         Map JavaDoc<String JavaDoc,Set JavaDoc<String JavaDoc>> missingFiles = new HashMap JavaDoc<String JavaDoc,Set JavaDoc<String JavaDoc>>();
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 JavaDoc<String JavaDoc> paths = missingFiles.get(src.getSourceFile());
130                     if (paths == null) {
131                         paths = new HashSet JavaDoc<String JavaDoc>();
132                         missingFiles.put(src.getSourceFile(), paths);
133                     }
134                     String JavaDoc fullPath = fullPath(src);
135                     // System.out.println("Missing " + fullPath);
136
paths.add(fullPath);
137                 }
138             }
139             Set JavaDoc<String JavaDoc> foundPaths = new HashSet JavaDoc<String JavaDoc>();
140             for(String JavaDoc f : commandLine.searchSourcePaths)
141                 for(File JavaDoc javaFile : RecursiveSearchForJavaFiles.search(new File JavaDoc(f))) {
142                     Set JavaDoc<String JavaDoc> matchingMissingClasses = missingFiles.get(javaFile.getName());
143                     if (matchingMissingClasses == null) {
144                         // System.out.println("Nothing for " + javaFile);
145
} else for(String JavaDoc sourcePath : matchingMissingClasses) {
146                         String JavaDoc path = javaFile.getAbsolutePath();
147                         if (path.endsWith(sourcePath)) {
148                             String JavaDoc 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             // OK, now we know all the missing source files
164
// we also know all the .java files in the directories we were pointed to
165

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 JavaDoc 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