Method Parameter Reflection in Java

Last updated on Dec 27 2022
Prabhas Ramanathan

Java provides a new feature in which you can get the names of formal parameters of any method or constructor. The java.lang.reflect package contains all the required classes like Method and Parameter to work with parameter reflection.

Table of Contents

Method class

It provides information about single method on a class or interface. The reflected method may be a class method or an instance method.

Method Class methods

Method Description
public boolean equals(Object obj) It compares this Method against the specified object. It returns true if the objects are the same. Two Methods are the same if they were declared by the same class and have the same name and formal parameter types and return type.
public AnnotatedType getAnnotatedReturnType() It returns an AnnotatedType object that represents the use of a type to specify the return type of the method/constructor.
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) It returns this element’s annotation for the specified type if such an annotation is present otherwise returns null. NullPointerException – if the given annotation class is null
public Annotation[] getDeclaredAnnotations() It returns annotations that are directly present on this element. This method ignores inherited annotations. If there are no annotations directly present on this element, the return value is an array of length 0. The caller of this method is free to modify the returned array. it will have no effect on the arrays returned to other callers.
public Class<?> getDeclaringClass() It returns the Class object representing the class or interface that declares the executable represented by this object.
public Object getDefaultValue() It returns the default value for the annotation member represented by this Method instance.
public Class<?>[] getExceptionTypes() It returns an array of Class objects that represent the types of exceptions declared to be thrown by the underlying executable represented by this object.
public Type[] getGenericExceptionTypes() It returns an array of Type objects that represent the exceptions declared to be thrown by this executable object. It returns an array of length 0 if the underlying executable declares no exceptions in its throws clause. It throws following exceptions: GenericSignatureFormatError – if the generic method signature does not conform to the format specified in The Java Virtual Machine Specification. TypeNotPresentException – if the underlying executable’s throws clause refers to a non-existent type declaration. MalformedParameterizedTypeException – if the underlying executable’s throws clause refers to a parameterized type that cannot be instantiated for any reason.
public Type[] getGenericParameterTypes() It returns an array of Type objects that represent the formal parameter types. It throws following exceptions: GenericSignatureFormatError – if the generic method signature does not conform to the format specified in The Java Virtual Machine Specification. TypeNotPresentException – if any of the parameter types of the underlying executable refers to a non-existent type declaration. MalformedParameterizedTypeException – if any of the underlying executable’s parameter types refer to a parameterized type that cannot be instantiated for any reason.
public int getModifiers() It returns the Java language modifiers for the executable represented by this object.
public String getName() It returns the name of the method represented by this Method object as a String.
public Annotation[][] getParameterAnnotations() It 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.
public int getParameterCount() It returns the number of formal parameters for the executable represented by this object.
public Class<?>[] getParameterTypes() It returns an array of Class objects that represent the formal parameter types. in declaration order, of the executable represented by this object. It returns an array of length 0 if the underlying executable takes no parameters.
public Class<?> getReturnType() It returns a Class object that represents the formal return type of the method represented by this Method object.
public TypeVariable<Method>[] getTypeParameters() It returns an array of TypeVariable objects that represent the type variables declared by the generic declaration represented by this GenericDeclaration object, in declaration order. It throws GenericSignatureFormatError, if the generic signature of this generic declaration does not conform to the format specified in The Java Virtual Machine Specification
public int hashCode() It returns a hashcode for this Method. The hashcode is computed as the exclusive-or of the hashcodes for the underlying method’s declaring class name and the method’s name.
public Object invoke(Object obj, Object… args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException It invokes the underlying method represented by this Method object, on the specified object with the specified parameters. If the underlying method is static, the specified obj argument is ignored. It may be null. If the number of formal parameters required by the underlying method is 0, the supplied args array may be of length 0 or null. If the underlying method is an instance method, it is invoked using dynamic method lookup as documented in The Java Language Specification. If the underlying method is static, the class that declared the method is initialized if it has not already been initialized. If the method completes normally, the value it returns is returned to the caller of invoke.
public boolean isBridge() It returns true if this method is a bridge method. otherwise returns false.
public boolean isDefault() It returns true if this method is a default method otherwise returns false. A default method is a public non-abstract instance method, that is, a non-static method with a body, declared in an interface type.
public boolean isSynthetic() It returns true if this executable is a synthetic construct; returns false otherwise.
public boolean isVarArgs() It returns true if this executable was declared to take a variable number of arguments; returns false otherwise.
public String toGenericString() It returns a string describing this Method, including type parameters.
public String toString() It returns a string.

Parameter class

Parameter class provides information about method parameters, including its name and modifiers. It also provides an alternate means of obtaining attributes for the parameter.

Parameter Methods

Methods Description
public boolean equals(Object obj) It compares based on the executable and the index.
public AnnotatedType getAnnotatedType() It returns an AnnotatedType object that represents the use of a type to specify the type of the formal parameter represented by this Parameter.
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) It returns this element’s annotation for the specified type if such an annotation is present, else null. It throws NullPointerException, if the given annotation class is null.
public Annotation[] getAnnotations() It returns annotations that are present on this element. If there are no annotations present on this element, the return value is an array of length 0.
public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) It returns annotations that are associated with this element. If there are no annotations associated with this element, the return value is an array of length 0. The difference between this method and AnnotatedElement.getAnnotation(Class) is that this method detects if its argument is a repeatable annotation type (JLS 9.6), and if so, attempts to find one or more annotations of that type by “looking through” a container annotation. It throws NullPointerException, if the given annotation class is null.
public <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) It returns this element’s annotation for the specified type if such an annotation is directly present, else null. This method ignores inherited annotations. It throws NullPointerException, if the given annotation class is null.
public Annotation[] getDeclaredAnnotations() It returns annotations that are directly present on this element. This method ignores inherited annotations. If there are no annotations directly present on this element, the return value is an array of length 0.
public <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) It returns this element’s annotations for the specified type if such annotations are either directly present or indirectly present. This method ignores inherited annotations. If there are no specified annotations directly or indirectly present on this element, the return value is an array of length 0. The difference between this method and AnnotatedElement.getDeclaredAnnotation(Class) is that this method detects if its argument is a repeatable annotation type (JLS 9.6), and if so, attempts to find one or more annotations of that type by “looking through” a container annotation if one is present. The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers. It throws NullPointerException, if the given annotation class is null
public Executable getDeclaringExecutable() It returns the Executable which declares this parameter.
public int getModifiers() It returns the modifier flags for the parameter represented by this Parameter object.
public String getName() It returns the name of the parameter. If the parameter’s name is present, this method returns the name provided by the class file. Otherwise, this method synthesizes a name of the form argN, where N is the index of the parameter in the descriptor of the method which declares the parameter.
public Type getParameterizedType() It returns a Type object that identifies the parameterized type for the parameter represented by this Parameter object.
public Class<?> getType() It returns a Class object that identifies the declared type for the parameter represented by this Parameter object.
public int hashCode()mul int arg0 int arg1 add int arg0 int arg1 It returns a hash code based on the executable’s hash code and the index.
public boolean isImplicit() It returns true if this parameter is implicitly declared in source code. Otherwise, returns false.
public boolean isNamePresent() It returns true if the parameter has a name according to the class file, otherwise, returns false.
public boolean isSynthetic() It returns true if this parameter is neither implicitly nor explicitly declared in source code. Otherwise returns false.
public boolean isVarArgs() It returns true if this parameter represents a variable argument list; returns false otherwise.
public String toString() It returns a string describing this parameter. The format is the modifiers for the parameter, if any, in canonical order as recommended by The Java? Language Specification.

