Java Annotations
Java Annotations
Simply speaking, an annotation is a mechanism for associating a meta-tag with program elements and
allowing the compiler or the VM to extract program behaviors from these annotated elements and
generate interdependent code when necessary.
An annotation type definition takes an "at" (@) sign, followed by the interface keyword plus the annotation
name. On the other hand, an annotation takes the form of an "at" sign (@), followed by the annotation
type. This is simplest form of annotation. Additionally, you can put data within parenthesis after the
annotation name. An example of each can be seen below:
Marker: Marker type annotations have no elements, except the annotation name itself.
Example:
Usage:
@MyAnnotation
public void mymethod() {
....
}
Example:
Usage:
Full-value or multi-value: Full-value type annotations have multiple data members. Therefore,
you must use a full data=value parameter syntax for each member.
Example:
1. Annotation declaration should start with an 'at' sign like @, following with an interface keyword,
following with the annotation name.
2. Method declarations should not have any parameters.
3. Method declarations should not have any throws clauses.
4. Return types of the method should be one of the following:
primitives
String
Class
enum
array of the above types
Simple annotations: These are the basic types supplied with Tiger, which you can use to
annotate your code only; you cannot use those to create a custom annotation type.
Meta annotations: These are the annotation types designed for annotating annotation-type
declarations. Simply speaking, these are called the annotations-of-annotations.
The Three Simple Java Annotations
Simple Java Annotations
There are only three types of simple annotations provided by JDK5. They are:
Override
Deprecated
Suppresswarnings
It's important to note that JDK5 (in other words, Tiger) actually does not have many built-in annotations;
rather, it allows core Java the ability to support the annotation feature. The charter for JSR-175 strictly
dictated it was to define a metadata facility. It was left to the programmers to write custom annotation
types and to other JSRs to write a set of standard annotation types. The following sections will describe
each simple annotation in more depth, along with examples.
What happens if a spelling mistake occurs with the method name? For example, if you change the name
of the toString method to "tostring" and compile the code, you will get something like the following:
The doSomething() method in this example is declared as a deprecated method. Therefore, this method
should not be used when this class is instantiated by other classes. If you compile Test_Deprecated.java,
no warning messages will be generated by the compiler. But, if you try to compile TestAnnotations.java
where the deprecated method is used, you will see something like this:
In this example, you are suppressing the deprecation warning for the method listing shown in Example 2.
Because the method is suppressed, you are unlikely to view the "deprecation" warning any more.
Note: It is a good idea to use this annotation at the most deeply nested element where it is effective.
Therefore, if you want to suppress a warning in a particular method, you should annotate that method
rather than its class.
Target
Retention
Documented
Inherited
The Target Annotation
The target annotation indicates the targeted elements of a class in which the annotation type will be
applicable. It contains the following enumerated types as its value:
@Target(ElementType.METHOD)
public @interface Test_Target {
public String doTestTarget();
}
The only change you can see from above is that the annotation declaration is shifted from method-level to
field-level, which is not correct. Because you have defined your annotation @Test_Target to be
applicable only at method-level, if you try to compile this class, you are likely to get something like this:
"TestAnnotations.java":
D:R_AND_DTestAnnotationsrctestmyannotation
TestAnnotations.java:16:
annotation type not applicable to this kind of declaration at line
16, column 0
@Test_Target(doTestTarget="Hello World !")
^
Error in javac compilation
Now, if you run the javadoc command and view the generated TestAnnotations.html file, you will see
something like Figure 1.
As you can see from the screenshot, there is no annotation-type information for the
doSomeTestRetention() method. But, this description is provided for the doSomeTestDocumented()
method. This is because of the @Documented tag attached with your Test_Documented annotation. Your
earlier annotation Test_Retention did not include this tag.
@Inherited
public @interface myParentObject {
boolean isInherited() default true;
String doSomething() default "Do what?";
}
@myParentObject
public Class myChildObject {
}
As you can see, you do not have to define the interface methods inside the implementing class. These
are automatically inherited because of using the @Inherited tag. What would happen if you define the
implementing class in old-fashioned Java-style? Take a look at this—defining the implementation class in
an old-style-java way:
Do you see the difference? You can see that you will have to implement all the methods that the parent
interface owns. Besides the isInherited() and doSomething() methods from myParentObject, you will have
to implement the equals(), toString(), and hasCode() methods of java.lang.Object and also the
annotationType() method of java.lang.annotation.Annotation class. It does not matter whether you want to
implement these methods or not; you will have to include these in your inherited object.
Conclusion
This article has shown you how you can make your development easier through the use of JDK5's
annotation feature. Annotations do not directly affect the semantics of a program. Development and
deployment tools can read these annotations and process them in some fashion, perhaps producing
additional Java programming language source files, XML documents, or other artifacts to be used in
conjunction with the program containing the annotations. You now can do the same things as you would
do before, but with less code and better compile-time error detection. The objective is to spend less time
on unhandy code-writing and focus more on business logic rules. This article is Part I of a series on Java
Annotations. In Part II, you will learn about how to use annotations to develop a simple Web application
with a flat table. Lastly, in Part III you will see a complex example that includes multiple database tables
relationships.