Do any Java libraries use annotations for code generation?

杀马特。学长 韩版系。学妹 提交于 2019-12-03 07:35:48

Yes, project Lombok does this. See http://projectlombok.org . It not only supports javac, but also Eclipse. So the methods are not in the source code, but are displayed in the outline view.

I certainly haven't seen this and I'm not really sure what value would be gained from it. I find that automatic toString generation is not usually what you want when scouring log files and most IDEs will have tools to allow you to generate hashCode and equals implementations, although granted these do not automatically reflect any schema modifications within your class.

There would be 2 options for implementing this:

  1. Compile-time class annotation which is processed (e.g. by your IDE) to generate the relevant methods. Can annotations modify the generated bytecode of the classes they are declared in? I think this appraoch is a really bad idea because you can't tell what is going on by looking at your source code
  2. Runtime annotation on the relevant methods to indicate that they should be evaluated via the special mechanism. This still requires you to declare and annotate your methods and I'm also not sure how you would actually intercept method calls to use the annotation in any event
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!