Wednesday, January 15, 2020

Annotations in Java(user defined Annotations) part-2

User defined Annotations


before understanding user defined Annotations in Java, we need to know about few Annotations those apply to other Annotations.

  • @Retention
  • @Target
  • @Documented
  • @Inherited
  • @Repeatable


@Retention 

Specifies how the marked Annotation will be stored.There can be three retention policies.

RetentionPolicy.SOURCE – The marked annotation is retained only in the source level and is ignored by the compiler.

RetentionPolicy.CLASS – The marked annotation is retained by the compiler at compile time, but is ignored by the Java Virtual Machine (JVM).

RetentionPolicy.RUNTIME – The marked annotation is retained by the JVM so it can be used by the run-time environment.

@Target

@Target annotation specify other annotations to which elements this annotation will be applied. A target annotation indicates one of the following element types as its value:

ANNOTATION_TYPE can be applied to an annotation type.
CONSTRUCTOR can be applied to a constructor.
FIELD can be applied to a field or property.
LOCAL_VARIABLE can be applied to a local variable.
METHOD can be applied to a method-level annotation.
PACKAGE can be applied to a package declaration.
PARAMETER can be applied to the parameters of a method.
TYPE can be applied to any element of a class.

@Documented

@Documented annotation indicates that whenever the specified annotation is used those elements should be documented using the Javadoc tool. By default, annotations are not included in Javadoc.

Javadoc is software tool, that parses the declarations and documentation comments in a set of source files and produces a set of HTML pages describing the classes, interfaces, constructors, methods, and fields.

@Inherited

@Inherited annotation specifies that the annotation type can be inherited from the super class. (This is not true by default.) When the user queries the annotation type and the class has no annotation for this type, then the super class of this class is queried for the annotation type. This annotation applies only to class declarations.

@Repeatable

@Repeatable annotation, introduced in Java SE 8, indicates that the marked annotation can be applied more than once to the same declaration or type use.

Example
/************************************************************/
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Documented
@Retention(RUNTIME)
@Target({ TYPE, FIELD, METHOD })

/**
 * @author Pushpendra
 *
 */
public @interface TestAnnotation {
int a() default 0;
String name() default "pushpendra";

}
/*****************************************************************/

class SuperA
{
void m1()
{
System.out.println("Super method");
}
}
public class A extends SuperA{
@Override
@TestAnnotation(a=10,name="Pushpendra")
public void m1()
{
System.out.println("Subclass method");
}
public static void main(String[] args) throws NoSuchMethodException, SecurityException {
A a=new A();
Method m=a.getClass().getMethod("m1");
TestAnnotation t=m.getAnnotation(TestAnnotation.class);
System.out.println(t.a()+"\t"+t.name());
}

}
/******************************************************************/
Output:

10 Pushpendra

Video Tutorial