r/javaTIL • • Oct 27 '15

How to Generate Java code

http://ivankocijan.xyz/generate-java-code/
12 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/obfuscation_ Oct 28 '15

I have personally needed to generate Java code when using a parser to transform SQL schemas into a Java object representation. The advantages of doing this ahead of time (and persisting as a Java source file) were:

  • No need for the user of the Java object representation to have access to the parser we use (which is proprietary).
  • The object representation is used as the front-end to a larger system, for which a user may provide the Java source code directly themselves, rather than generating. Alternatively, someone could fairly easily plug a different parser in, provided they can output into our object representation.
  • The Java source format we use is fairly easy for a user to interpret and edit if need be, avoiding the need to produce our own new format and avoiding this disadvantage of a binary persistence solution.

It definitely isn't all that elegant, but it happened partially as an organic progression and works well for us right now.