simple Hello World example, modify the output string 'Hello World! 👀' to 'Bye, Bye! 🖐️' using javassist library.
openjdk : 11.0.11
# build helloworld.jar
cd helloworldapp
make jar
- prints 'Hello World! 👀' to console
# run helloworld.jar
cd agent
make
- prints 'Bye, Bye! 🖐️' to console
├── README.md
├── agent
│ ├── Makefile
│ ├── lib
│ │ └── javassist.jar
│ └── src
│ ├── manifest.mf
│ └── myagent
│ ├── MyAgent.java
│ └── MyTransformer.java
└── helloworldapp
├── Makefile
└── src
├── HelloWorld.java
└── manifest.mf
- javassist.jar
- helloworld.jar
- myagent.jarlibrary.
wget https://github.com/jboss-javassist/javassist/releases/download/rel_3_29_2_ga/javassist.jar
- simple java application, print 'Hello World!' to console
- specify the 'Main-class' header in manifest.mf, in provided example, it is
theapp.HelloWorld
- Consists of a entry point class and a transformer class
- Create
MyTransformer
class implements the interfacejava.lang.instrument.ClassFileTransformer
MyTransformer
overrides the methodtransform
to modify the bytecode of HelloWorld.class- Create
ClassPool
object to load the class file - Read the class file into a
CtClass
object usingClassPool
and aByteArrayInputStream
created from theclassfileBuffer
- Obtain the
CtMethod
representing themain
method from theCtClass
object - Convert the modified
CtClass
back to bytecode usingcc.toBytecode()
- Update the
classfileBuffer
with the modified bytecode - specify the 'Premain-Class' header in manifest.mf, in provided example, it is
myagent.MyAgent