Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Method Class | getParameterTypes() Method in Java
Next article icon

Method Class | getParameterAnnotations() method in Java

Last Updated : 19 May, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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(); } 
Output:
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(); } 
Output:
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--


Next Article
Method Class | getParameterTypes() Method in Java

A

AmanSingh2210
Improve
Article Tags :
  • Java
  • Java-lang package
  • Java-Functions
  • Java-Method Class
  • java-lang-reflect-package
Practice Tags :
  • Java

Similar Reads

  • Method Class | getAnnotation() method in Java
    The java.lang.reflect.Method.getAnnotation(Class< T > annotationClass) method of Method class returns Method objects' annotation for the specified type passed as parameter if such an annotation is present, else null. This is important method to get annotation for Method object. Syntax: public
    3 min read
  • Method Class | getDeclaredAnnotations() method in Java
    The java.lang.reflect.Method.getDeclaredAnnotations() method of Method class returns annotations declared only on the method and ignores inherited annotations by method. If there are no annotations directly declared on the method, the returned array of annotation is empty. Modification of the return
    3 min read
  • Method Class | getParameterCount() method in Java
    The java.lang.reflect.Method.getParameterCount() method of Method class returns the number of parameters declared on a method Object. Syntax: public int getParameterCount() Return Value: This method returns number of formal parameters defined on this Method Object. Below programs illustrates getPara
    3 min read
  • Method Class | getParameterTypes() Method in Java
    Prerequisite : Java.lang.Class class in Java | Set 1, Java.lang.Class class in Java | Set 2 java.lang.reflectMethod class help us to get information of a single method on a class or interface. This class also provides access to the methods of classes and invoke them at runtime. getParameterTypes() m
    4 min read
  • Method Class | getTypeParameters() Method in Java
    The java.lang.reflect.Method.getTypeParameters() method of Method class returns an array of TypeVariable objects declared by the generic declaration of this Method object, in declaration order. Elements of array represent the type variables objects declared by Method. An array of length 0 is returne
    4 min read
  • Method Class | getGenericParameterTypes() method in Java
    The java.lang.reflect.Method.getGenericParameterTypes() method of Method class returns an array of Type objects that represent the parameter types, declared in method at time of coding. It means that the getGenericParameterTypes() method returns array of parameters that belongs to method object. It
    4 min read
  • Class getAnnotations() method in Java with Examples
    The getAnnotations() method of java.lang.Class class is used to get the annotations present in this class. The method returns an array of annotations present. Syntax: public Annotation[] getAnnotations() Parameter: This method does not accepts any parameter. Return Value: This method returns an arra
    2 min read
  • Method Class | getReturnType() Method in Java
    Prerequisite : Java.lang.Class class in Java | Set 1, Java.lang.Class class in Java | Set 2The java.lang.reflectMethod Class help in getting information of a single method on a class or interface. This class also provides access to the methods of classes and invoke them at runtime. The getReturnType
    4 min read
  • Class getAnnotation() method in Java with Examples
    The getAnnotation() method of java.lang.Class class is used to get the annotation of the specified annotation type, if such an annotation is present in this class. The method returns that class in the form of an object. Syntax: public T getAnnotation(Class<T> annotationClass) Parameter: This m
    2 min read
  • Method Class | getName() Method in Java
    The getName() method of java.lang.reflect.Method class is helpful to get the name of methods, as a String. To get name of all methods of a class, get all the methods of that class object. Then call getName() on those method objects. Syntax: public String getName() Return Value: It returns the name o
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences