-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Install AsyncResult extensions during class initialization
- Loading branch information
Showing
9 changed files
with
124 additions
and
54 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/PreloadingVisitor.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,84 @@ | ||
package datadog.trace.agent.tooling; | ||
|
||
import datadog.trace.util.Strings; | ||
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.jar.asm.ClassVisitor; | ||
import net.bytebuddy.jar.asm.MethodVisitor; | ||
import net.bytebuddy.jar.asm.Opcodes; | ||
import net.bytebuddy.pool.TypePool; | ||
|
||
public final class PreloadingVisitor implements AsmVisitorWrapper { | ||
private final String internalPreloadName; | ||
|
||
public PreloadingVisitor(String preloadClass) { | ||
this.internalPreloadName = Strings.getInternalName(preloadClass); | ||
} | ||
|
||
@Override | ||
public int mergeWriter(int flags) { | ||
return flags; | ||
} | ||
|
||
@Override | ||
public int mergeReader(int flags) { | ||
return flags; | ||
} | ||
|
||
@Override | ||
public ClassVisitor wrap( | ||
final TypeDescription instrumentedType, | ||
final ClassVisitor classVisitor, | ||
final Implementation.Context implementationContext, | ||
final TypePool typePool, | ||
final FieldList<FieldDescription.InDefinedShape> fields, | ||
final MethodList<?> methods, | ||
final int writerFlags, | ||
final int readerFlags) { | ||
return new ClassVisitor(Opcodes.ASM8, classVisitor) { | ||
|
||
private static final String TYPE_INITIALIZER_NAME = "<clinit>"; | ||
private static final String VOID_METHOD_DESCRIPTOR = "()V"; | ||
|
||
private boolean hasTypeInitializer; | ||
|
||
@Override | ||
public MethodVisitor visitMethod( | ||
int access, String name, String descriptor, String signature, String[] exceptions) { | ||
MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions); | ||
if (TYPE_INITIALIZER_NAME.equals(name)) { | ||
hasTypeInitializer = true; | ||
return new MethodVisitor(Opcodes.ASM8, mv) { | ||
@Override | ||
public void visitCode() { | ||
visitMethodInsn( | ||
Opcodes.INVOKESTATIC, internalPreloadName, "init", VOID_METHOD_DESCRIPTOR, false); | ||
super.visitCode(); | ||
} | ||
}; | ||
} | ||
return mv; | ||
} | ||
|
||
@Override | ||
public void visitEnd() { | ||
if (!hasTypeInitializer) { | ||
MethodVisitor mv = | ||
visitMethod( | ||
Opcodes.ACC_STATIC, TYPE_INITIALIZER_NAME, VOID_METHOD_DESCRIPTOR, null, null); | ||
mv.visitCode(); | ||
mv.visitMethodInsn( | ||
Opcodes.INVOKESTATIC, internalPreloadName, "init", VOID_METHOD_DESCRIPTOR, false); | ||
mv.visitInsn(Opcodes.RETURN); | ||
mv.visitMaxs(0, 0); | ||
mv.visitEnd(); | ||
} | ||
super.visitEnd(); | ||
} | ||
}; | ||
} | ||
} |
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
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
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