Open In App

Constructor getParameterAnnotations() method in Java with Examples

Last Updated : 27 Nov, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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 {
}


Output:

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

Program 2:




// 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 {
}


Output:

Parameter annotations -->
@Path()

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads