I want to call a class function through invoke(...) in class Method. If
this function's arguments are different primitive types, can I call it
with invoke(...)? For example, how can I call foo() in class A in
following example?
public class A
{
public A() {}
public int foo(int a, boolean b)
{
...
}
}
Is it doable?
Thanks a lot!
Zach
Bjorn Abelli - 08 Jun 2005 02:45 GMT
"Zach" wrote...
> I want to call a class function through invoke(...) in class
> Method. If this function's arguments are different primitive
[quoted text clipped - 12 lines]
>
> Is it doable?
Yes, it is.
Just use the corresponding wrapper for primitive types.
int --> Integer
boolean --> Boolean
etc..
// Bjorn A
John Currier - 08 Jun 2005 03:21 GMT
Class[] paramTypes = new Class[] {int.class, boolean.class};
Method foo = A.class.getMethod("foo", paramTypes);
Object[] params = new Object[] {new Integer(55), Boolean.TRUE};
int rc = ((Integer)foo.invoke(new A(), params)).intValue();
System.out.println("foo returned " + rc);
John