Skip to content

Commit 5d426dd

Browse files
authored
Support simple calls to just inject a single class (#28)
1 parent 50c74a4 commit 5d426dd

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

class-inject/src/main/java/datadog/instrument/classinject/ClassInjector.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
package datadog.instrument.classinject;
88

9+
import static java.util.Collections.singletonMap;
910
import static org.objectweb.asm.Opcodes.*;
1011

1112
import datadog.instrument.glue.DefineClassGlue;
@@ -38,6 +39,44 @@
3839
@SuppressWarnings({"rawtypes", "unchecked"})
3940
public final class ClassInjector {
4041

42+
/**
43+
* Injects a class on the bootstrap classpath.
44+
*
45+
* @param name the name of the class
46+
* @param bytecode the class bytecode
47+
* @return the injected class
48+
* @throws IllegalStateException if class injection is not enabled
49+
*/
50+
public static Class<?> injectBootClass(String name, byte[] bytecode) {
51+
return injectBootClasses(singletonMap(name, bytecode)).get(0);
52+
}
53+
54+
/**
55+
* Injects a class using the given class-loader.
56+
*
57+
* @param name the name of the class
58+
* @param bytecode the class bytecode
59+
* @param cl the class-loader to use
60+
* @return the injected class
61+
* @throws IllegalStateException if class injection is not enabled
62+
*/
63+
public static Class<?> injectClass(String name, byte[] bytecode, ClassLoader cl) {
64+
return injectClasses(singletonMap(name, bytecode), cl).get(0);
65+
}
66+
67+
/**
68+
* Injects a class using the given protection domain.
69+
*
70+
* @param name the name of the class
71+
* @param bytecode the class bytecode
72+
* @param pd the protection domain to use
73+
* @return the injected class
74+
* @throws IllegalStateException if class injection is not enabled
75+
*/
76+
public static Class<?> injectClass(String name, byte[] bytecode, ProtectionDomain pd) {
77+
return injectClasses(singletonMap(name, bytecode), pd).get(0);
78+
}
79+
4180
/**
4281
* Injects classes on the bootstrap classpath.
4382
*

0 commit comments

Comments
 (0)