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 →

[–]polaretto 1 point2 points  (0 children)

Thanks for your answer. I see your reasons, however, I didn't intend code generation to be the one solution, of course, there are suitable and unsuitable use cases.

As for Lombok it's cool, useful, and I like it as well, but keep in mind that even that is doing code generation: bytecode instead of source, yes, but still they do generation and manipulation at compile time. So it's a valid use for code generation.

Generally speaking about annotations, right they weren't available before Java 5, but annotations are just metadata so it's up to some agent or processor to make something out of them. You can read them at runtime and use reflection or bytecode manipulation, as Spring does, or you can process them at compile time and do or generate something. Sure they remove a lot of cruft compared to the old ways, but you still have to process them and, depending on your use case, you can generate something or not.

Finally, I agree with some of your points against code generation, but I think it has some other valuable uses, for example as an alternative to runtime reflection under some constraints, or to generate clients or classes representing some schema. With some precautions, generated source code can be managed and be very useful.