April 18, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

The Three Simple Java Annotations, Page 2

  • October 14, 2005
  • By M. M. Islam Chisty
  • Send Email »
  • More Articles »

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.

The Override Annotation

An override annotation indicates that the annotated method is required to override a method in a super class. If a method with this annotation does not override its super-class's method, the compiler will generate an error. Example 1 demonstrates the override annotation:

Java Annotation Example 1

public class Test_Override {
@Override
public String toString() {
return super.toString() +
" Testing annotation name: 'Override'";
}
}

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:

Compiling 1 source file to D:tempNew Folder (2)
TestJavaApplication1buildclasses
D:tempNew Folder (2)TestJavaApplication1srctest
myannotationTest_Override.java:24: method does not override
a method from its superclass
@Override
1 error
BUILD FAILED (total time: 0 seconds)

The Deprecated Annotation

This annotation indicates that when a deprecated program element is used, the compiler should warn you about it. Example 2 shows you the deprecated annotation.

Java Annotation Example 2

First, create a class with the deprecated method as follows:

public class Test_Deprecated {
@Deprecated
public void doSomething() {
System.out.println("Testing annotation name: 'Deprecated'");
}
}

Next, try to invoke this method from another class:

public class TestAnnotations {
public static void main(String arg[]) throws Exception {
new TestAnnotations();
}
public TestAnnotations() {
Test_Deprecated t2=new Test_Deprecated();
t2.doSomething();
}

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:

Compiling 1 source file to D:tempNew Folder
(2)TestJavaApplication1buildclasses
D:tempNew Folder
(2)TestJavaApplication1srctestmyannotation
TestAnnotations.java:27:
warning: [deprecation] doSomething() in
test.myannotation.Test_Deprecated has been deprecated
t2.doSomething();
1 warning

The Suppresswarnings Annotation

This annotation indicates that compiler warnings should be shielded in the annotated element and all of its sub-elements. The set of warnings suppressed in an element is the superset of the warnings in all of its containing sub-elements. As an example, if you annotate a class to suppress one warning and one of its methods to suppress another warning, both warnings will be suppressed at the method level only. See Example 3 for the suppresswarnings annotation.

Java Annotation Example 3

public class TestAnnotations {
public static void main(String arg[]) throws Exception {
new TestAnnotations().doSomeTestNow();
}
@SuppressWarnings({"deprecation"})
public void doSomeTestNow() {
Test_Deprecated t2 = new Test_Deprecated();
t2.doSomething();
}
}

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.

Meta-Annotations (Java Annotation Types)

Meta-annotations, which are actually known as the annotations of annotations, contain four types. These are:

  • 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.TYPE)—can be applied to any element of a class
  • @Target(ElementType.FIELD)—can be applied to a field or property
  • @Target(ElementType.METHOD)—can be applied to a method level annotation
  • @Target(ElementType.PARAMETER)—can be applied to the parameters of a method
  • @Target(ElementType.CONSTRUCTOR)—can be applied to constructors
  • @Target(ElementType.LOCAL_VARIABLE)—can be applied to local variables
  • @Target(ElementType.ANNOTATION_TYPE)—indicates that the declared type itself is an annotation type

Example 4 demonstrates the target annotation:

Java Annotation Example 4

First, define an annotation named Test_Target with @Target metadata, as follows:

@Target(ElementType.METHOD)
public @interface Test_Target {
public String doTestTarget();
}

Next, create a class that will use the Test_Target annotation:

public class TestAnnotations {
public static void main(String arg[]) {
new TestAnnotations().doTestTarget();
}
@Test_Target(doTestTarget="Hello World !")
public void doTestTarget() {
System.out.printf("Testing Target annotation");
}
}

The @Target(ElementType.METHOD) indicates that this annotation type can be used to annotate only at the method levels. If you compile the preceding code, no warning messages will be shown. Now, if you declare a String variable and apply your newly created annotation, what will happen? Let me demonstrate this as follows:

public class TestAnnotations {
@Test_Target(doTestTarget="Hello World !")
private String str;
public static void main(String arg[]) {
new TestAnnotations().doTestTarget();
}
public void doTestTarget() {
System.out.printf("Testing Target annotation");
}
}

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




Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel