How do I call a declared method using reflection?
Author: Deron Eriksson
Description: This Java tutorial describes how to use invoke a method using reflection.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.4


Page:    1 2 >

In another tutorial, we examined how we could use reflection to get a Method object by calling getMethod() on a Class object. In that tutorial, we saw how an error would be thrown if we tried to call getMethod() on a private or protected class, since the getMethod() method only returns public methods (which can be inherited public methods).

However, it is possible to call private and protected methods that have been declared on a class (but not inherited) by calling getDeclaredMethod() on a Class object. After obtaining the declared method, we can then invoke it using reflection by calling the invoke() method on the Method object.

The ClassMethodTest illustrates this. It instantiates a Testing object, gets a Class object for that Testing object, and then calls getDeclaredMethod() twice to get two protected Method objects from the Class object, and then it invokes both methods. Following this, for illustration purposes, it attempts to call getMethod() on a protected method, which, since it is illegal, results in an error being thrown.

ClassMethodTest.java

package test;

import java.lang.reflect.Method;

public class ClassMethodTest {

	public static void main(String args[]) {
		Testing t = new Testing();
		Class tClass = t.getClass();

		try {
			System.out.println("getting declared method 'setProtectedDude'");
			Method proSetMethod = tClass.getDeclaredMethod("setProtectedDude", new Class[] { String.class });
			System.out.println("invoking setProtectedDude with 'blah'");
			proSetMethod.invoke(t, new Object[] { "blah" });

			System.out.println("\ngetting declared method 'getProtectedDude'");
			Method proGetMethod = tClass.getDeclaredMethod("getProtectedDude", new Class[] {});
			System.out.println("invoking getProtectedDude");
			String str1 = (String) proGetMethod.invoke(t, new Object[] {});
			System.out.println("getProtectedDude returned: " + str1);

			System.out.println("\ntrying to get method 'getProtectedDude' (will throw exception)");
			Method proGetMethod2 = tClass.getMethod("getProtectedDude", new Class[] {});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

The Testing class is shown below.

Testing.java

package test;

public class Testing {

	private String string1;
	private boolean boolean1;
	private String privateDude;
	private String protectedDude;

	public Testing() {
	}

	public Testing(String string1, boolean boolean1) {
		this.string1 = string1;
		this.boolean1 = boolean1;
	}

	private String getPrivateDude() {
		return privateDude;
	}

	private void setPrivateDude(String privateDude) {
		this.privateDude = privateDude;
	}

	protected String getProtectedDude() {
		return protectedDude;
	}

	protected void setProtectedDude(String protectedDude) {
		this.protectedDude = protectedDude;
	}

	public boolean isBoolean1() {
		return boolean1;
	}

	public void setBoolean1(boolean boolean1) {
		this.boolean1 = boolean1;
	}

	public String getString1() {
		return string1;
	}

	public void setString1(String string1) {
		this.string1 = string1;
	}

}

(Continued on page 2)

Page:    1 2 >