Reputation: 263
Are Java annotations used for adding functionality to Java code besides just adding documentation about what's going on in the code? What's the most advanced/complex functionality you could add to your code through an annotation?
Upvotes: 4
Views: 625
Reputation: 30723
An annotation needs a tool to react to it. If such a tool does not exist the annotation is merely a notation. The "tool" can be an APT
based agent or some piece of code that uses reflection (for instance, JUnit's @Test
).
Several annotations are recognized by the Java compiler and thus have pre-defined semantics: @Override
, @Deprecated
, @Target
.
Upvotes: 2
Reputation: 189464
I would understand Annotations as a way to document your code in a machine readable way. For example in Hibernate you can specify the whole persistence information for your objects as annotations. This is directly readable for you and not in a distant xml file. But is also readable for the tool to generate configurations, database schemes etc.
Upvotes: 1
Reputation: 51311
Annotation are basically not more than a tag (with optional additional data) on a class/method/field. Other code (libraries or tools) can discover these tags and execute functionality dependant on the annotations found. I don't see a real limit on the complexity of the functionality possibly added by annotations. This can for example emulate AOP (adding functionality before or after a method with an annotation).
Upvotes: 2
Reputation: 308021
Annotations as such only add information (metadata) to a class.
One can easily build a system that uses that metadata to provide additional functionality, however.
For example you can use apt
to generate classes based on the information provided by the annotation.
Upvotes: 2