1 19 20 package edu.umd.cs.findbugs.gui2; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.net.URL ; 27 import java.util.ArrayList ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 import javax.swing.JFileChooser ; 33 import javax.swing.JOptionPane ; 34 35 import org.dom4j.DocumentException; 36 37 import edu.umd.cs.findbugs.AbstractBugReporter; 38 import edu.umd.cs.findbugs.BugCollection; 39 import edu.umd.cs.findbugs.BugInstance; 40 import edu.umd.cs.findbugs.BugReporter; 41 import edu.umd.cs.findbugs.BugReporterObserver; 42 import edu.umd.cs.findbugs.DetectorFactoryCollection; 43 import edu.umd.cs.findbugs.FindBugs; 44 import edu.umd.cs.findbugs.FindBugs2; 45 import edu.umd.cs.findbugs.FindBugsProgress; 46 import edu.umd.cs.findbugs.IFindBugsEngine; 47 import edu.umd.cs.findbugs.Priorities; 48 import edu.umd.cs.findbugs.Project; 49 import edu.umd.cs.findbugs.ProjectStats; 50 import edu.umd.cs.findbugs.SortedBugCollection; 51 import edu.umd.cs.findbugs.annotations.NonNull; 52 import edu.umd.cs.findbugs.ba.JavaClassAndMethod; 53 import edu.umd.cs.findbugs.ba.MissingClassException; 54 import edu.umd.cs.findbugs.ba.SourceFinder; 55 import edu.umd.cs.findbugs.classfile.ClassDescriptor; 56 import edu.umd.cs.findbugs.classfile.MethodDescriptor; 57 import edu.umd.cs.findbugs.config.UserPreferences; 58 import edu.umd.cs.findbugs.workflow.Update; 59 60 65 public class BugLoader { 66 67 public static BugCollection mainBugCollection = null; 69 private static Project loadedProject; 70 71 77 private static class PrintCallBack implements edu.umd.cs.findbugs.BugReporter 78 { 79 FindBugs fb; 80 ArrayList <BugLeafNode> bugs; 81 ArrayList <BugReporterObserver> bros; 82 ArrayList <String > errorLog; 83 boolean done; 84 int priorityThreadhold = Priorities.LOW_PRIORITY; 85 86 PrintCallBack() 87 { 88 bros=new ArrayList <BugReporterObserver>(); 89 errorLog=new ArrayList <String >(); 90 bugs=new ArrayList <BugLeafNode>(); 91 done=false; 92 } 93 94 public void setEngine(FindBugs engine) { 95 if (done==true) 96 { 97 throw new IllegalStateException ("This PrintCallBack has already finished its analysis, create a new callback if you want another analysis done"); 98 } 99 if (fb!=null) 100 { 101 throw new IllegalStateException ("The engine has already been set, but the analysis was not finished, create a new callback instead of running multiple analyses from this one"); 102 } 103 fb=engine; 104 } 106 107 public void setErrorVerbosity(int level) { 108 } 110 111 public void setPriorityThreshold(int threshold) { 112 priorityThreadhold = Math.min(Priorities.EXP_PRIORITY, threshold); 113 } 114 115 public void reportBug(BugInstance bugInstance) { 116 if (bugInstance.getPriority() > priorityThreadhold) return; 117 BugLeafNode b = new BugLeafNode(bugInstance); 118 bugs.add(b); 119 } 120 121 public void finish() { 122 for (String s: errorLog) 124 { 125 System.err.println(s); 126 } 127 done=true; 128 } 129 130 public void reportQueuedErrors() { 131 } 134 135 public void addObserver(BugReporterObserver observer) { 136 bros.add(observer); 137 } 138 139 public ProjectStats getProjectStats() { 140 return null; 142 } 143 144 public BugReporter getRealBugReporter() { 145 return this; 146 } 147 148 public void reportMissingClass(ClassNotFoundException ex) { 149 Debug.println(ex); 150 } 151 152 public void reportSkippedAnalysis(JavaClassAndMethod method) { 153 } 155 156 public void logError(String message) { 157 errorLog.add(message); 159 } 160 161 public void logError(String message, Throwable e) { 162 if (e instanceof MissingClassException) { 163 reportMissingClass(((MissingClassException)e).getClassNotFoundException()); 164 return; 165 } 166 errorLog.add(message); 169 } 170 171 public void observeClass(ClassDescriptor classDescriptor) { 172 } 174 175 public BugSet getBugs() 176 { 177 return new BugSet(bugs); 178 } 179 180 public void reportSkippedAnalysis(MethodDescriptor method) { 181 } 183 184 187 public void reportMissingClass(ClassDescriptor classDescriptor) { 188 190 } 191 192 193 } 194 195 202 public static BugSet doAnalysis(@NonNull Project p, FindBugsProgress progressCallback) 203 { 204 PrintCallBack pcb=new PrintCallBack(); 205 IFindBugsEngine fb=createEngine(p, pcb); 206 fb.setUserPreferences(UserPreferences.getUserPreferences()); 207 fb.setProgressCallback(progressCallback); 208 try { 209 fb.execute(); 210 List <String > possibleDirectories=p.getSourceDirList(); 211 MainFrame.getInstance().setSourceFinder(new SourceFinder()); 213 MainFrame.getInstance().getSourceFinder().setSourceBaseList(possibleDirectories); 214 215 return pcb.getBugs(); 216 } catch (IOException e) { 217 Debug.println(e); 218 } catch (InterruptedException e) { 219 } 221 222 223 return null; 224 225 } 226 227 235 private static IFindBugsEngine createEngine(@NonNull Project p, PrintCallBack pcb) 236 { 237 if (false) { 238 return new FindBugs(pcb, p); 240 } else { 241 FindBugs2 engine = new FindBugs2(); 242 engine.setBugReporter(pcb); 243 engine.setProject(p); 244 245 engine.setDetectorFactoryCollection(DetectorFactoryCollection.instance()); 246 247 return engine; 248 } 249 } 250 251 public static BugSet loadBugsHelper(BugCollection collection) 252 { 253 mainBugCollection = collection; 254 ArrayList <BugLeafNode> bugList=new ArrayList <BugLeafNode>(); 255 Iterator <BugInstance> i=collection.iterator(); 256 while (i.hasNext()) 257 { 258 bugList.add(new BugLeafNode(i.next())); 259 } 260 261 return new BugSet(bugList); 262 263 } 264 265 public static BugSet loadBugs(Project project, URL url){ 266 try { 267 return loadBugs(project, url.openConnection().getInputStream()); 268 } catch (IOException e) { 269 JOptionPane.showMessageDialog(null,"This file contains no bug data"); 270 } 271 return null; 272 } 273 public static BugSet loadBugs(Project project, File file){ 274 try { 275 return loadBugs(project, new FileInputStream (file)); 276 } catch (IOException e) { 277 JOptionPane.showMessageDialog(null,"This file contains no bug data"); 278 } 279 return null; 280 } 281 public static BugSet loadBugs(Project project, InputStream in) 282 { 283 try 284 { 285 SortedBugCollection col=new SortedBugCollection(); 286 col.readXML(in, project); 287 List <String > possibleDirectories=project.getSourceDirList(); 288 MainFrame.getInstance().setSourceFinder(new SourceFinder()); 289 MainFrame.getInstance().getSourceFinder().setSourceBaseList(possibleDirectories); 290 291 return loadBugsHelper(col); 292 } catch (IOException e) { 293 JOptionPane.showMessageDialog(null,"This file contains no bug data"); 294 } catch (DocumentException e) { 295 JOptionPane.showMessageDialog(null,"This file does not have the correct format; it may be corrupt."); 296 } 297 return null; 298 } 299 300 301 public static Project getLoadedProject() 302 { 303 return loadedProject; 304 } 305 306 private BugLoader() 307 { 308 } 310 311 318 public static BugSet combineBugHistories() 319 { 320 try 321 { 322 FBFileChooser chooser=new FBFileChooser(); 323 chooser.setFileFilter(new FindBugsAnalysisFileFilter()); 324 chooser.setMultiSelectionEnabled(true); 326 chooser.setDialogTitle(edu.umd.cs.findbugs.L10N.getLocalString("dlg.choose_xmls_ttl", "Choose All XML's To Combine")); 327 if (chooser.showOpenDialog(MainFrame.getInstance())==JFileChooser.CANCEL_OPTION) 328 return null; 329 330 loadedProject=new Project(); 331 SortedBugCollection conglomeration=new SortedBugCollection(); 332 conglomeration.readXML(chooser.getSelectedFiles()[0],loadedProject); 333 Update update = new Update(); 334 for (int x=1; x<chooser.getSelectedFiles().length;x++) 335 { 336 File f=chooser.getSelectedFiles()[x]; 337 Project p=new Project(); 338 SortedBugCollection col=new SortedBugCollection(); 339 col.readXML(f,p); 340 conglomeration=(SortedBugCollection) update.mergeCollections(conglomeration, col, false); } 342 343 return loadBugsHelper(conglomeration); 344 } catch (IOException e) { 345 Debug.println(e); 346 return null; 347 } catch (DocumentException e) { 348 Debug.println(e); 349 return null; 350 } 351 352 } 353 354 359 public static BugSet redoAnalysisKeepComments(@NonNull Project p) 360 { 361 BugSet oldSet=BugSet.getMainBugSet(); 362 SortedBugCollection current=new SortedBugCollection(); 363 364 for (BugLeafNode node: oldSet) 365 { 366 BugInstance bug=node.getBug(); 367 current.add(bug); 368 } 369 Update update = new Update(); 370 final SortedBugCollection justAnalyzed=new SortedBugCollection(); 371 372 RedoAnalysisCallback ac= new RedoAnalysisCallback(justAnalyzed); 373 374 new AnalyzingDialog(p,ac,true); 375 376 if (ac.finished) 377 return loadBugsHelper(update.mergeCollections(current, justAnalyzed, false)); 378 else 379 return null; 380 381 } 382 383 384 private static class RedoAnalysisCallback implements AnalysisCallback 385 { 386 public RedoAnalysisCallback(SortedBugCollection collection) 387 { 388 justAnalyzed=collection; 389 } 390 391 SortedBugCollection justAnalyzed; 392 boolean finished; 393 public void analysisFinished(BugSet b) 394 { 395 for(BugLeafNode node: b) 396 { 397 BugInstance bug=node.getBug(); 398 justAnalyzed.add(bug); 399 } 400 finished=true; 401 } 402 403 public void analysisInterrupted() 404 { 405 finished=false; 406 } 407 } 408 } | Popular Tags |