Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Taming Tiger, Part 3

Decorate your code with Java annotations

  • Print
  • Feedback

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):



What is metadata?

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.

Built-in annotations

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.

  • Print
  • Feedback

Resources