Field getAnnotatedType() method in Java With Examples

The getAnnotatedType() method of java.lang.reflect.Field is used to return an annonatedType object that represents the use of a type to specify the declared type of the field. Every Field of the class is declared by some AnnotatedType.AnnotatedType represents the potentially annotated use of any type including an array type, a parameterized type, a type variable, or a wildcard type currently running in Java Virtual Machine. The returned AnnotatedType instance can be an implementation of AnnotatedType itself or an implementation of one of its sub-interfaces: AnnotatedArrayType, AnnotatedParameterizedType, AnnotatedTypeVariable, AnnotatedWildcardType.

Syntax:

public AnnotatedType getAnnotatedType()

Parameters: This method accepts accepts nothing.



Return: This method returns an object representing the declared type of the field represented by this Field.

Below programs illustrate getAnnotatedType() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// getAnnotatedType() method
  
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Field;
import java.util.Arrays;
  
public class GFG {
  
    private int number;
  
    public static void main(String[] args)
        throws NoSuchFieldException
    {
  
        // get Field object
        Field field
            = GFG.class
                  .getDeclaredField("number");
  
        // apply getAnnotatedType() method
        AnnotatedType annotatedType
            = field.getAnnotatedType();
  
        // print the results
        System.out.println(
            "Type: "
            + annotatedType
                  .getType()
                  .getTypeName());
        System.out.println(
            "Annotations: "
            + Arrays
                  .toString(
                      annotatedType
                          .getAnnotations()));
    }
}

chevron_right


Output:

Type: int
Annotations: []

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to illustrate
// getAnnotatedType() method
  
import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.Arrays;
  
public class GFG {
  
    private int @SpecialNumber[] number;
  
    public static void main(String[] args)
        throws NoSuchFieldException
    {
        // get Field object
        Field field
            = GFG.class
                  .getDeclaredField("number");
  
        // apply getAnnotatedType() method
        AnnotatedType annotatedType
            = field.getAnnotatedType();
  
        // print the results
        System.out.println(
            "Type: "
            + annotatedType
                  .getType()
                  .getTypeName());
        System.out.println(
            "Annotations: "
            + Arrays
                  .toString(
                      annotatedType
                          .getAnnotations()));
        System.out.println(
            "Declared Annotations: "
            + Arrays
                  .toString(
                      annotatedType
                          .getDeclaredAnnotations()));
    }
  
    @Target({ ElementType.TYPE_USE })
    @Retention(RetentionPolicy.RUNTIME)
    private @interface SpecialNumber {
    }
}

chevron_right


Output:

Type: int[]
Annotations: [@GFG$SpecialNumber()]
Declared Annotations: [@GFG$SpecialNumber()]

References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getAnnotatedType–



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.