2

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 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I can see your point. I've looked at Javaassist a while ago. If no one knows, then that is probably what I'm going to do.
I've looked at it again, and I'm not sure how to do that either with Javassist. Your "source code" that you use to generate the class files will probably contain stuff like "$cflow(...)", so it is not real Java code. And I can only see methods to save the compiled class in CtClass. Are you sure this is supported?
No other answer after a day. Must not be possible with ASM, so you get the points.
1

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

1 Comment

I'm not sure if this is useful to me, but I give you +1 for the typo: "you can sue it here" LOL!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.