-
Notifications
You must be signed in to change notification settings - Fork 11
Instrumentation API
Devan-Kerman edited this page Jul 14, 2020
·
1 revision
Sometimes, for, whatever reason you may have, you want to mix into the JDK, or maybe change classes after they've been defined. Well my dear friend.. I have just the thing for you.
Allow me to introduce you to the magic of Instrumentation
,
InstrumentationApi
is the class you will need.
// as Object is already defined (obviously) we are at the whims of redefinition, which is somewhat limited in that you can't add fields or methods, however, you can modify existing ones
InstrumentationApi.retransformClass(Object.class, (s, b) -> {
for(MethodNode node : b.methods) {
if("equals".equals(node.name)) {// there is an irony in this
node.instructions.clear(); // remove all old instructions
node.visitInsn(ICONST_1); // load true on stack
node.visitInsn(IRETURN); // return true
}
}
});
You can also use it to transform classes that are normally out of your reach, such as classes in floader and mixin (provided the class you want to target is loaded after you)
// this will make any mixin to java.net.Authenticator valid, and mixin will apply it (provided some other smelly mod doesn't load it before you)
InstrumentationApi.pipeClassThroughTransformerBootstrap("java.net.Authenticator");