dynamic method call using reflection only works with certain methods?

Discussion in 'Java' started by Soefara, Aug 20, 2003.

  1. Soefara

    Soefara Guest

    Dear all,

    I'm really struggling to see what's wrong with the following
    method which uses reflection to dynamically call a method
    of an object.

    It compiles fine, but when I pass it a String instance and
    the methodName "toUpperCase" or "toLowerCase" it reports
    no such method found. It does however work with "toString" !

    The same thing happens with any other class objects I pass
    it. About the only methodName that is understood is "toString".
    Anything else will return the "no such method" error :(

    Am I missing something obvious here?

    -------------------------------------------------------------

    // Call a method of an object with no arguments/parameters.
    public static Object callMethod(Object obj, String methodName) {
    Class c = Object.class;
    Object result = null;
    try {
    Method m = c.getMethod(methodName, new Class[]{});
    result = m.invoke(obj, new Object[]{} );
    }
    catch(java.lang.NoSuchMethodException e) {
    System.out.println("Error - No such method: \n" + e.getMessage());
    }
    catch(java.lang.IllegalAccessException e) {
    System.out.println("Error - Illegal access: \n" + e.getMessage());
    }
    catch (java.lang.reflect.InvocationTargetException e) {
    System.out.println("Error - Reflection error : \n" + e.getMessage());
    }
    return result;
    }

    -------------------------------------------------------------


    Thank you very much in advance,

    Soefara
     
    Soefara, Aug 20, 2003
    #1
    1. Advertising

  2. Soefara () wrote:
    : public static Object callMethod(Object obj, String methodName) {
    : Class c = Object.class;
    : Object result = null;
    : try {
    : Method m = c.getMethod(methodName, new Class[]{});

    Hmm, c is the class Object, not the class of obj. That is the
    misstake right?

    Object does have a toString, but it does not have a toUpperCase.

    /robo
     
    Robert Olofsson, Aug 20, 2003
    #2
    1. Advertising

  3. Soefara

    pete kirkham Guest

    Re: dynamic method call using reflection only works with certainmethods?

    You also will need to specify the types and classes of the parameters if
    you are trying to find a method which takes any.

    Pete
     
    pete kirkham, Aug 20, 2003
    #3
    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. jerry051
    Replies:
    2
    Views:
    804
    Oliver Sturm
    Aug 2, 2005
  2. Replies:
    0
    Views:
    677
  3. Marc E
    Replies:
    2
    Views:
    532
    Marc E
    Jul 19, 2006
  4. Replies:
    2
    Views:
    555
    bruce barker
    Mar 25, 2008
  5. Ezra Zygmuntowicz
    Replies:
    2
    Views:
    126
    Ezra Zygmuntowicz
    Jul 10, 2006
Loading...

Share This Page