My Google foo is failing me. If I have some framework that can do code generation based on annotations, and I would like to support both build-time source code generation, so that the developer can compile it in advance, also see what gets generated, and possibly even modify the code, but also offer runtime bytecode generation for more dynamic apps, for example plugin-based apps, can I use ASM for both tasks, or do I really need to program this twice, with two different libraries? If it is possible, a link to an example would be welcome.
2 Answers
Whilst this doesn't directly answer the question, I recommend using Javassist over ASM.
With Javassist you start out by generating the source code and you simply give it to the Javassist compiler to turn into bytecode, so you now have both available without any extra tricks. Plus, IMHO, generating source code for Javassist is MUCH easier than messing around with bytecode and ASM.
3 Comments
For build time code generation I recommend using eclipse java compiler, which doesn't have open API for that, but which is actually is very easy to use. You can take a look on how you can use it here: http://git.jetbrains.org/?p=mps/mps.git;a=tree;f=core/kernel/source/jetbrains/mps/compiler;h=6787147e3b36899574f042dd2132a4c08963670e;hb=HEAD (This code was written mostly by me). There's also an example of how you can load compiled classes dynamically (MyClassLoader inner class).