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
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.
Maybe you should be interested in cglib?
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.
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.
来源:https://stackoverflow.com/questions/13322609/looking-for-a-java-code-generation-library