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.
Syntax:
public Annotation[][] getParameterAnnotations()
Return Value: This method returns a two-dimensional Annotation array, that 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.
Java
// Java program to demonstrate how to // apply getParameterAnnotations() method // of Method Class. import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; public class GFG { // Main method public static void main(String[] args) { try { // create class object Class classobj = GFG. class ; // create method object of setManyValues Method setManyValueObject = null ; Method[] methods = classobj.getMethods(); for (Method m : methods) { if (m.getName().equals("setManyValues")) setManyValueObject = m; } // get Annotation of parameter Annotation[][] Arrayannotations = setManyValueObject .getParameterAnnotations(); System.out.println("Method Name: " + setManyValueObject.getName()); // print parameter annotation for (Annotation[] annotationRow : Arrayannotations) { for (Annotation annotation : annotationRow) { AnnotationDemo anndemo = (AnnotationDemo)annotation; System.out.println("key of annotation: " + anndemo.key()); System.out.println("value of annotation: " + anndemo.value()); } } } catch (Exception e) { e.printStackTrace(); } } // method name setManyValues public void setManyValues( @AnnotationDemo (key = "methodParameter1", value = "Some value") String parameter1) { System.out.println("setManyValues"); } } // create a custom Annotation to apply on method @Retention (RetentionPolicy.RUNTIME) @interface AnnotationDemo { // This annotation has two attributes. public String key(); public String value(); } |
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.
Java
// Java program to demonstrate how to // apply getParameterAnnotations() method // of Method Class. import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; public class GFG { // Main method public static void main(String[] args) { try { // create class object Class classobj = GFG. class ; // create list of method objects Method[] methods = classobj.getMethods(); for (Method m : methods) { // print details for only getValues // and setValue method // so filter list of methods if (m.getName().equals("getValues") || m.getName().equals("setValue")) { // get Annotations of parameter Annotation[][] Arrayannotations = m .getParameterAnnotations(); System.out.println("Method Name: " + m.getName()); // print parameter annotation printAnnotation(Arrayannotations); } } } catch (Exception e) { e.printStackTrace(); } } // method name setValue with // no annotations at parameter public void setValue() { System.out.println("setValue"); } // method name getValues with annotations at the parameter public String getValues( @AnnotationDemo (field1 = "GFG", field2 = "Works", field3 = "fine", field4 = "Hurray!!") String value) { System.out.println("setManyValues"); return value; } public static void printAnnotation(Annotation[][] Arrayannotations) { System.out.println("Annotation length: " + Arrayannotations.length); // print parameter annotation for (Annotation[] annotationRow : Arrayannotations) { for (Annotation annotation : annotationRow) { AnnotationDemo anndemo = (AnnotationDemo)annotation; System.out.println("field1 of annotation: " + anndemo.field1()); System.out.println("field2 of annotation: " + anndemo.field2()); System.out.println("field3 of annotation: " + anndemo.field3()); System.out.println("field4 of annotation: " + anndemo.field4()); } } } } // create a custom Annotation to apply on method @Retention (RetentionPolicy.RUNTIME) @interface AnnotationDemo { // This annotation has many attributes. public String field1(); public String field2(); public String field3(); public String field4(); } |
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
Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getParameterAnnotations–