问题
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:
- 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
- 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