-
Notifications
You must be signed in to change notification settings - Fork 905
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove virtual field interfaces from reflection results (#4722)
* Remove virtual field interfaces from reflection results * fix java8 and openj9
- Loading branch information
Showing
9 changed files
with
164 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
.../io/opentelemetry/javaagent/instrumentation/internal/reflection/ClassInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.internal.reflection; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import net.bytebuddy.asm.AsmVisitorWrapper; | ||
import net.bytebuddy.description.field.FieldDescription; | ||
import net.bytebuddy.description.field.FieldList; | ||
import net.bytebuddy.description.method.MethodList; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.implementation.Implementation; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import net.bytebuddy.pool.TypePool; | ||
import org.objectweb.asm.ClassVisitor; | ||
import org.objectweb.asm.ClassWriter; | ||
import org.objectweb.asm.MethodVisitor; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.Type; | ||
|
||
public class ClassInstrumentation implements TypeInstrumentation { | ||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("java.lang.Class"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyTransformer( | ||
(builder, typeDescription, classLoader, module) -> | ||
builder.visit( | ||
new AsmVisitorWrapper() { | ||
@Override | ||
public int mergeWriter(int flags) { | ||
return flags | ClassWriter.COMPUTE_MAXS; | ||
} | ||
|
||
@Override | ||
public int mergeReader(int flags) { | ||
return flags; | ||
} | ||
|
||
@Override | ||
public ClassVisitor wrap( | ||
TypeDescription instrumentedType, | ||
ClassVisitor classVisitor, | ||
Implementation.Context implementationContext, | ||
TypePool typePool, | ||
FieldList<FieldDescription.InDefinedShape> fields, | ||
MethodList<?> methods, | ||
int writerFlags, | ||
int readerFlags) { | ||
return new ClassClassVisitor(classVisitor); | ||
} | ||
})); | ||
} | ||
|
||
private static class ClassClassVisitor extends ClassVisitor { | ||
|
||
ClassClassVisitor(ClassVisitor cv) { | ||
super(Opcodes.ASM7, cv); | ||
} | ||
|
||
@Override | ||
public MethodVisitor visitMethod( | ||
int access, String name, String descriptor, String signature, String[] exceptions) { | ||
MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions); | ||
if ("getInterfaces".equals(name) | ||
&& ("()[Ljava/lang/Class;".equals(descriptor) | ||
|| "(Z)[Ljava/lang/Class;".equals(descriptor))) { | ||
mv = | ||
new MethodVisitor(api, mv) { | ||
@Override | ||
public void visitMethodInsn( | ||
int opcode, String owner, String name, String descriptor, boolean isInterface) { | ||
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface); | ||
// filter the result of call to getInterfaces0, which is used on hotspot, and | ||
// J9VMInternals.getInterfaces which is used on openj9 | ||
if (((opcode == Opcodes.INVOKEVIRTUAL || opcode == Opcodes.INVOKESPECIAL) | ||
&& "getInterfaces0".equals(name) | ||
&& "()[Ljava/lang/Class;".equals(descriptor)) | ||
|| (opcode == Opcodes.INVOKESTATIC | ||
&& "getInterfaces".equals(name) | ||
&& "java/lang/J9VMInternals".equals(owner) | ||
&& "(Ljava/lang/Class;)[Ljava/lang/Class;".equals(descriptor))) { | ||
mv.visitVarInsn(Opcodes.ALOAD, 0); | ||
mv.visitMethodInsn( | ||
Opcodes.INVOKESTATIC, | ||
Type.getInternalName(ReflectionHelper.class), | ||
"filterInterfaces", | ||
"([Ljava/lang/Class;Ljava/lang/Class;)[Ljava/lang/Class;", | ||
false); | ||
} | ||
} | ||
}; | ||
} | ||
return mv; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...tstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/VirtualFieldAccessorMarker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.bootstrap; | ||
|
||
/** A marker interface implemented by virtual field accessor classes. */ | ||
public interface VirtualFieldAccessorMarker {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters