KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > ant > InstrumentTask


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (C) 2000-2002 The Apache Software Foundation. All rights
5  * reserved.
6  * Copyright (C) 2003 jcoverage ltd.
7  * Copyright (C) 2005 Mark Doliner
8  * Copyright (C) 2005 Joakim Erdfelt
9  * Copyright (C) 2005 Grzegorz Lukasik
10  * Copyright (C) 2005 Alexei Yudichev
11  * Copyright (C) 2006 John Lewis
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowlegement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowlegement may appear in the software itself,
30  * if and wherever such third-party acknowlegements normally appear.
31  *
32  * 4. The names "Ant" and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Group.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  */

60
61 package net.sourceforge.cobertura.ant;
62
63 import java.io.File JavaDoc;
64 import java.io.IOException JavaDoc;
65 import java.util.ArrayList JavaDoc;
66 import java.util.HashMap JavaDoc;
67 import java.util.List JavaDoc;
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 JavaDoc dataFile = null;
80
81     private File JavaDoc toDir = null;
82
83     List JavaDoc ignoreRegexs = new ArrayList JavaDoc();
84     
85     List JavaDoc includeClassesRegexs = new ArrayList JavaDoc();
86
87     List JavaDoc excludeClassesRegexs = new ArrayList JavaDoc();
88
89     private Integer JavaDoc forkedJVMDebugPort;
90     
91     private Path instrumentationClasspath = null;
92
93     private HashMap JavaDoc fileSetMap = new HashMap JavaDoc();
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     /*
130      * TODO: Is the following method needed to use a classpath ref? If so,
131      * test it and uncomment it.
132      */

133     /*
134     public void setInstrumentationClasspathRef(Reference r)
135     {
136         createInstrumentationClasspath().setRefid(r);
137     }
138     */

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 JavaDoc 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         // Execute GPL licensed code in separate virtual machine
177
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 JavaDoc[] sources = instrumentationClasspath.list();
200         for (int i = 0; i < sources.length; i++) {
201             File JavaDoc fileOrDir = new File JavaDoc(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 JavaDoc file)
214     {
215         File JavaDoc dir = file.getParentFile();
216         String JavaDoc filename = file.getName();
217         FileSet fileSet = getFileSet(dir);
218         fileSet.createInclude().setName(filename);
219     }
220
221     private FileSet getFileSet(File JavaDoc dir)
222     {
223         String JavaDoc 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             // Now add the new fileset to the map and to the fileSets list
232
fileSetMap.put(key, fileSet);
233             addFileset(fileSet);
234         }
235         return fileSet;
236     }
237
238     private void createFilesetForDirectory(File JavaDoc dir)
239     {
240         FileSet fileSet = getFileSet(dir);
241         fileSet.createInclude().setName("**/*.class");
242     }
243
244     public void setDataFile(String JavaDoc dataFile)
245     {
246         this.dataFile = dataFile;
247     }
248
249     public void setToDir(File JavaDoc toDir)
250     {
251         this.toDir = toDir;
252     }
253
254     public void setForkedJVMDebugPort(Integer JavaDoc forkedJVMDebugPort)
255     {
256         this.forkedJVMDebugPort = forkedJVMDebugPort;
257     }
258
259 }
260
Popular Tags