Skip to main content

developerWorks >  Java technology  >  Forums  >  Java Virtual Machine (JVM) and Bytecode  >  developerWorks

Using Annotations to modify/generate source code (or bytecode)    Point your RSS reader here for a feed of the latest messages in this thread


Tags for this thread: 

     

 
 

My developerWorks
 Welcome, Guest
Sign in or register
Permlink Replies: 2 - Pages: 1 - Last Post: Nov 24, 2009 4:48 PM Last Post By: Timur_C
Timur_C

Posts: 3
Registered: Nov 14, 2009 12:32:45 PM
Using Annotations to modify/generate source code (or bytecode)
Posted: Nov 14, 2009 01:04:14 PM
Click to report abuse...   Click to reply to this thread Reply
Hello, everybody.
I'm new here, so please excuse me if I ask an inappropriate question in an inappropriate place. I hope somebody gives me some clue to solve my problem.
I annotate some methods with a custom annotation. I would like to modify those methods according to this annotation in this way: insert small custom code in the begining and the end of such method OR to substitute original method with mine and call it's renamed copy from inside of my new method (this modification must be transparent to clients of course). My custom code creates an anonymous inner class (subclass) overriding one method in it (superclass method) and calls a couple of other methods. Important thing is that I need to have these modified classes to be loadable by other JVM via RMI, so I suppose no on-the-fly modification will help - I need ready .class files before deploying/starting the application.
I'm new to byte-code, but read a couple of articles about BCEL and ASM and still haven't found any solution. So my questions:
1) Can I do this with byte-code generation tools? (especially creating anonymous classes and overriding methods in them)
2) Are there any source code modification/generation tools that could help me? (it would be much easier to work on the source level for me

I will be very glad to see any links, suggestions, even just keywords. If you need some details, just ask.
Timur_C

Posts: 3
Registered: Nov 14, 2009 12:32:45 PM
Re: Using Annotations to modify/generate source code (or bytecode)
Posted: Nov 14, 2009 02:01:10 PM   in response to: Timur_C in response to: Timur_C's post
Click to report abuse...   Click to reply to this thread Reply
I've thought a while about my questions... and got to idea that I don't necessarily need anonymous inner classes... I just need to generate new classes according to the annotation and then use them in modified methods. So I think I can do everything at bytecode level (if a tool can generate new classes, overriding methods).
Could you suggest me what tool is better for my problem? BCEL, AMS, Javasist or something else?
Timur_C

Posts: 3
Registered: Nov 14, 2009 12:32:45 PM
Re: Using Annotations to modify/generate source code (or bytecode)
Posted: Nov 24, 2009 04:48:41 PM   in response to: Timur_C in response to: Timur_C's post
Click to report abuse...   Click to reply to this thread Reply
Thanks for all your kind answers...

I've used Javassist because it's much easier to work in its pseudo-code than in plain bytecode.

1) No, you can't create anonymous or any inner classes (bytecode doesn't have such things, they are all compiled as ordinary classes)
2) Yes, with Javassist you can create, copy, modify and delete methods; you can specify method body and call inside it other methods or create any objects (and more other things in almost the same way as in java source code)
3) I haven't found any source code modification and generation tools, except these (I haven't checked any of them myself, so I'm not sure if they do what is promised):
http://www.fuin.org/srcgen4javassist/examples.html
  • A small wrapper for Javassist that can be used to generate source files and bytecode.

http://spoon.gforge.inria.fr/Spoon/HomePage
  • Spoon is a Java program processor that fully supports Java 5 and 6. It provides a complete and fine-grained Java metamodel where any program element (classes, methods, fields, statements, expressions...) can be accessed both for reading and modification.

I decided to stick to bytecode generation/modification, so I didn't check those tools. Maybe this topic will help somebody else. No it may be closed, I solved my problem by myself.

 Tags
Help

Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular type of content or application that you're viewing.

My tags shows your tags for this particular type of content or application that you're viewing.

 

MoreLess 


Point your RSS reader here for a feed of the latest messages in all forums