Java Method Parameter Reflection Example

File: Calculate.java

1. public class Calculate { 
2. int add(int a, int b){ 
3. return (a+b); 
4. } 
5. int mul(int a, int b){ 
6. return (b*a); 
7. } 
8. }

Note – before compiling and executing below code, first compile Calculate class by following command:
1. javac -parameters Calculate.java
-parameter flag in the above command is used to store parameters in the Calculate class file. By default .class does not store parameters and returns argsN as parameter name, where N is a number of parameters in the method.
File: ParameterReflection.java

1. import java.lang.reflect.Method; 
2. import java.lang.reflect.Parameter; 
3. public class ParameterReflection { 
4. public static void main(String[] args) { 
5. // Creating object of a class 
6. Calculate calculate = new Calculate(); 
7. // instantiating Class class 
8. Classcls = calculate.getClass(); 
9. // Getting declared methods inside the Calculate class 
10. Method[] method = cls.getDeclaredMethods(); // It returns array of methods 
11. // Iterating method array 
12. for (Method method2 : method) { 
13. System.out.print(method2.getName()); // getting name of method 
14. // Getting parameters of each method 
15. Parameter parameter[] = method2.getParameters(); // It returns array of parameters 
16. // Iterating parameter array 
17. for (Parameter parameter2 : parameter) { 
18. System.out.print(""+parameter2.getParameterizedType()); // returns type of parameter 
19. System.out.print(""+parameter2.getName()); // returns parameter name 
20. } 
21. System.out.println(); 
22. } 
23. } 
24. }

