I have been looking at Reflections lately in java and I have come up with a simple example
Reflection.java
import java.lang.reflect.*;
public class Reflection{
}
public static void main(String args[])
{
try {
Class cls = Class.forName("Method");
Method methlist[]
= cls.getDeclaredMethods();
for (int i = 0; i < methlist.length;
i++) {
Method m = methlist[i];
System.out.println("name
= " + m.getName());
System.out.println("decl class = " +
m.getDeclaringClass());
Class pvec[] = m.getParameterTypes();
for (int j = 0; j < pvec.length; j++)
System.out.println("
param #" + j + " " + pvec[j]);
Class evec[] = m.getExceptionTypes();
for (int j = 0; j < evec.length; j++)
System.out.println("exc #" + j
+ " " + evec[j]);
System.out.println("return type = " +
m.getReturnType());
System.out.println("-----");
}
}
catch (Throwable e) {
System.err.println(e);
}
}
}
Method.java
public class Method {
private int f1(
Object p, int x) throws NullPointerException
{
if (p == null)
throw new NullPointerException();
return x;
}
From the above example i am able to return all the information from Method.java which looks like this
name = f1
decl class = class Method
param #0 class java.lang.Object
param #1 int
exc #0 class java.lang.NullPointerException
return type = int
-----
name = main
decl class = class method1
param #0 class [Ljava.lang.String;
return type = void
-----
But my question is, is there a way to test Method.java from Reflection.java with something like
int x = cls.getMethod("f1", Object = anObject, Integer.TYPE = 2 )
System.out.println(x);
And the console should print out 2 because f1 was passed an object?
You need to create an instance of
Methodand invoke the private methodf1with the two parameters. It will return an object. There are a couple of pitfalls in your code, but here’s a working solution (Note: I renamed your classMethodtoMyMethodto avoid a name clash:int.classis allowed. JavaDoc tells us, that if the value has a primitive type, it is first appropriately wrapped in an object. So we’ll receive an instance ofInteger.