1 60 61 package net.sourceforge.cobertura.ant; 62 63 import java.io.File ; 64 import java.io.IOException ; 65 import java.util.ArrayList ; 66 import java.util.HashMap ; 67 import java.util.List ; 68 69 import net.sourceforge.cobertura.util.CommandLineBuilder; 70 71 import org.apache.tools.ant.BuildException; 72 import org.apache.tools.ant.Project; 73 import org.apache.tools.ant.types.FileSet; 74 import org.apache.tools.ant.types.Path; 75 76 public class InstrumentTask extends CommonMatchingTask 77 { 78 79 private String dataFile = null; 80 81 private File toDir = null; 82 83 List ignoreRegexs = new ArrayList (); 84 85 List includeClassesRegexs = new ArrayList (); 86 87 List excludeClassesRegexs = new ArrayList (); 88 89 private Integer forkedJVMDebugPort; 90 91 private Path instrumentationClasspath = null; 92 93 private HashMap fileSetMap = new HashMap (); 94 95 public InstrumentTask() 96 { 97 super("net.sourceforge.cobertura.instrument.Main"); 98 } 99 100 public Ignore createIgnore() 101 { 102 Ignore ignoreRegex = new Ignore(); 103 ignoreRegexs.add(ignoreRegex); 104 return ignoreRegex; 105 } 106 107 public IncludeClasses createIncludeClasses() 108 { 109 IncludeClasses includeClassesRegex = new IncludeClasses(); 110 includeClassesRegexs.add(includeClassesRegex); 111 return includeClassesRegex; 112 } 113 114 public ExcludeClasses createExcludeClasses() 115 { 116 ExcludeClasses excludeClassesRegex = new ExcludeClasses(); 117 excludeClassesRegexs.add(excludeClassesRegex); 118 return excludeClassesRegex; 119 } 120 121 public Path createInstrumentationClasspath() 122 { 123 if (instrumentationClasspath == null) { 124 instrumentationClasspath = new Path(getProject()); 125 } 126 return instrumentationClasspath.createPath(); 127 } 128 129 133 139 140 public void execute() throws BuildException 141 { 142 CommandLineBuilder builder = null; 143 try { 144 builder = new CommandLineBuilder(); 145 if (dataFile != null) 146 builder.addArg("--datafile", dataFile); 147 if (toDir != null) 148 builder.addArg("--destination", toDir.getAbsolutePath()); 149 150 for (int i = 0; i < ignoreRegexs.size(); i++) { 151 Ignore ignoreRegex = (Ignore)ignoreRegexs.get(i); 152 builder.addArg("--ignore", ignoreRegex.getRegex()); 153 } 154 155 for (int i = 0; i < includeClassesRegexs.size(); i++) { 156 IncludeClasses includeClassesRegex = (IncludeClasses)includeClassesRegexs.get(i); 157 builder.addArg("--includeClasses", includeClassesRegex.getRegex()); 158 } 159 160 for (int i = 0; i < excludeClassesRegexs.size(); i++) { 161 ExcludeClasses excludeClassesRegex = (ExcludeClasses)excludeClassesRegexs.get(i); 162 builder.addArg("--excludeClasses", excludeClassesRegex.getRegex()); 163 } 164 165 if (instrumentationClasspath != null) { 166 processInstrumentationClasspath(); 167 } 168 createArgumentsForFilesets(builder); 169 170 builder.saveArgs(); 171 } catch (IOException ioe) { 172 getProject().log("Error creating commands file.", Project.MSG_ERR); 173 throw new BuildException("Unable to create the commands file.", ioe); 174 } 175 176 getJava().createArg().setValue("--commandsfile"); 178 getJava().createArg().setValue(builder.getCommandLineFile()); 179 if (forkedJVMDebugPort != null && forkedJVMDebugPort.intValue() > 0) { 180 getJava().createJvmarg().setValue("-Xdebug"); 181 getJava().createJvmarg().setValue("-Xrunjdwp:transport=dt_socket,address=" + forkedJVMDebugPort + ",server=y,suspend=y"); 182 } 183 AntUtil.transferCoberturaDataFileProperty(getJava()); 184 if (getJava().executeJava() != 0) { 185 throw new BuildException( 186 "Error instrumenting classes. See messages above."); 187 } 188 189 builder.dispose(); 190 } 191 192 private void processInstrumentationClasspath() 193 { 194 if (includeClassesRegexs.size() == 0) 195 { 196 throw new BuildException("'includeClasses' is required when 'instrumentationClasspath' is used"); 197 } 198 199 String [] sources = instrumentationClasspath.list(); 200 for (int i = 0; i < sources.length; i++) { 201 File fileOrDir = new File (sources[i]); 202 if (fileOrDir.exists()) 203 { 204 if (fileOrDir.isDirectory()) { 205 createFilesetForDirectory(fileOrDir); 206 } else { 207 addFileToFilesets(fileOrDir); 208 } 209 } 210 } 211 } 212 213 private void addFileToFilesets(File file) 214 { 215 File dir = file.getParentFile(); 216 String filename = file.getName(); 217 FileSet fileSet = getFileSet(dir); 218 fileSet.createInclude().setName(filename); 219 } 220 221 private FileSet getFileSet(File dir) 222 { 223 String key = dir.getAbsolutePath(); 224 FileSet fileSet = (FileSet)fileSetMap.get(key); 225 if (fileSet == null) 226 { 227 fileSet = new FileSet(); 228 fileSet.setProject(getProject()); 229 fileSet.setDir(dir); 230 231 fileSetMap.put(key, fileSet); 233 addFileset(fileSet); 234 } 235 return fileSet; 236 } 237 238 private void createFilesetForDirectory(File dir) 239 { 240 FileSet fileSet = getFileSet(dir); 241 fileSet.createInclude().setName("**/*.class"); 242 } 243 244 public void setDataFile(String dataFile) 245 { 246 this.dataFile = dataFile; 247 } 248 249 public void setToDir(File toDir) 250 { 251 this.toDir = toDir; 252 } 253 254 public void setForkedJVMDebugPort(Integer forkedJVMDebugPort) 255 { 256 this.forkedJVMDebugPort = forkedJVMDebugPort; 257 } 258 259 } 260 | Popular Tags |