Skip to content

Commit 506cc62

Browse files
committed
fix things again
1 parent 403caaa commit 506cc62

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

Diff for: javaagent-tooling/javaagent-tooling-java9/src/main/java/io/opentelemetry/javaagent/tooling/Java9LambdaClassFileTransformer.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import java.lang.instrument.IllegalClassFormatException;
1010
import java.security.ProtectionDomain;
1111

12-
/** {@link ClassFileTransformer} implementation that provides java9 jpms module compatibility */
12+
/** {@link ClassFileTransformer} for lambda instrumentation with java9 jpms module compatibility */
1313
public class Java9LambdaClassFileTransformer implements ClassFileTransformer {
1414

15-
private ClassFileTransformer delegate;
15+
private final ClassFileTransformer delegate;
1616

1717
public Java9LambdaClassFileTransformer(ClassFileTransformer delegate) {
1818
this.delegate = delegate;
@@ -27,7 +27,12 @@ public byte[] transform(
2727
byte[] classfileBuffer)
2828
throws IllegalClassFormatException {
2929

30-
Module module = targetClass != null ? targetClass.getModule() : null;
30+
if (targetClass == null) {
31+
// we expect the target class argument to be non-null, however it's only used to resolve
32+
// the JPMS module
33+
throw new IllegalStateException("can't resolve module without class");
34+
}
35+
Module module = targetClass.getModule();
3136

3237
// lambda instrumentation happens only when the lambda is defined, thus the classBeingRedefined
3338
// must be null otherwise we get a partial instrumentation, for example virtual fields are not

Diff for: javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/AgentInstaller.java

+3
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ private static void installBytebuddyAgent(
203203
if (JavaModule.isSupported()) {
204204
// wrapping in a JPMS compliant implementation
205205
transformer = new Java9LambdaClassFileTransformer(transformer);
206+
} else {
207+
// wrapping in a java 8 compliant transformer
208+
transformer = new LambdaClassFileTransformer(transformer);
206209
}
207210
ClassFileTransformerHolder.setLambdaClassFileTransformer(transformer);
208211

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.tooling;
7+
8+
import java.lang.instrument.ClassFileTransformer;
9+
import java.lang.instrument.IllegalClassFormatException;
10+
import java.security.ProtectionDomain;
11+
12+
/** {@link ClassFileTransformer} for lambda instrumentation without jpms modules */
13+
public class LambdaClassFileTransformer implements ClassFileTransformer {
14+
15+
private final ClassFileTransformer delegate;
16+
17+
public LambdaClassFileTransformer(ClassFileTransformer delegate) {
18+
this.delegate = delegate;
19+
}
20+
21+
@Override
22+
public byte[] transform(
23+
ClassLoader loader,
24+
String className,
25+
Class<?> classBeingRedefined,
26+
ProtectionDomain protectionDomain,
27+
byte[] classfileBuffer)
28+
throws IllegalClassFormatException {
29+
30+
// lambda instrumentation happens only when the lambda is defined, thus the classBeingRedefined
31+
// must be null otherwise we get a partial instrumentation, for example virtual fields are not
32+
// properly applied. This parameter is however used in Java9LambdaClassFileTransformer.
33+
return delegate.transform(loader, className, null, null, classfileBuffer);
34+
}
35+
}

0 commit comments

Comments
 (0)