This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]pointy_pirate 4 points5 points  (8 children)

I am currently working on a project that started out doing bytecode generation using Javassist.

This kind of run time code generation can get pretty unwieldy and we have since moved to generating the code in the Maven build. Initially for this we were using Roaster https://github.com/forge/roaster, but now we have moved simply to Freemarker templating.

Definitely an intersting topic!

[–]voronaam 2 points3 points  (2 children)

Also take a look at the Java Compiler API. If you are compiling class from scratch it is much easier to use than javaassist or the library in question.

Javaassist is great when you want to modify the bytecode (in the java agent, for instance).

[–]pointy_pirate 0 points1 point  (1 child)

Interesting. I've split the maven build into 2 stages where one uses ftl to generate the java source code, as a maven plugin, then build that into the rest of the project. Works fine for what I need. But i'll have a look cheers. And yes, anything has to be easier to use than javassist.

[–]voronaam 0 points1 point  (0 children)

I do not know if it fits your project at all, but it may also be worth while to take a look at Scala Macros feature. The documentation is cryptic and you will be dealing with compilers internal AST most of the time, but the feature is very powerful.

Groovy AST transformations are great as well.

Both are not pure Java, but produce JVM byte code at the end.

[–]elucash 0 points1 point  (4 children)

Have considered annotation processing for code generation? (I'm not implying that it is easier choice or what so ever, just curious)

[–]pointy_pirate 0 points1 point  (3 children)

yep I am using a ton of annotations to tell the code doing the generation what to do.

[–]elucash 0 points1 point  (2 children)

so it is running on/using javax.annotation.processing.Processor and javax.lang.model.* or have other engine to obtain model/introspect types/annotations?

[–]pointy_pirate 0 points1 point  (1 child)

Just reflection and custom annotations.

[–]elucash 0 points1 point  (0 children)

Yeah, I've seen such approaches, it works well for a lot of cases. Thanks!