Java Package getAnnotation() Method



Description

The Java Package getAnnotation(Class<A> annotationClass) method returns this element's annotation for the specified type if such an annotation is present, else null.

Declaration

Following is the declaration for java.lang.Package.getAnnotation() method

public <A extends Annotation> A getAnnotation(Class<A> annotationClass)

Parameters

annotationClass − the Class object corresponding to the annotation type

Return Value

This method returns this element's annotation for the specified annotation type if present on this element, else null

Exception

  • NullPointerException − if the given annotation class is null

  • IllegalMonitorStateException − if the current thread is not the owner of the object's monitor.

Getting Annotation of Given Type Example

The following example shows the usage of getAnnotation() method. In this program, we've declared an annotation Demo as an interface with a method returing a string and a another method return int value. In PackageDemo class, we've applied that annotation Demo on a method example() with some values. In example() method, we've retrieved the class of the PackageDemo class using getClass() method.

Now using getMethod() example, we retrieved example method instance and then using getAnnotation() method, we've passed Demo Class as argument to get the annotation object. Annotation object is then used to get the assigned values to the annotation applied. In the main method, this example() method is called and result is printed.

package com.tutorialspoint;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

// declare a new annotation
@Retention(RetentionPolicy.RUNTIME)
@interface Demo {
   String str();
   int val();
}

public class PackageDemo {

   // set values for the annotation
   @Demo(str = "Demo Annotation", val = 100)
   // a method to call in the main
   public static void example() {
      PackageDemo ob = new PackageDemo();

      try {
         Class c = ob.getClass();

         // get the method example
         Method m = c.getMethod("example");

         // get the annotation for class Demo
         Demo annotation = m.getAnnotation(Demo.class);

         // print the annotation
         System.out.println(annotation.str() + " " + annotation.val());
      } catch (NoSuchMethodException exc) {
         exc.printStackTrace();
      }
   }
   public static void main(String args[]) {
      example();
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Demo Annotation 100

Facing Exception While Getting Annotation of Given Type Example

The following example shows the usage of getAnnotation() method. In this program, we've declared an annotation Demo as an interface with a method returing a string and a another method return int value. In PackageDemo class, we've applied that annotation Demo on a method example() with some values. In example() method, we've retrieved the class of the PackageDemo class using getClass() method.

Now using getMethod() example, we retrieved example method instance and then using getAnnotation() method, we've passed null as argument to get the annotation object. As it is causing a NullPointerException, exception handler block prints the error message when program is executed.

package com.tutorialspoint;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

// declare a new annotation
@Retention(RetentionPolicy.RUNTIME)
@interface Demo {
   String str();
   int val();
}

public class PackageDemo {

   // set values for the annotation
   @Demo(str = "Demo Annotation", val = 100)
   // a method to call in the main
   public static void example() {
      PackageDemo ob = new PackageDemo();

      try {
         Class c = ob.getClass();

         // get the method example
         Method m = c.getMethod("example");

         // get the annotation 
         Demo annotation = m.getAnnotation(null);

         if(annotation != null) {
        	 // print the annotation
        	 System.out.println(annotation.str() + " " + annotation.val());
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   public static void main(String args[]) {
      example();
   }
}

Output

Let us compile and run the above program, this will produce the following result −

java.lang.NullPointerException
	at java.base/java.util.Objects.requireNonNull(Objects.java:233)
	at java.base/java.lang.reflect.Executable.getAnnotation(Executable.java:599)
	at java.base/java.lang.reflect.Method.getAnnotation(Method.java:793)
	at com.tutorialspoint.PackageDemo.example(PackageDemo.java:29)
	at com.tutorialspoint.PackageDemo.main(PackageDemo.java:40)
java_lang_package.htm
Advertisements