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

Discussion in 'Java' started by joe.weinstein@gmail.com, Mar 17, 2006.

  1. Guest

    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
    , Mar 17, 2006
    #1
    1. Advertising

  2. opalinski from opalpaweb, Mar 17, 2006
    #2
    1. Advertising

  3. Oliver Wong Guest

    <> wrote in message
    news:...
    > 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
    >


    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
    Oliver Wong, Mar 17, 2006
    #3
  4. Guest

    perfect. Thanks. Thanks Oliver too.
    Joe
    , Mar 17, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ewolfman
    Replies:
    4
    Views:
    653
    ewolfman
    Nov 15, 2005
  2. Joe Weinstein
    Replies:
    2
    Views:
    514
    Chris Uppal
    Mar 18, 2006
  3. Schnoffos
    Replies:
    2
    Views:
    1,195
    Martien Verbruggen
    Jun 27, 2003
  4. Hal Styli
    Replies:
    14
    Views:
    1,612
    Old Wolf
    Jan 20, 2004
  5. chucky
    Replies:
    14
    Views:
    1,270
    Twisted
    Aug 2, 2007
Loading...

Share This Page