Do any Java libraries use annotations for code generation?

时光总嘲笑我的痴心妄想 提交于 2019-12-04 13:40:25

问题


Is anyone aware of a library that uses the the techniques (annotations and classworking) described in this article for automatically generating the standard Object methods toString(), equals() and hashcode() for standard java classes?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/1170417/do-any-java-libraries-use-annotations-for-code-generation

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