Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

A Class.getMethod() question... (an int's class)

Thread view: 
Joe Weinstein - 17 Mar 2006 17:53 GMT
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
Joe Weinstein - 17 Mar 2006 18:07 GMT
> Hi all,
>    What's the form for asking a class for a method
[quoted text clipped - 36 lines]
> thanks in advance,
> Joe

And, I guess the question is academic, not performance-related.
I was hoping the Class object would keep it's methods in a
sorted order to speed up method-finding, but in looking at
the code, it seems it simply keeps an array which it copies
to return via getMethods(), and getMethod() doesn't do any-
thing faster...
Joe
Chris Uppal - 18 Mar 2006 11:27 GMT
> I guess
> my problems boils down to how to get an int's class into
> the Class array.

Either of the expressions:
   Integer.TYPE
or
   int.class
evaluate to the java.lang.Class object corresponding to the primitive type.

For peculiar reasons, 1.5 considers the generic "type" of the expression to be
   Class<Integer>
which you may have to take account of when using the int.class object.

   -- chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.