Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Class.getMethod() question (or how to refer to an int's class)

Reply
Thread Tools

Class.getMethod() question (or how to refer to an int's class)

 
 
joe.weinstein@gmail.com
Guest
Posts: n/a
 
      03-17-2006
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[i].getName().equals(methodName))
{
System.out.println("got it");
Object[] oo = new Object[1];
oo[0] = new Integer(myInt);
meths[i].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

 
Reply With Quote
 
 
 
 
opalpa@gmail.com opalinski from opalpaweb
Guest
Posts: n/a
 
      03-17-2006
> I guess my problems boils down to how to get an int's
> class intothe Class array.



Integer.TYPE is it.

All the best,
Opalinski
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.geocities.com/opalpaweb/

 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      03-17-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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[i].getName().equals(methodName))
> {
> System.out.println("got it");
> Object[] oo = new Object[1];
> oo[0] = new Integer(myInt);
> meths[i].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
>


Use Integer.TYPE

<SSCCE>
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class test1 {
public static void main(String[] args) throws SecurityException,
NoSuchMethodException, IllegalArgumentException,
IllegalAccessException,
InvocationTargetException, ClassNotFoundException {
Class[] cls = new Class[1];
cls[0] = Integer.TYPE;
Method meth = test1.class.getMethod("foo", cls);
System.out.println("got it");
Object[] oo = new Object[1];
oo[0] = Integer.valueOf(5);
meth.invoke(new test1(), oo);
}

public void foo(int i) {
System.out.println(i);
}
}
</SSCCE>

- Oliver

 
Reply With Quote
 
joe.weinstein@gmail.com
Guest
Posts: n/a
 
      03-17-2006
perfect. Thanks. Thanks Oliver too.
Joe

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple question: How to refer to an image on the header control? tom ASP .Net 1 02-26-2006 07:01 AM
VS2003: Error Adding Existing Item - <FileURL> does not refer to p =?Utf-8?B?U2ltb25EZXY=?= ASP .Net 2 10-11-2005 06:53 AM
What do Coil and Pinnacle refer to on a Cat 65xx? Christopher J Pilkington Cisco 1 08-21-2004 06:14 AM
How to refer to Root on Transfer paul reed ASP .Net 3 10-17-2003 05:54 AM
How to refer to a variable on another webform Tor Inge Rislaa ASP .Net 3 08-05-2003 02:01 PM



Advertisments