Output:
mul int a int b
add int a int b

Above code will produce the below output if you don’t use -parameters flag to compile the Calculate.java file.
Output:
mul int arg0 int arg1
add int arg0 int arg1

So, this brings us to the end of blog. This Tecklearn ‘Method Parameter Reflection in Java’ blog helps you with commonly asked questions if you are looking out for a job in Java Programming. If you wish to learn Java and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Java and JEE Training

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

• Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
• Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
• Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

• Java Fundamentals
• Introduction to Java Basics
• Features of Java
• Various components of Java language
• Benefits of Java over other programming languages
• Key Benefits of Java

Installation and IDE’s for Java Programming Language

• Installation of Java
• Setting up of Eclipse IDE
• Components of Java Program
• Editors and IDEs used for Java Programming
• Writing a Simple Java Program

Data Handling and Functions

• Data types, Operations, Compilation process, Class files, Loops, Conditions
• Using Loop Constructs
• Arrays- Single Dimensional and Multi-Dimensional
• Functions
• Functions with Arguments

OOPS in Java: Concept of Object Orientation

• Object Oriented Programming in Java
• Implement classes and objects in Java
• Create Class Constructors
• Overload Constructors
• Inheritance
• Inherit Classes and create sub-classes
• Implement abstract classes and methods
• Use static keyword
• Implement Interfaces and use it

Polymorphism, Packages and String Handling

• Concept of Static and Run time Polymorphism
• Function Overloading
• String Handling –String Class
• Java Packages

Exception Handling and Multi-Threading

• Exception handling
• Various Types of Exception Handling
• Introduction to multi-threading in Java
• Extending the thread class
• Synchronizing the thread

File Handling in Java

• Input Output Streams
• Java.io Package
• File Handling in Java

Java Collections

• Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
• Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
• Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable

Java Database Connectivity (JDBC)

• Introduction to SQL: Connect, Insert, Update, Delete, Select
• Introduction to JDBC and Architecture of JDBC
• Insert/Update/Delete/Select Operations using JDBC
• Batch Processing Transaction
• Management: Commit and Rollback

Java Enterprise Edition – Servlets

• Introduction to J2EE
• Client Server architecture
• URL, Port Number, Request, Response
• Need for servlets
• Servlet fundamentals
• Setting up a web project in Eclipse
• Configuring and running the web app with servlets
• GET and POST request in web application with demo
• Servlet lifecycle
• Servlets Continued
• Session tracking and filter
• Forward and include Servlet request dispatchers

Java Server Pages (JSP)

• Fundamentals of Java Server Page
• Writing a code using JSP
• The architecture of JSP
• JSP Continued
• JSP elements: Scriptlets, expressions, declaration
• JSP standard actions
• JSP directives
• Introduction to JavaBeans
• ServletConfig and ServletContext
• Servlet Chaining
• Cookies Management
• Session Management

Hibernate

• Introduction to Hibernate
• Introduction to ORM
• ORM features
• Hibernate as an ORM framework
• Hibernate features
• Setting up a project with Hibernate framework
• Basic APIs needed to do CRUD operations with Hibernate
• Hibernate Architecture

POJO (Plain Old Java Object)

• POJO (Plain Old Java Object)
• Persistent Objects
• Lifecycle of Persistent Object

Spring

• Introduction to Spring
• Spring Fundamentals
• Advanced Spring

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Method Parameter Reflection in Java"

Leave a Message

Your email address will not be published. Required fields are marked *