Java annotations provide some information about the code, but do not directly affect the code content it annotates. In this tutorial, we will learn about Java annotations, how to customize annotations, the use of annotations, and how to parse annotations through reflection.
Java 1.5 introduces annotations, and many Java frameworks currently use annotations extensively, such as Hibernate, Jersey, and Spring. Annotations are embedded in the program as metadata of the program. Annotations can be parsed by some parsing tools or compiling tools. We can also declare that annotations have an effect during compilation or execution.
Before using annotations, the program source data only goes through java annotations and javadoc, but the annotations provide much more than these. Annotations not only contain metadata, but also can act on the program's execution sequence. The annotation interpreter can solve the program's execution order through annotation. For example, in Jersey webservice we add a **PATH** annotation in the form of a URI string to the method, then during the program running, the jerser interpreter will determine that the method will call the given URI.
Create Java custom annotations
Creating a custom annotation is similar to creating an interface, but the annotated interface keyword needs to start with the @ symbol. We can declare methods for annotation. Let's first look at the annotation example, and then we will discuss some of its characteristics.
package com.journaldev.annotations; import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target; @Documented@Target(ElementType.METHOD)@Inherited@Retention(RetentionPolicy.RUNTIME) public @interface MethodInfo{ String author() default 'Pankaj'; String date(); int revision() default 1; String comments();} The annotation method cannot carry parameters;
The return value types of the annotation method are limited to: basic types, String, Enums, Annotation or arrays of these types;
Annotation methods can have default values;
Annotations themselves can contain meta annotations, which are used to annotate other annotations.
Here are four types of meta annotations :
1. @Documented -- Indicates that the elements that have this annotation can be documented by tools like javadoc. This type should be used to annotate those types that affect the customer's declaration of using annotated elements. If a declaration is annotated using Documented, this type of annotated annotation is used as a public API for the annotated program member.
2. @Target - Indicates the scope of program elements that annotations of this type can annotate. The values of this meta annotation can be TYPE, METHOD, CONSTRUCTOR, FIELD, etc. If the Target meta annotation does not appear, the defined annotation can be applied to any element of the program.
3. @Inherited -- Indicates that the annotation type is automatically inherited. If the user querys this meta annotation type in the current class and the declaration of the current class does not contain this meta annotation type, it will also automatically query whether the parent class of the current class has an Inherited meta annotation. This action will be repeated to know that the annotation type has been found, or the parent class at the top level is queryed.
4.@Retention - specifies the length of time the Annotation is retained. The RetentionPolicy values are SOURCE, CLASS, RUNTIME.
Java built-in annotations
Java provides three built-in annotations.
1. @Override - When we want to rewrite the method in the parent class, we need to use this annotation to inform the compiler that we want to rewrite this method. This way the compiler will prompt an error message when the method in the parent class is removed or changes occur.
2. @Deprecated -- When we want the compiler to know that a method is not recommended, we should use this annotation. Java recommends this annotation in javadoc, we should provide why this method is not recommended and alternative methods.
3. @SuppressWarnings - This is just to tell the compiler to ignore specific warning messages, such as using native data types in generics. Its retention policy is SOURCE (Translator's Note: valid in the source file) and is discarded by the compiler.
Let’s take a look at an example of built-in annotations in Java, refer to the custom annotations mentioned above.
package com.journaldev.annotations; import java.io.FileNotFoundException;import java.util.ArrayList;import java.util.List; public class AnnotationExample { public static void main(String[] args) {} @Override@MethodInfo(author = 'Pankaj', comments = 'Main method', date = 'Nov 17 2012', revision = 1)public String toString() { return 'Overriden toString method';} @Deprecated@MethodInfo(comments = 'deprecated method', date = 'Nov 17 2012')public static void oldMethod() { System.out.println('old method, don't use it.');} @SuppressWarnings({ 'unchecked', 'deprecation' })@MethodInfo(author = 'Pankaj', comments = 'Main method', date = 'Nov 17 2012', revision = 10)public static void genericsTest() throws FileNotFoundException { List l = new ArrayList(); l.add('abc'); oldMethod();} }I believe this example can be self-explanatory and can be displayed in different scenarios.
Java annotation analysis
We will use reflection technology to parse the annotations of java classes. Then the annotation RetentionPolicy should be set to RUNTIME otherwise the annotation information of the java class will not be available during execution, so we cannot get any data related to the annotation from it.
package com.journaldev.annotations; import java.lang.annotation.Annotation;import java.lang.reflect.Method; public class AnnotationParsing { public static void main(String[] args) { try { for (Method method : AnnotationParsing.class .getClassLoader() .loadClass(('com.journaldev.annotations.AnnotationExample')) .getMethods()) { // checks if MethodInfo annotation is present for the method if (method.isAnnotationPresent(com.journaldev.annotations.MethodInfo.class)) { try { // iterates all the annotations available in the method for (Annotation anno : method.getDeclaredAnnotations()) { System.out.println('Annotation in Method ''+ method + '' : ' + anno); } MethodInfo methodAnno = method.getAnnotation(MethodInfo.class); if (methodAnno.revision() == 1) { System.out.println('Method with revision no 1 = '+ method); } } catch (Throwable ex) { ex.printStackTrace(); } } } } catch (SecurityException | ClassNotFoundException e) { e.printStackTrace(); } } } Running the above program will output:
Annotation in Method 'public java.lang.String com.journaldev.annotations.AnnotationExample.toString()' : @com.journaldev.annotations.MethodInfo(author=Pankaj, revision=1, comments=Main method, date=Nov 17 2012)Method with revision no 1 = public java.lang.String com.journaldev.annotations.AnnotationExample.toString()Annotation in Method 'public static void com.journaldev.annotations.AnnotationExample.oldMethod()' : @java.lang.Deprecated()Annotation in Method 'public static void com.journaldev.annotations.AnnotationExample.oldMethod()' : @com.journaldev.annotations.MethodInfo(author=Pankaj, revision=1, comments=deprecated method, date=Nov 17 2012)Method with revision no 1 = public static void com.journaldev.annotations.AnnotationExample.oldMethod()Annotation in Method 'public static void com.journaldev.annotations.AnnotationExample.genericsTest() throws java.io.FileNotFoundException' : @com.journaldev.annotations.MethodInfo(author=Pankaj, revision=10, comments=Main method, date=Nov 17 2012)
That's all about this tutorial, and hopefully you can learn something from it.