Looking for a java code generation library [closed]

五迷三道 提交于 2019-12-01 03:35:36

问题


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


回答1:


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

  • Javassist
  • Apache BCEL
  • ASM
  • cglib

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




回答2:


Maybe you should be interested in cglib?




回答3:


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.




回答4:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!