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.
This topic has been locked.
2 replies Latest Post - 2013-07-18T04:23:42Z by poisy
Pinned topic Using Annotations to modify/generate source code (or bytecode)
Answered question This question has been answered.
Unanswered question This question has not been answered yet.
Updated on 2009-11-24T21:48:41Z at 2009-11-24T21:48:41Z by Timur_C
Re: Using Annotations to modify/generate source code (or bytecode)2009-11-14T19:01:10Z in response to Timur_CI'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?
Re: Using Annotations to modify/generate source code (or bytecode)2009-11-24T21:48:41Z in response to Timur_CThanks 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):
- A small wrapper for Javassist that can be used to generate source files and bytecode.
- 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.
This reply was deleted by poisy 270006GYQJ 2013-07-18T04:24:57Z.