7

What is/are "good" java code generation libs? I found a generation part in JaxMe, but it's poor and old. I like to generate java code through java code. So basically use a lib to tell that it has to generate a certain concrete class or interface with X fields, Y methods, etc.. that are then written to the file system.. I know that frameworks use them, but can't find a standalone lib.. - Ed

Adam Arold
  • 29,285
  • 22
  • 112
  • 207
edbras
  • 4,145
  • 9
  • 41
  • 78
  • 1
    If your program knows the name of a class it wants to generate, and the name and content of all the members, why on earth do you need a code generator? Won't a print statement to an output stream be enough? Interesting code generators have to add something by themselves to be useful. – Ira Baxter Nov 10 '12 at 14:10
  • The info you talk about isn't known beforehand and even if it's know, you are talking about a mass of info that changes every year due to law changes... Example: an xbrl/xsd that you want to use to generate code and the corresponding validations... – edbras Nov 11 '12 at 11:22
  • Duplicate of http://stackoverflow.com/questions/121324/a-java-api-to-generate-java-source-files – Adam Gent Mar 12 '15 at 00:39

4 Answers4

1

There are several libraries with various capabilities and ease of use:

You will probably have to take a look at the API of each one to determine which one is more suitable for your purposes.

Dániel Kis
  • 2,341
  • 5
  • 28
  • 51
thkala
  • 84,049
  • 23
  • 157
  • 201
  • 7
    These all (except maybe cglib) seem to be byte-code-editing tools, not source code generators... – Markus A. Nov 10 '12 at 16:30
  • Thanks for the feedback, but like already mentioned these are mainly byte code generators, and I am talking about source code generators. I notice that there isn't a mature stand-alone source code generator project, but mostly sub project like codemodel and apache JaxMe... which often are "old" – edbras Nov 11 '12 at 11:20
  • @edbras: I thought you where asking about bytecode generation, since that is what most people mean when talking about code generation in Java. In general, code generators are intended to allow the use of higher-level abstractions. What would that abstraction be in your case, considering the output is already Java *source* code? – thkala Nov 11 '12 at 13:32
  • Sorry for not being clear. Example usage: xbrl/xsd file that I use to generate code including validations (maxOccurs/minOccurs/fractionDigits, etc..). I read this and then generate source code, currently using the apache JaxMe subproject to generate source code – edbras Nov 11 '12 at 18:17
0

Maybe you should be interested in cglib?

Adam Sznajder
  • 9,108
  • 4
  • 39
  • 60
0

I like ASM the best. It uses a visitor pattern that can be a little confusing at first when you are generating code; but once you grasp it, it's really clean and easy.

erickson
  • 265,237
  • 58
  • 395
  • 493
0

I've had a look into this a little while back as well and had a pretty hard time finding anything useful. The most promising candidate I found was actually built right into the compiler:

Java annotation processing (APT)

They recently included APT with javac, so it's always available.

I haven't really had too much time yet to look into it, but at first glance, it seems infinitely powerful (generate/modify source code or byte code, throw errors, warnings, ...) but it could be a bit complex to wrap your head around. There might be some good tutorials out there, but I don't have a specific link for you at the moment.

Markus A.
  • 12,349
  • 8
  • 52
  • 116
  • Annotation processing sounds pretty amazing, until you get down to actually trying to use it. Last time I checked, you had to jump through an incredible amount of hoops to e.g. get the current source file and you *still* need to mess with your build system to use it... sometimes I just miss the C preprocessor... – thkala Nov 11 '12 at 13:30