Here is an example annotation type declaration:
public @interface RequestForEnhancement {
int id();
String synopsis();
String engineer() default "[unassigned]";
String date(); default "[unimplemented]";
}
Once an annotation type is defined, you can use it to annotate declarations. Annotations consist of an at-sign (@) followed by an annotation type and a parenthesized list of element-value pairs. The values must be compile-time constants. Here is a method declaration with an annotation corresponding to the annotation type declared above:
@RequestForEnhancement(
id = 2868724,
synopsis = "Enable time-travel",
engineer = "Mr. Peabody",
date = "4/1/3007"
)
public static void travelThroughTime(Date destination) { ... }
42
Originally posted by Jeroen T Wenting:
I've seen very few systems where they were useful, and none at all where some other existing language feature couldn't have done the same thing.
Only place I've ever used them myself is to mark a set of classes which are controlled through a servlet with the request parameters each individual class supports so I can build the form for each of them on the fly.
Would have been possible using several other systems as well, but I thought it would be a nice experiment for an otherwise pretty useless part of the language.
Tony Morris
Java Q&A (FAQ, Trivia)
42
I've seen very few systems where they were useful, and none at all where some other existing language feature couldn't have done the same thing
Annotations are however the worst that's made it into a released version of the platform so far
Originally posted by Jeroen T Wenting:
You seem to hate Java with a vengeance.
Tony Morris
Java Q&A (FAQ, Trivia)
You can't wake a person who is <b><i>pretending</i></b> to be asleep.<br />Like what <b>"it"</b> does not like - <i> Gurdjieff </i>
Originally posted by Jeroen T Wenting:
I've seen very few systems where they were useful, and none at all where some other existing language feature couldn't have done the same thing.
Originally posted by Jeroen T Wenting:
Annotations are however the worst that's made it into a released version of the platform so far (though Mustang will see more madness, with the inclusion of a complete database engine, SOAP server, and webserver as part of the core API).
Originally posted by jaikiran pai:
Thats right. After having a brief look at Annotations, i do prefer sticking with the traditional approach in Java.
Note that the rest of the world is going to use annotations, whether you like it or not, and if you don't know annotations, your'e going to stay behind.
What exactly does this mean, in simple terms. My interpretation is, we are defining an annotation type which contains 4 methods. Is my understanding correct? And is this all the above type declaration conveys, or is there something more?
Now, how do i interpret this piece? I could not understand much out of this, except for the fact that we have a method travelThroughTime signature preceeded by the annotation. What purpose does the annotation serve to this method? And what significance do the values in the annotation declaration hold? I also see that the elements in the element-value pair have the same name as the method names in the annotation type(Ex: the id() method in the annotation type and the id=2868724 element in the annotation declaration). Is this just a co-incidence or does this serve a purpose?
You'll note that the @Test annotation from JUnit 4 is taken directly from JTiger/TestNG which both use annotations for contract metadata.
Tony Morris
Java Q&A (FAQ, Trivia)
The things you define in the annotation type declaration are not really methods, they're really the parameters of the declaration type
The interpretation is this: The method travelThroughTime(...) is annotated with a RequestForEnhancement annotation. So there is a request for enhancement associated with this method
id = 2868724,
synopsis = "Enable time-travel",
engineer = "Mr. Peabody",
date = "4/1/3007"
Ofcourse it's not a coincidence that the parameter names are the same as the "methods" that you see in the annotation declaration.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
You could access the information at runtime via reflection
You can also do it by using reflection as Ilja says, but that works only if you made sure that the annotations are retained in the class file (normally, the compiler throws away the annotations)