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