1 18 package edu.umd.cs.findbugs.workflow; 19 20 import java.io.File ; 21 import java.io.FileOutputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 28 import org.apache.bcel.generic.GETSTATIC; 29 import org.dom4j.DocumentException; 30 31 import edu.umd.cs.findbugs.BugAnnotation; 32 import edu.umd.cs.findbugs.BugCollection; 33 import edu.umd.cs.findbugs.BugInstance; 34 import edu.umd.cs.findbugs.ClassAnnotation; 35 import edu.umd.cs.findbugs.DetectorFactoryCollection; 36 import edu.umd.cs.findbugs.PackageMemberAnnotation; 37 import edu.umd.cs.findbugs.Project; 38 import edu.umd.cs.findbugs.SortedBugCollection; 39 import edu.umd.cs.findbugs.SourceLineAnnotation; 40 import edu.umd.cs.findbugs.ba.SourceFinder; 41 42 48 49 public class CopyBuggySource { 50 51 54 private static final String USAGE = "Usage: <cmd> " 55 + " <bugs.xml> <destinationSrcDir>"; 56 57 public static void main(String [] args) throws IOException , 58 DocumentException { 59 60 DetectorFactoryCollection.instance(); 61 if (args.length != 2) { 62 System.out.println(USAGE); 63 return; 64 } 65 66 Project project = new Project(); 67 BugCollection origCollection; 68 origCollection = new SortedBugCollection(); 69 origCollection.readXML(args[0], project); 70 File src = new File (args[1]); 71 byte buf[] = new byte[4096]; 72 if (!src.isDirectory()) 73 throw new IllegalArgumentException (args[1] 74 + " is not a source directory"); 75 SourceFinder sourceFinder = new SourceFinder(); 76 sourceFinder.setSourceBaseList(project.getSourceDirList()); 77 HashSet <String > copied = new HashSet <String >(); 78 for (BugInstance bug : origCollection.getCollection()) { 79 for (Iterator <BugAnnotation> i = bug.annotationIterator(); i 80 .hasNext();) { 81 BugAnnotation ann = i.next(); 82 SourceLineAnnotation sourceAnnotation; 83 if (ann instanceof PackageMemberAnnotation) 84 sourceAnnotation = ((PackageMemberAnnotation) ann) 85 .getSourceLines(); 86 else if (ann instanceof SourceLineAnnotation) 87 sourceAnnotation = (SourceLineAnnotation) ann; 88 else 89 continue; 90 if (sourceAnnotation == null) 91 continue; 92 String fullName; 93 94 String packageName = sourceAnnotation.getPackageName(); 95 String sourceFile = sourceAnnotation.getSourceFile(); 96 if (packageName == "") 97 fullName = sourceFile; 98 else 99 fullName = packageName.replace('.', File.separatorChar) 100 + File.separatorChar + sourceFile; 101 if (copied.add(fullName)) { 102 File file = new File (src, fullName); 103 if (file.exists()) { 104 System.out.println(file + " already exists"); 105 continue; 106 } 107 File parent = file.getParentFile(); 108 parent.mkdirs(); 109 InputStream in = null; 110 OutputStream out = null; 111 try { 112 in = sourceFinder.openSource(packageName, sourceFile); 113 out = new FileOutputStream (file); 114 while (true) { 115 int sz = in.read(buf); 116 if (sz < 0) 117 break; 118 out.write(buf, 0, sz); 119 } 120 System.out.println("Copied " + file); 121 } catch (IOException e) { 122 System.out.println("Problem copying " + file); 123 e.printStackTrace(System.out); 124 } finally { 125 close(in); 126 close(out); 127 } 128 } 129 } 130 } 131 } 132 133 public static void close(InputStream in) { 134 try { 135 if (in != null) 136 in.close(); 137 } catch (IOException e) { 138 } 139 140 } 141 142 public static void close(OutputStream out) { 143 try { 144 if (out != null) 145 out.close(); 146 } catch (IOException e) { 147 } 148 149 } 150 } 151 | Popular Tags |