• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
3  * This program and the accompanying materials are made available under
4  * the terms of the Eclipse Public License 2.0 which is available at
5  * http://www.eclipse.org/legal/epl-2.0
6  *
7  * SPDX-License-Identifier: EPL-2.0
8  *
9  * Contributors:
10  *    Brock Janiczak - initial API and implementation
11  *
12  *******************************************************************************/
13 package org.jacoco.ant;
14 
15 import java.io.File;
16 import java.io.IOException;
17 
18 import org.apache.tools.ant.BuildException;
19 import org.apache.tools.ant.Task;
20 import org.jacoco.agent.AgentJar;
21 import org.jacoco.core.runtime.AgentOptions;
22 import org.jacoco.core.runtime.AgentOptions.OutputMode;
23 
24 /**
25  * Base class for all coverage tasks that require agent options
26  */
27 public class AbstractCoverageTask extends Task {
28 
29 	private final AgentOptions agentOptions;
30 
31 	private File destfile;
32 
33 	private boolean enabled;
34 
35 	/**
36 	 * Create default agent options
37 	 */
AbstractCoverageTask()38 	protected AbstractCoverageTask() {
39 		super();
40 		agentOptions = new AgentOptions();
41 		destfile = new File(AgentOptions.DEFAULT_DESTFILE);
42 		enabled = true;
43 	}
44 
45 	/**
46 	 * @return Whether or not the current task is enabled
47 	 */
isEnabled()48 	public boolean isEnabled() {
49 		return enabled;
50 	}
51 
52 	/**
53 	 * Sets whether or not the current task is enabled
54 	 *
55 	 * @param enabled
56 	 *            Enablement state of the task
57 	 */
setEnabled(final boolean enabled)58 	public void setEnabled(final boolean enabled) {
59 		this.enabled = enabled;
60 	}
61 
62 	/**
63 	 * Sets the location to write coverage execution data to. Default is
64 	 * <code>jacoco.exec</code>.
65 	 *
66 	 * @param file
67 	 *            Location to write coverage execution data to
68 	 */
setDestfile(final File file)69 	public void setDestfile(final File file) {
70 		destfile = file;
71 	}
72 
73 	/**
74 	 * Append execution coverage data if a coverage file is already present.
75 	 * Default is <code>true</code>
76 	 *
77 	 * @param append
78 	 *            <code>true</code> to append execution data to an existing file
79 	 */
setAppend(final boolean append)80 	public void setAppend(final boolean append) {
81 		agentOptions.setAppend(append);
82 	}
83 
84 	/**
85 	 * List of wildcard patterns classes to include for instrumentation. Default
86 	 * is <code>*</code>
87 	 *
88 	 * @param includes
89 	 *            Wildcard pattern of included classes
90 	 */
setIncludes(final String includes)91 	public void setIncludes(final String includes) {
92 		agentOptions.setIncludes(includes);
93 	}
94 
95 	/**
96 	 * List of wildcard patterns classes to exclude from instrumentation.
97 	 * Default is the empty string, no classes excluded
98 	 *
99 	 * @param excludes
100 	 *            Wildcard pattern of excluded classes
101 	 */
setExcludes(final String excludes)102 	public void setExcludes(final String excludes) {
103 		agentOptions.setExcludes(excludes);
104 	}
105 
106 	/**
107 	 * List of wildcard patterns for classloaders that JaCoCo will not
108 	 * instrument classes from. Default is
109 	 * <code>sun.reflect.DelegatingClassLoader</code>
110 	 *
111 	 * @param exclClassLoader
112 	 *            Wildcard pattern of class loaders to exclude
113 	 */
setExclClassLoader(final String exclClassLoader)114 	public void setExclClassLoader(final String exclClassLoader) {
115 		agentOptions.setExclClassloader(exclClassLoader);
116 	}
117 
118 	/**
119 	 * Sets whether classes from the bootstrap classloader should be
120 	 * instrumented.
121 	 *
122 	 * @param include
123 	 *            <code>true</code> if bootstrap classes should be instrumented
124 	 */
setInclBootstrapClasses(final boolean include)125 	public void setInclBootstrapClasses(final boolean include) {
126 		agentOptions.setInclBootstrapClasses(include);
127 	}
128 
129 	/**
130 	 * Sets whether classes without source location should be instrumented.
131 	 *
132 	 * @param include
133 	 *            <code>true</code> if classes without source location should be
134 	 *            instrumented
135 	 */
setInclNoLocationClasses(final boolean include)136 	public void setInclNoLocationClasses(final boolean include) {
137 		agentOptions.setInclNoLocationClasses(include);
138 	}
139 
140 	/**
141 	 * Sets the session identifier. Default is a auto-generated id
142 	 *
143 	 * @param id
144 	 *            session identifier
145 	 */
setSessionId(final String id)146 	public void setSessionId(final String id) {
147 		agentOptions.setSessionId(id);
148 	}
149 
150 	/**
151 	 * Dump coverage data on VM termination. Default is <code>true</code>
152 	 *
153 	 * @param dumpOnExit
154 	 *            <code>true</code> to write coverage data on VM termination
155 	 */
setDumpOnExit(final boolean dumpOnExit)156 	public void setDumpOnExit(final boolean dumpOnExit) {
157 		agentOptions.setDumpOnExit(dumpOnExit);
158 	}
159 
160 	/**
161 	 * Sets the output method. Default is <code>file</code>
162 	 *
163 	 * @param output
164 	 *            Output method
165 	 */
setOutput(final String output)166 	public void setOutput(final String output) {
167 		agentOptions.setOutput(output);
168 	}
169 
170 	/**
171 	 * Sets the IP address or hostname to bind to when output method is tcp
172 	 * server or connect to when the output method is tcp client. Default is
173 	 * <code>localhost</code>
174 	 *
175 	 * @param address
176 	 *            Address to bind or connect to
177 	 */
setAddress(final String address)178 	public void setAddress(final String address) {
179 		agentOptions.setAddress(address);
180 	}
181 
182 	/**
183 	 * Sets the Port to bind to when the output method is tcp server or connect
184 	 * to when the output method is tcp client. Default is <code>6300</code>
185 	 *
186 	 * @param port
187 	 *            port to bind to or connect to
188 	 */
setPort(final int port)189 	public void setPort(final int port) {
190 		agentOptions.setPort(port);
191 	}
192 
193 	/**
194 	 * Sets the directory where all class files seen by the agent should be
195 	 * dumped to.
196 	 *
197 	 * @param dir
198 	 *            dump output location
199 	 */
setClassdumpdir(final File dir)200 	public void setClassdumpdir(final File dir) {
201 		agentOptions.setClassDumpDir(dir.getAbsolutePath());
202 	}
203 
204 	/**
205 	 * Sets whether the agent should expose functionality via JMX.
206 	 *
207 	 * @param jmx
208 	 *            <code>true</code> if JMX should be enabled
209 	 */
setJmx(final boolean jmx)210 	public void setJmx(final boolean jmx) {
211 		agentOptions.setJmx(jmx);
212 	}
213 
214 	/**
215 	 * Creates JVM argument to launch with the specified JaCoCo agent jar and
216 	 * the current options
217 	 *
218 	 * @return JVM Argument to pass to new VM
219 	 */
getLaunchingArgument()220 	protected String getLaunchingArgument() {
221 		return prepareAgentOptions().getVMArgument(getAgentFile());
222 	}
223 
prepareAgentOptions()224 	private AgentOptions prepareAgentOptions() {
225 		if (OutputMode.file.equals(agentOptions.getOutput())) {
226 			agentOptions.setDestfile(destfile.getAbsolutePath());
227 		}
228 		return agentOptions;
229 	}
230 
getAgentFile()231 	private File getAgentFile() {
232 		try {
233 			File agentFile = null;
234 			final String agentFileLocation = getProject()
235 					.getProperty("_jacoco.agentFile");
236 			if (agentFileLocation != null) {
237 				agentFile = new File(agentFileLocation);
238 			} else {
239 				agentFile = AgentJar.extractToTempLocation();
240 				getProject().setProperty("_jacoco.agentFile",
241 						agentFile.toString());
242 			}
243 
244 			return agentFile;
245 		} catch (final IOException e) {
246 			throw new BuildException("Unable to extract agent jar", e,
247 					getLocation());
248 		}
249 	}
250 
251 }
252