This week's book giveaway is in the Testing forum. We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line! See this thread for details.
So I was wondering for sometime how to achieve following, "I want to insert byte code instructions in a method's byte code such that I can call custom code from there and pass values of the formal arguments that were passed to the actual method call".
So far my thinking about what I should do is, 1. I should first learn little byte codes for JVM
2. Then figure out how/where JVM organizes the formal method calls at runtime (in a stackframe of course but I don't know how to access that stackframe from byte code instructions)
3. Use some library like Apache's byte code (don't recall exact name right now) to insert such byte codes easily
Now, the 1,3 parts are okay but I think I would have to put more effort figuring out how to achieve the 2nd point here and then I should be in good shape.. I think I can study some .class file's byte codes to figure out how to see how to access the value of formal arugments for a method...
Do I make sense? Any pointer or comment would be helpful to see if I am thinking right way...
You won't be able to do it quite the way you've imagined it; you can insert bytecode and such before a class is loaded, but once the class is loaded, you don't have access to the bytecode stream being executed.
In any case, libraries that modify bytecode on the fly do so using a special ClassLoader which loads the actual bits out of a class file, modifies or augments them, and then passes those modified bits up to the superclass ClassLoader to actual load them into the VM.
Not quite what you are asking, but AOP (Aspect Oriented Programming) as implemented (e.g.) in AspectJ works by modifying the classes created by the compiler. That would allow you to hook into particular methods.
If instead you want to try modifying bytecodes at runtime (creating new classes in the process, as Ernest pointed out), have a look at the Javassist (on SourceForge) and Jakarta BCEL projects. [ July 30, 2006: Message edited by: Ulf Dittmer ]
I am okay with pre modifying .class files too. I don't have to do it on the fly really. I can modify the .class files before running the application and that will also do fine for me. Actually, that is what I planned that after I am done with coding the application I will run another program separately which can modify the class files and then use the jar file with these modified class files...So this way, evertime I would probably have to run this other program which inserts the byte codes into the class files but later on I would try to do it little smarter way...
I certainly don't want any complicated stuff of dealing with class loaders..
So Ulf and Ernest, can't I use BCEL to like premodify .class files like AspectJ thing?
I am fine with pre modifying class files. I just don't want to have that code written in my java source files so the source file authors don't have to worry about any of this. So logic would be,
1. Other developers writes java source and compiles and all 2. After the application is complete I would run a program which will go and modify all the class files to insert custom code to achieve what I mentioned in the first post