J
Joe Weinstein
Hi all,
What's the form for asking a class for a method
with a given name, that takes an int as an argument,
and then invoking it? I have one way which works, but
it is clunky, having to get all the methods first:
void executeIntMethodOn( String methodName, Object myObj, int myInt )
{
Method[] meths = myObj.getClass().getMethods();
for (int i = 0; i < meths.length; i++)
{
if (meths.getName().equals(methodName))
{
System.out.println("got it");
Object[] oo = new Object[1];
oo[0] = new Integer(myInt);
meths.invoke( p, oo );
break;
}
}
}
If I try code like:
Integer i = new Integer(myInt);
Class[] cls = new Class[1];
cls[0] = i.getClass();
Method meth = myObj.getClass().getMethod(methodName, cls );
System.out.println("got it");
Object[] oo = new Object[1];
oo[0] = i;
meth.invoke( p, oo );
It fails to find the method, saying there is no method
of that name taking an Integer (it takes an int). I guess
my problems boils down to how to get an int's class into
the Class array.
thanks in advance,
Joe
What's the form for asking a class for a method
with a given name, that takes an int as an argument,
and then invoking it? I have one way which works, but
it is clunky, having to get all the methods first:
void executeIntMethodOn( String methodName, Object myObj, int myInt )
{
Method[] meths = myObj.getClass().getMethods();
for (int i = 0; i < meths.length; i++)
{
if (meths.getName().equals(methodName))
{
System.out.println("got it");
Object[] oo = new Object[1];
oo[0] = new Integer(myInt);
meths.invoke( p, oo );
break;
}
}
}
If I try code like:
Integer i = new Integer(myInt);
Class[] cls = new Class[1];
cls[0] = i.getClass();
Method meth = myObj.getClass().getMethod(methodName, cls );
System.out.println("got it");
Object[] oo = new Object[1];
oo[0] = i;
meth.invoke( p, oo );
It fails to find the method, saying there is no method
of that name taking an Integer (it takes an int). I guess
my problems boils down to how to get an int's class into
the Class array.
thanks in advance,
Joe