Java’s Byte Code Enhancement Method


1. Modify bytecode

The tools to be used in the first step are ASM, javassist and cglib (ASM-based), which are used to modify bytecode.

2. Load new bytecode

The second step is to customize the ClassLoader to load the modified bytecode.

3. Replace the original bytecode

Or replace the original bytecode, there are mainly two technologies:
1. Intercept and return the modified bytecode when JVM loads the user’s Class
2. At runtime, the instrumentation. redefine classes method is used to replace the original bytecode, and the instances related to this class take effect immediately.

Agent plugin is required at startup
java -javaagent:xxxtransformer.jar xxxclass
Or the runtime attaches the agent to the pid via VirtualMachine.attach and VirtualMachine.loadAgent