Method Class | getParameterAnnotations() method in Java
The java.lang.reflect.Method.getParameterAnnotations() method of Method class returns a two-dimensional Annotation array, that represents the annotations on the parameters, of the Method object. If the Method contains no parameters, an empty array will be returned. If the Method 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.
At the time of compilation mandated and synthetic parameters are added to the array. Mandated parameter are parameters which implicitly declared in source and Synthetic parameter are parameters that are neither implicitly nor explicitly declared in the source.
The objects of annotation returned by this method are serializable. The array of arrays returned by this method can be easily modified.
public Annotation getParameterAnnotations()
Return Value: This method returns a two-dimensional Annotation array, that represent represent the annotations on the formal and implicit parameters, of the Method object.
Below programs illustrates getParameterAnnotations() method of Method class:
Example 1: To use getParameterAnnotations() for a Specific Method given as Input.
The program contains a Method name setManyValues() which contains parameter Annotations. So this program is going to get all parameter Annotations contain by setManyValues() Method.
Method Name: setManyValues key of annotation: methodParameter1 value of annotation: Some value
Example 2: To use getParameterAnnotations() for Methods of class GFG, if method contains ParameterAnnotations.
Method Name: getValues Annotation length: 1 field1 of annotation: GFG field2 of annotation: Works field3 of annotation: fine field4 of annotation: Hurray!! Method Name: setValue Annotation length: 0
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.