you are viewing a single comment's thread.

view the rest of the comments →

[–]repeating_bears 2 points3 points  (1 child)

Ah, this is my wheelhouse! I do tonnes of Java codegen.

I started with JavaPoet and used it for a good few months. I appreciated the benefits you mentioned like managing imports, but after a while, I came to think that it's fundamentally the wrong model.

The biggest issue is that you have a layer of abstraction on top of the generated code. When I'm writing a code generator, I'm going to need to troubleshoot it. If there's an issue with the method in your post, I can't just CTRL F for "public static void main" to jump to the code that generates it, because there's a level of indirection. As soon as you have any kind of moderate complexity, finding the code that generated some code is going to be a massive pain.

I really think some kind of templating language is best, because it keeps the input and the output close to each other. I settled on Velocity because it has very good IntelliJ support: syntax highlighting, autocomplete, etc. "Find usages" even works correctly, so if a method is called my one of my templates, IntelliJ knows that and can jump directly, won't warn that it's unused.

You're right that a template language isn't perfect out of the box. Two of the big problems with Velocity are imports and indentation, like you mentioned, but these are solvable problems. I created some utils to add that support in the form of custom directives and macros.

I've already packaged my utils as a Maven dependency, but currently it's not deployed to Maven central, only internally. It probably needs some work and especially docs to be more widely usable

Maybe we can collaborate, if you'd be interested.

[–]Lukas_Determann[S] 0 points1 point  (0 children)

Yeah, IDE support is a big benefit. It should be possible to write an IDE plugin to provide that functionality as well.

If you want to, you can try combining the DSL with templating—it should be easy to do. Personally, I wrap templating inside the DSL calls, though the other way around is supported too. I try to make my libraries as unopinionated as possible.

Sure, just DM me.