Skip to content

Commit

Permalink
Remove logic to open java.base/java.lang. Fixes jacoco#1328
Browse files Browse the repository at this point in the history
It appears if this was necessary, it no longer is. All tests pass without this logic.
  • Loading branch information
DanielThomas committed Jun 16, 2022
1 parent ec257cb commit b3a715d
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 58 deletions.
1 change: 1 addition & 0 deletions org.jacoco.agent.rt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<Implementation-Title>${project.description}</Implementation-Title>
<Implementation-Vendor>${project.organization.name}</Implementation-Vendor>
<Implementation-Version>${project.version}</Implementation-Version>
<Add-Opens>java.base/java.lang</Add-Opens>
</manifestEntries>
</transformer>
</transformers>
Expand Down
61 changes: 3 additions & 58 deletions org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/PreMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,67 +49,12 @@ public static void premain(final String options, final Instrumentation inst)

final Agent agent = Agent.getInstance(agentOptions);

final IRuntime runtime = createRuntime(inst);
final IRuntime runtime = ModifiedSystemClassRuntime.createFor(inst,
"java/lang/UnknownError");

runtime.startup(agent.getData());
inst.addTransformer(new CoverageTransformer(runtime, agentOptions,
IExceptionLogger.SYSTEM_ERR));
}

private static IRuntime createRuntime(final Instrumentation inst)
throws Exception {

if (redefineJavaBaseModule(inst)) {
return new InjectedClassRuntime(Object.class, "$JaCoCo");
}

return ModifiedSystemClassRuntime.createFor(inst,
"java/lang/UnknownError");
}

/**
* Opens {@code java.base} module for {@link InjectedClassRuntime} when
* executed on Java 9 JREs or higher.
*
* @return <code>true</code> when running on Java 9 or higher,
* <code>false</code> otherwise
* @throws Exception
* if unable to open
*/
private static boolean redefineJavaBaseModule(
final Instrumentation instrumentation) throws Exception {
try {
Class.forName("java.lang.Module");
} catch (final ClassNotFoundException e) {
return false;
}

Instrumentation.class.getMethod("redefineModule", //
Class.forName("java.lang.Module"), //
Set.class, //
Map.class, //
Map.class, //
Set.class, //
Map.class //
).invoke(instrumentation, // instance
getModule(Object.class), // module
Collections.emptySet(), // extraReads
Collections.emptyMap(), // extraExports
Collections.singletonMap("java.lang",
Collections.singleton(
getModule(InjectedClassRuntime.class))), // extraOpens
Collections.emptySet(), // extraUses
Collections.emptyMap() // extraProvides
);
return true;
}

/**
* @return {@code cls.getModule()}
*/
private static Object getModule(final Class<?> cls) throws Exception {
return Class.class //
.getMethod("getModule") //
.invoke(cls);
}

}

0 comments on commit b3a715d

Please sign in to comment.