Last active
April 22, 2022 05:30
-
-
Save apangin/f646eb0166410877d343 to your computer and use it in GitHub Desktop.
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
import java.lang.instrument.*; | |
import java.security.ProtectionDomain; | |
public class GetBytecode implements ClassFileTransformer { | |
private static Instrumentation inst; | |
public static synchronized void agentmain(String args, Instrumentation inst) { | |
GetBytecode.inst = inst; | |
} | |
public static synchronized void premain(String args, Instrumentation inst) { | |
GetBytecode.inst = inst; | |
} | |
public static synchronized byte[] getClassFile(Class cls) throws UnmodifiableClassException { | |
Instrumentation inst = GetBytecode.inst; | |
if (inst == null) { | |
throw new IllegalStateException("Agent has not been loaded"); | |
} | |
GetBytecode transformer = new GetBytecode(); | |
inst.addTransformer(transformer, true); | |
inst.retransformClasses(cls); | |
inst.removeTransformer(transformer); | |
return transformer.classFile; | |
} | |
private byte[] classFile; | |
@Override | |
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, | |
ProtectionDomain pd, byte[] classFile) throws IllegalClassFormatException { | |
if (classBeingRedefined != null) { | |
this.classFile = classFile; | |
} | |
return null; | |
} | |
} |
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
Agent-Class: GetBytecode | |
Premain-Class: GetBytecode | |
Can-Retransform-Classes: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment