Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Taming Tiger, Part 3

Decorate your code with Java annotations

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.

1 | 2 |  Next >

Discuss

Start a new discussion or jump into one of the threads below:

Subject Replies Last post
. Small bug in sample code.
By Anonymous
1 05/09/08 07:33 AM
by Anonymous
. Multiple occurance of Custom Annotation
By Anonymous
1 05/09/08 03:53 AM
by Anonymous
. interesting and funny
By badis
3 04/22/08 06:03 AM
by Anonymous
. Couple of comments / questions
By csa216
0 04/22/08 06:02 AM
by Anonymous
. Not thread safe?
By mpcooke3
1 04/22/08 06:02 AM
by Anonymous
. Taming Tiger, Part 3
By JavaWorldAdministrator
2 04/22/08 06:02 AM
by Anonymous


Resources