Thursday, July 4, 2024
HomeLanguagesJavaField getAnnotationsByType() method in Java With Examples

Field getAnnotationsByType() method in Java With Examples

The getAnnotationsByType() method of java.lang.reflect.Field is used to return annotations that are associated with this field element. This is an important method to get annotation for Field object. If there are no annotations associated with this Field element, the return empty array. The caller can modify the returned array as method sent a copy of the actual object; it will have no effect on the arrays returned to other callers.

Syntax:

public <T extends Annotation> T[] 
  getAnnotationsByType(Class<T> annotationClass)

Parameters: This method accepts annotationClass which is the Class object corresponding to the annotation type.

Return: This method returns all this element’s annotations for the specified annotation type if associated with this element, else an array of length zero.

Exception: This method throws NullPointerException if the given annotation class is null.

Below programs illustrate getAnnotationsByType() method:
Program 1:




// Java program to illustrate
// getAnnotationsByType() method
  
import java.lang.annotation.*;
import java.lang.reflect.Field;
import java.util.Arrays;
  
public class GFG {
  
    // initialize field with
    // default value in annotation
    @annotations(32512.21)
    private double realNumbers;
  
    public static void main(String[] args)
        throws NoSuchFieldException
    {
  
        // create Field object
        Field field
            = GFG.class
                  .getDeclaredField("realNumbers");
  
        // apply getAnnotationsByType()
        annotations[] annotations
            = field.getAnnotationsByType(
                annotations.class);
  
        // print results
        System.out.println(
            Arrays
                .toString(annotations));
    }
  
    @Target({ ElementType.FIELD })
    @Retention(RetentionPolicy.RUNTIME)
    private @interface annotations {
        double value() default 99.9;
    }
}


Output:

[@GFG$annotations(value=32512.21)]

Program 2:




// Java program to illustrate
// getAnnotationsByType() method
  
import java.lang.annotation.*;
import java.lang.reflect.Field;
import java.util.Arrays;
  
public class GFG {
  
    // initialize field with
    // default value in annotation
    @annotations("ChipherCodes@#!")
    private double string;
  
    public static void main(String[] args)
        throws NoSuchFieldException
    {
  
        // create a Field object
        Field field
            = GFG.class
                  .getDeclaredField("string");
  
        // apply getAnnotationsByType()
        annotations[] annotations
            = field.getAnnotationsByType(
                annotations.class);
  
        // print results
        System.out.println(
            Arrays.toString(
                annotations));
    }
  
    @Target({ ElementType.FIELD })
    @Retention(RetentionPolicy.RUNTIME)
    private @interface annotations {
        String value();
    }
}


Output:

[@GFG$annotations(value=ChipherCodes@#!)]

References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getAnnotationsByType-java.lang.Class-

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments