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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Question about using byte coding to get formal arugments of a method

 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

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...

Regards
Maulin
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ernest

Thanks for your reply.

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 given that, would my direction be right?

Thanks
Maulin
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

Regards
Maulin
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, BCEL and Javassist can do this.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also see the java.lang.instrument package.
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/instrument/package-summary.html
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic