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
MyTransformerclass implements the interfacejava.lang.instrument.ClassFileTransformer MyTransformeroverrides the methodtransformto modify the bytecode of HelloWorld.class- Create
ClassPoolobject to load the class file - Read the class file into a
CtClassobject usingClassPooland aByteArrayInputStreamcreated from theclassfileBuffer - Obtain the
CtMethodrepresenting themainmethod from theCtClassobject - Convert the modified
CtClassback to bytecode usingcc.toBytecode() - Update the
classfileBufferwith the modified bytecode - specify the 'Premain-Class' header in manifest.mf, in provided example, it is
myagent.MyAgent