Skip to content

Commit 2988647

Browse files
authored
AgentModule should use ClassLoader of agent instead of SystemClassLoader (#1651)
1 parent 75a4e31 commit 2988647

6 files changed

Lines changed: 151 additions & 6 deletions

File tree

‎org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/AgentModule.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static boolean isSupported() {
6060
* if it cannot be created
6161
*/
6262
public AgentModule() throws Exception {
63-
classLoader = new ClassLoader() {
63+
classLoader = new ClassLoader(AgentModule.class.getClassLoader()) {
6464
@Override
6565
protected Class<?> loadClass(final String name,
6666
final boolean resolve) throws ClassNotFoundException {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2009, 2026 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+
* https://www.eclipse.org/legal/epl-2.0
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
*
9+
* Contributors:
10+
* Evgeny Mandrikov - initial API and implementation
11+
*
12+
*******************************************************************************/
13+
package org.jacoco.ant;
14+
15+
import static org.jacoco.ant.AntUnitSuiteFactory.suiteFor;
16+
17+
import java.io.IOException;
18+
19+
import org.apache.ant.antunit.junit4.AntUnitSuiteRunner;
20+
import org.jacoco.agent.AgentJar;
21+
import org.junit.runner.RunWith;
22+
23+
import junit.framework.TestSuite;
24+
25+
@RunWith(AntUnitSuiteRunner.class)
26+
public class AgentClassLoaderTest {
27+
28+
public static TestSuite suite() throws IOException {
29+
System.setProperty("org.jacoco.ant.agentClassLoaderTest.classes.dir",
30+
TestTarget.getClassPath());
31+
System.setProperty("org.jacoco.ant.agentClassLoaderTest.agent.jar",
32+
AgentJar.extractToTempLocation().getAbsolutePath());
33+
return suiteFor(AgentClassLoaderTest.class);
34+
}
35+
36+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Copyright (c) 2009, 2026 Mountainminds GmbH & Co. KG and Contributors
5+
This program and the accompanying materials are made available under
6+
the terms of the Eclipse Public License 2.0 which is available at
7+
https://www.eclipse.org/legal/epl-2.0
8+
9+
SPDX-License-Identifier: EPL-2.0
10+
11+
Contributors:
12+
Evgeny Mandrikov - initial API and implementation
13+
-->
14+
15+
<project name="JaCoCo Agent ClassLoader Test" xmlns:au="antlib:org.apache.ant.antunit" xmlns:jacoco="antlib:org.jacoco.ant">
16+
17+
<target name="setUp">
18+
<tempfile property="temp.dir" prefix="jacocoTest" destdir="${java.io.tmpdir}"/>
19+
<mkdir dir="${temp.dir}"/>
20+
</target>
21+
22+
<target name="testAgentClassLoader">
23+
<jar destfile="${temp.dir}/agent-loader.jar">
24+
<fileset dir="${org.jacoco.ant.agentClassLoaderTest.classes.dir}"
25+
includes="org/jacoco/ant/AgentLoader.class"/>
26+
<manifest>
27+
<attribute name="Main-Class" value="org.jacoco.ant.AgentLoader"/>
28+
<attribute name="Premain-Class" value="org.jacoco.ant.AgentLoader"/>
29+
</manifest>
30+
</jar>
31+
32+
<java fork="true" failonerror="true" jar="${temp.dir}/agent-loader.jar" logError="true" dir="${temp.dir}">
33+
<jvmarg value="-javaagent:${temp.dir}/agent-loader.jar"/>
34+
<arg value="${org.jacoco.ant.agentClassLoaderTest.agent.jar}"/>
35+
</java>
36+
37+
<au:assertFileExists file="${temp.dir}/jacoco.exec"/>
38+
</target>
39+
40+
<target name="tearDown">
41+
<delete dir="${temp.dir}" quiet="false" failonerror="true"/>
42+
</target>
43+
44+
</project>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2009, 2026 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+
* https://www.eclipse.org/legal/epl-2.0
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
*
9+
* Contributors:
10+
* Evgeny Mandrikov - initial API and implementation
11+
*
12+
*******************************************************************************/
13+
package org.jacoco.ant;
14+
15+
import java.io.File;
16+
import java.lang.instrument.Instrumentation;
17+
import java.lang.reflect.Method;
18+
import java.net.URL;
19+
import java.net.URLClassLoader;
20+
import java.util.jar.Manifest;
21+
22+
/**
23+
* Simulates behavior of <a href=
24+
* "https://github.com/jboss-modules/jboss-modules/blob/2.3.0/src/main/java/org/jboss/modules/Main.java">JBoss
25+
* Modules</a> that in contrast to {@link java.lang.instrument normal agent
26+
* execution} loads and executes agents using non
27+
* {@link ClassLoader#getSystemClassLoader() system class loader}.
28+
* <p>
29+
* This class must be declared and executed as both
30+
* {@link #premain(String, Instrumentation) Premain-Class} and
31+
* {@link #main(String[]) Main-Class}, so that first execution can capture
32+
* instance of {@link Instrumentation} used by second.
33+
* </p>
34+
*/
35+
public final class AgentLoader {
36+
37+
private static Instrumentation instrumentation;
38+
39+
/** Captures instance of {@link Instrumentation}. */
40+
public static void premain(final String agentArgs,
41+
final Instrumentation instrumentation) {
42+
AgentLoader.instrumentation = instrumentation;
43+
}
44+
45+
/**
46+
* Loads and executes {@code Premain-Class} from JAR file passed as first
47+
* argument using new (<strong>non system</strong>) class loader.
48+
*/
49+
public static void main(final String[] args) throws Exception {
50+
final URLClassLoader classLoader = new URLClassLoader(
51+
new URL[] { new File(args[0]).toURI().toURL() });
52+
final Manifest manifest = new Manifest();
53+
manifest.read(
54+
classLoader.findResource("META-INF/MANIFEST.MF").openStream());
55+
final Class<?> agentClass = classLoader.loadClass(
56+
manifest.getMainAttributes().getValue("Premain-Class"));
57+
final Method premain = agentClass.getDeclaredMethod("premain",
58+
String.class, Instrumentation.class);
59+
premain.invoke(null, "", instrumentation);
60+
}
61+
62+
}

‎org.jacoco.ant.test/src/org/jacoco/ant/InstrumentTaskTest.xml‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
<jacoco:instrument destdir="${instr.dir}">
6464
<fileset dir="${lib.dir}" includes="*.jar"/>
6565
</jacoco:instrument>
66-
<au:assertLogContains text="Instrumented 18 classes to ${temp.dir}"/>
66+
<au:assertLogContains text="Instrumented 20 classes to ${temp.dir}"/>
6767

6868
<unzip src="${instr.dir}/test.jar" dest="${instr.dir}"/>
6969
<au:assertFileDoesntExist file="${instr.dir}/META-INF/TEST.RSA" />
@@ -84,7 +84,7 @@
8484
<jacoco:instrument destdir="${instr.dir}" removesignatures="false">
8585
<fileset dir="${lib.dir}" includes="*.jar"/>
8686
</jacoco:instrument>
87-
<au:assertLogContains text="Instrumented 18 classes to ${temp.dir}"/>
87+
<au:assertLogContains text="Instrumented 20 classes to ${temp.dir}"/>
8888

8989
<unzip src="${instr.dir}/test.jar" dest="${instr.dir}"/>
9090
<au:assertFileExists file="${instr.dir}/META-INF/TEST.RSA" />
@@ -95,7 +95,7 @@
9595
<jacoco:instrument destdir="${temp.dir}">
9696
<fileset dir="${org.jacoco.ant.instrumentTaskTest.classes.dir}" includes="**/*.class"/>
9797
</jacoco:instrument>
98-
<au:assertLogContains text="Instrumented 18 classes to ${temp.dir}"/>
98+
<au:assertLogContains text="Instrumented 20 classes to ${temp.dir}"/>
9999
<au:assertFileExists file="${temp.dir}/org/jacoco/ant/InstrumentTaskTest.class" />
100100

101101
<echo file="${temp.dir}/jacoco-agent.properties">destfile=test.exec</echo>
@@ -112,7 +112,7 @@
112112
<jacoco:instrument destdir="${temp.dir}">
113113
<fileset dir="${org.jacoco.ant.instrumentTaskTest.classes.dir}" includes="**/*.class"/>
114114
</jacoco:instrument>
115-
<au:assertLogContains text="Instrumented 18 classes to ${temp.dir}"/>
115+
<au:assertLogContains text="Instrumented 20 classes to ${temp.dir}"/>
116116
<au:assertFileExists file="${temp.dir}/org/jacoco/ant/InstrumentTaskTest.class" />
117117

118118
<java classname="org.jacoco.ant.TestTarget" failonerror="true" fork="true">
@@ -129,7 +129,7 @@
129129
<jacoco:instrument destdir="${temp.dir}">
130130
<fileset dir="${org.jacoco.ant.instrumentTaskTest.classes.dir}" includes="**/*.class"/>
131131
</jacoco:instrument>
132-
<au:assertLogContains text="Instrumented 18 classes to ${temp.dir}"/>
132+
<au:assertLogContains text="Instrumented 20 classes to ${temp.dir}"/>
133133
<au:assertFileExists file="${temp.dir}/org/jacoco/ant/InstrumentTaskTest.class" />
134134

135135
<java classname="org.jacoco.ant.TestTarget" failonerror="false" fork="true">

‎org.jacoco.doc/docroot/doc/changes.html‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ <h3>Fixed bugs</h3>
6363
<code>BufferedOutputStream</code>, this fixes regression introduced
6464
in version 0.6.2
6565
(GitHub <a href="https://github.com/jacoco/jacoco/issues/2073">#2073</a>).</li>
66+
<li>Fixed <code>NullPointerException</code> when JaCoCo agent is loaded by
67+
non system class loader, for example when loaded by JBoss Modules
68+
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1651">#1651</a>).</li>
6669
</ul>
6770

6871
<h2>Release 0.8.14 (2025/10/11)</h2>

0 commit comments

Comments
 (0)