How to execute method using reflection in Java

  • 28 October 2016
  • ADM

 

How to execute method using reflection in Java - images/logos/java.jpg

 

Java Reflection provides classes and interfaces for obtaining reflective information about classes and objects. Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use of reflected fields, methods, and constructors to operate on their underlying counterparts, within security restrictions. Also provides the possibility to instantiate new objects, invoke methods and get/set field values.

Here is an example how to execute a method using reflection in Java.

Example

Our test class will be a simple model class having 2 constructors and a few methods for testing.

package com.admfactory.reflect;

public class User {

    private int id;
    private String username;

    public User() {
	System.out.println("User object has been created using constructor User()");
    }

    public User(int id, String username) {
	this.id = id;
	this.username = username;
	System.out.println(String.format("User object has been created using constructor User(%d, %s)", id, username));
    }

    public int getId() {
	return id;
    }

    public void setId(int id) {
	System.out.println("setId method called with parameter: " + id);
	this.id = id;
    }

    public String getUsername() {
	return username;
    }

    public void setUsername(String username) {
	this.username = username;
    }

    public void display() {
	System.out.println(String.format("ID: %d, username: %s", id, username));
    }

}

In this tutorial will treat 3 different situations:

  • methods with no value returned;
  • methods with a value returned;
  • methods with parameters;

Note: all combinations are possible between return/ no return and parameters/ no parameters.

Methods with no value returned

/** Calling a method without returning a value */
Method method1 = clazz.getMethod("display");
method1.invoke(user3);

This is the most simple situation. We just need to take the method and invoke it on our object.

Methods with a value returned

/** Calling a method returning a value */
Method method3 = clazz.getMethod("getUsername");
String value = (String) method3.invoke(user3);

Methods with parameters

/** Calling a method with a parameter */
Method method2 = clazz.getMethod("setId", int.class);
method2.invoke(user3, 30);

Here is the full printout for the example.

package com.admfactory.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class MethodTest {
    public static void main(String[] args) throws Exception {

	System.out.println("Method call using reflection example");
	System.out.println();
	System.out.println("Normal method call");
	System.out.println();
	User user1 = new User();
	user1.display();
	System.out.println();
	User user2 = new User(2, "user2");
	user2.display();
	String un = user2.getUsername();
	System.out.println("username: " + un);

	System.out.println();
	System.out.println("Method call using reflection");
	System.out.println();
	
	Class<User> clazz = User.class;
	
	/** Create new user object */
	Constructor<User> constr2 = clazz.getConstructor(int.class, String.class);
	User user3 = constr2.newInstance(3, "user3");
	
	/** Calling a method without returning a value */
	Method method1 = clazz.getMethod("display");
	method1.invoke(user3);
	System.out.println();
	
	/** Calling a method with a parameter */
	Method method2 = clazz.getMethod("setId", int.class);
	method2.invoke(user3, 30);
	System.out.println();
	
	/** Calling a method returning a value */
	Method method3 = clazz.getMethod("getUsername");
	String value = (String) method3.invoke(user3);
	System.out.println("username: " + value);
    }
}

Output

Method call using reflection example

Normal method call

User object has been created using constructor User()
ID: 0, username: null

User object has been created using constructor User(2, user2)
ID: 2, username: user2
username: user2

Method call using reflection

User object has been created using constructor User(3, user3)
ID: 3, username: user3

setId method called with parameter: 30

username: user3

 

References