What exactly is Java bytecode injection and why would one use it?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Java code compiles into bytecode (
Foo.java ->> Foo.class). Bytecode injection is modifyingFoo.classat runtime to inject code into it right before its loaded and run.Imagine a scenario where I want to find out how many times method
is invoked in
Foo.class. I could write an agent usingjava.lang.instrumentthat interceptsFoo.classduring class load, modifies it using ASM so thatbar()callscom.amir.agent.incrementCount()on method entry.Now I can run my program:
and at runtime,
Foo.classwill not only do its normal work, it will also do work I defined inMyAgenteach timebar()is invoked.For a pointer on writing an agent from scratch, start here.