Get Method

    Using Java Reflection you can inspect the methods of classes and invoke them at runtime.

    This is done via the Java class java.lang.reflect.Method.

    This text will get into more detail about the Java Method object.

    You can access the methods of a class like this:-

    Method[] method = aClass.getMethods();

    Here is a list of the topics covered:-

  1. Obtaining Method Objects
  2. Method Parameters and Return Types
  3. Instantiating Objects using Constructor Object

Obtaining Method Objects:
The Method class is obtained from the Class object.
Example:
Class aClass = ...//obtain class object
Method[] methods = aClass.getMethods();
The Method[] array will have one Method instance for each public method declared in the class.

If you know the precise parameter types of the method you want to access, you can do so rather than obtain the array all methods.

This example returns the public method named "doSomething", in the given class which takes a String as parameter:
Class aClass = ...//obtain class object
Method method = aClass.getMethod("doSomething", new Class[]{String.class});

If no method matches the given method name and arguments, in this caseString.class, a NoSuchMethodException is thrown.

If the method you are trying to access takes no parameters, pass null as the parameter type array, like this:
Class aClass = ...//obtain class object
Method method = aClass.getMethod("doSomething", null);
Method Parameters and Return Types:
You can read what parameters a given method takes like this:
Method method = ... // obtain method - see above
Class[] parameterTypes = method.getParameterTypes();
You can access the return type of a method like this:
Method method = ... // obtain method - see above
Class returnType = method.getReturnType();
Invoking Methods using Method Object:
You can invoke a method like this:
//get method that takes a String as argument
Method method = MyObject.class.getMethod("doSomething", String.class);
Object returnValue = method.invoke(null, "parameter-value1");
The null parameter is the object you want to invoke the method on. If the method is static you supply null instead of an object instance.

In this example, ifdoSomething(String.class) is not static, you need to supply a valid MyObject instance instead of null;

The Method.invoke(Object target, Object ... parameters) method takes an optional amount of parameters, but you must supply exactly one parameter per argument in the method you are invoking. In this case it was a method taking a String, so oneString must be supplied.
Private Field and Methods:
    Despite the common belief it is actually possible to access private fields and methods of other classes via Java Reflection. It is not even that difficult.

    This can be very handy during unit testing. This text will show you how.

    Note: This only works when running the code as a standalone Java application, like you do with unit tests and regular applications. If you try to do this inside a Java Applet, you will need to fiddle around with the SecurityManager. But, since that is not something you need to do very often, it is left out of this text so far. Here is a list of the topics covered in this text:

  1. Accessing Private Fields
  2. Accessing Private Methods
Accessing Private Fields
To access a private field you will need to call the Class.getDeclaredField(String name) or Class.getDeclaredFields() method.

The methods Class.getField(String name) and Class.getFields() methods only return public fields, so they won't work.

Simple example of a class with a private field, and below that the code to access that field via Java Reflection:

public class PrivateObject {
private String privateString = null;
public PrivateObject(String privateString) {
this.privateString = privateString;
}
}
PrivateObject privateObject = new PrivateObject("The Private Value");
Field privateStringField = PrivateObject.class.
getDeclaredField("privateString");
privateStringField.setAccessible(true);
String fieldValue = (String) privateStringField.get(privateObject);
System.out.println("fieldValue = " + fieldValue);
This code example will print out the text "fieldValue = The Private Value", which is the value of the private field privateString of the PrivateObject instance created at the beginning of the code sample.

Notice the use of the methodPrivateObject.class. getDeclaredField("privateString"). It is this method call that returns the private field. This method only returns fields declared in that particular class, not fields declared in any superclasses.

Notice the line in bold too. By calling Field.setAcessible(true) you turn off the access checks for this particular Field instance, for reflection only. Now you can access it even if it is private, protected or package scope, even if the caller is not part of those scopes. You still can't access the field using normal code. The compiler won't allow it.

Accessing Private Methods:
To access a private method you will need to call the Class.getDeclaredMethod(String name, Class[] parameterTypes) or Class.getDeclaredMethods() method.

The methods Class.getMethod(String name, Class[] parameterTypes) andClass.getMethods() methods only return public methods, so they won't work.

Here is a simple example of a class with a private method, and below that the code to access that method via Java Reflection:-
public class PrivateObject {
private String privateString = null;
public PrivateObject(String privateString) {
this.privateString = privateString;
}
private String getPrivateString(){
return this.privateString;
}
}
PrivateObject privateObject = new PrivateObject("The Private Value");
Method privateStringMethod = PrivateObject.class.
getDeclaredMethod("getPrivateString", null);
privateStringMethod.setAccessible(true);
String returnValue = (String)
privateStringMethod.invoke(privateObject, null);
System.out.println("returnValue = " + returnValue);

This code example will print out the text "returnValue = The Private Value", which is the value returned by the method getPrivateString() when invoked on the PrivateObjectinstance created at the beginning of the code sample.

Notice the use of the methodPrivateObject.class. getDeclaredMethod("privateString"). It is this method call that returns the private method. This method only returns methods declared in that particular class, not methods declared in any superclasses.

Notice the line in bold too. By calling Method.setAcessible(true) you turn off the access checks for this particular Method instance, for reflection only. Now you can access it even if it is private, protected or package scope, even if the caller is not part of those scopes. You still can't access the method using normal code. The compiler won't allow it.