Constructor getParameterAnnotations() method in Java with Examples

The getParameterAnnotations() method of a Constructor class is used to get a two-dimensional Annotation array that represent the annotations on the formal parameters of this Constructor. If the Constructor contains no parameters, an empty array will be returned. If the Constructor contains one or more parameters then a two-dimension Annotation array will be returned. A nested array of this two-dimension array will be empty for the parameter with no annotations. The objects of annotation returned by this method are serializable. The array of arrays returned by this method can be easily modified.

Syntax:

public Annotation[][] getParameterAnnotations()

Parameters: This method accepts nothing.



Return value: This method returns an array of arrays that represent the annotations on the formal and implicit parameters, in declaration order, of the executable represented by this object.

Below programs illustrate the getParameterAnnotations() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Constructor.getParameterAnnotations() method
  
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Constructor;
import java.util.Arrays;
  
public class GFG {
  
    public static void main(String... args)
        throws NoSuchMethodException
    {
  
        // Create Constructor Object
        Constructor[] constructors
            = Test.class.getConstructors();
  
        // get Annotation array
        Annotation[][] annotations
            = constructors[0].getParameterAnnotations();
  
        System.out.println("Parameter annotations -->");
        System.out.println(Arrays.deepToString(annotations));
    }
}
class Test {
  
    public Test(@Properties Object config)
    {
    }
}
  
@Retention(RetentionPolicy.RUNTIME)
@interface Properties {
}

chevron_right


Output:

Parameter annotations -->
[[@Properties()]]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Constructor.getParameterAnnotations() method
  
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Constructor;
  
public class GFG {
  
    public static void main(String... args)
        throws NoSuchMethodException
    {
  
        // Create Constructor Object
        Constructor[] constructors
            = GfgDemo.class.getConstructors();
  
        // get Annotation array
        Annotation[][] annotations
            = constructors[0].getParameterAnnotations();
  
        System.out.println("Parameter annotations -->");
        for (Annotation[] ann : annotations) {
            for (Annotation annotation : ann) {
                System.out.println(annotation);
            }
        }
    }
}
class GfgDemo {
  
    public GfgDemo(@Path Path path)
    {
    }
}
  
@Retention(RetentionPolicy.RUNTIME)
@interface Path {
}

chevron_right


Output:

Parameter annotations -->
@Path()

References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#getParameterAnnotations()



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.