Skip to content

Commit

Permalink
Initialize Offline runtime outside class initializers
Browse files Browse the repository at this point in the history
This avoids JVM crashes, see JDK-8228485.
  • Loading branch information
marchof committed Aug 6, 2019
1 parent 9b5e9f8 commit 278e8bb
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions org.jacoco.agent.rt/src/org/jacoco/agent/rt/internal/Offline.java
Expand Up @@ -22,23 +22,27 @@
*/
public final class Offline {

private static final RuntimeData DATA;
private static final String CONFIG_RESOURCE = "/jacoco-agent.properties";

static {
final Properties config = ConfigLoader.load(CONFIG_RESOURCE,
System.getProperties());
try {
DATA = Agent.getInstance(new AgentOptions(config)).getData();
} catch (final Exception e) {
throw new RuntimeException("Failed to initialize JaCoCo.", e);
}
}

private Offline() {
// no instances
}

private static RuntimeData data;

private static synchronized RuntimeData getRuntimeData() {
if (data == null) {
final Properties config = ConfigLoader.load(CONFIG_RESOURCE,
System.getProperties());
try {
data = Agent.getInstance(new AgentOptions(config)).getData();
} catch (final Exception e) {
throw new RuntimeException("Failed to initialize JaCoCo.", e);
}
}
return data;
}

/**
* API for offline instrumented classes.
*
Expand All @@ -52,7 +56,7 @@ private Offline() {
*/
public static boolean[] getProbes(final long classid,
final String classname, final int probecount) {
return DATA
return getRuntimeData()
.getExecutionData(Long.valueOf(classid), classname, probecount)
.getProbes();
}
Expand Down

0 comments on commit 278e8bb

Please sign in to comment.