Welcome to the third and final part of this three-part series on Sun Microsystems' latest release of the Java 2 Platform, Standard Edition (J2SE). As I mentioned in Part 1, J2SE 5 (also called Tiger) is the most significant revision to the Java language since its original inception and has extended Java with several new features. To refresh your memory, Part 1 provided a brief introduction to J2SE 5 and covered many new additions to the Java language. Part 2 was devoted entirely to generics, which are Java's counterpart to templates in C++ and a similar facility (also called generics) in C# 2.0. In this final installment, I focus exclusively on the newly introduced metadata feature in J2SE 5 called annotations. Annotations allow programmers to decorate Java code with their own attributes. These attributes can be used for code documentation, code generation, and, even during runtime, for providing special services such as enhanced business-level security or special business logic.
Read the whole series: "Taming Tiger," Tarak Modi (JavaWorld):
If you look up the definition of metadata, the dictionary will tell you it's data about data. The same definition applies to metadata in Java as well. Metadata is not a completely foreign concept to Java. Java has always supported a (very) limited implementation of metadata via its javadoc tags. An example code fragment follows:
/**
* @author Tarak Modi
*
*/
public final class AnnotationsTest
{
}
In the above code fragment, the @author tag is an example of metadata for the AnnotationsTest class. In other words, AnnotationsTest is annotated or decorated by the author metadata. Currently, this metadata is only used by tools such as javadoc and is not even available during runtime. XDoclet,
an open source project, is another example that uses javadoc-style metadata tags to annotate and generate code based on the
metadata.
With J2SE 5, the Java team has taken Java's metadata capability to a new level. The concept is formally called annotations. In addition to the built-in annotations included in Version 5, you can also use custom annotations that you define to decorate types (such as classes, interfaces, and enums), methods and constructors, and fields. You can control the availability of these annotations as well. For example, some annotations may be available only in the source code; others may be available in the compiled class files as well. Some annotations may even be available during runtime in the JVM.
J2SE 5 comes with six prebuilt annotations. Each annotation is described below. The annotation's actual definition is also provided in the reference's description. Two points to note from the definitions are their simplicity and that the annotations themselves are annotated. Later in this article, I walk you through a complete example of building and using your own annotation; during that discussion, I explain the declaration's syntax.
| Subject |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
At this sight the animals’By csy211 on October 15, 2009, 2:26 amAt this sight the animals’ courage returned to them. wow goldAion kinahThe fear and despair they had felt a moment earlier were drowned in their rage against this...
Reply | Read entire comment
But when the animals saw theBy csy211 on October 15, 2009, 2:23 amBut when the animals saw the green flag flying, and heard the gun firing again-seven times it was fired in all -- and heard the speech that Napoleon made, wow goldwow...
Reply | Read entire comment
A cry of lamentation wentBy csy211 on October 15, 2009, 2:20 amA cry of lamentation went up. Aion time cardbuy aion kinahStraw was laid down outside the doors of the farmhouse, and the animals walked on tiptoe. With tears in...
Reply | Read entire comment
Example of AnnotationBy leapingfrog on March 2, 2009, 12:51 pmThis example worked for me. What was missing was the headers. Maybe the authors IDE filled in the blanks but it took me a while since I used a simple editor. In...
Reply | Read entire comment
Wrong annotation typeBy Anonymous on January 29, 2009, 2:17 amCorrect one should be java.lang.Override.
Reply | Read entire comment
View all comments