Topic
IC4NOTICE: developerWorks Community will be offline May 29-30, 2015 while we upgrade to the latest version of IBM Connections. For more information, read our upgrade FAQ.
2 replies Latest Post - ‏2013-07-18T04:23:42Z by poisy
Timur_C
Timur_C
3 Posts
ACCEPTED ANSWER

Pinned topic Using Annotations to modify/generate source code (or bytecode)

‏2009-11-14T18:04:14Z |
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.
Updated on 2009-11-24T21:48:41Z at 2009-11-24T21:48:41Z by Timur_C
  • Timur_C
    Timur_C
    3 Posts
    ACCEPTED ANSWER

    Re: Using Annotations to modify/generate source code (or bytecode)

    ‏2009-11-14T19:01:10Z  in response to Timur_C
    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
      Timur_C
      3 Posts
      ACCEPTED ANSWER

      Re: Using Annotations to modify/generate source code (or bytecode)

      ‏2009-11-24T21:48:41Z  in response to Timur_C
      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.
  • This reply was deleted by poisy 2013-07-18T04:24:57Z.