commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Martin Cooper" <martin.coo...@tumbleweed.com>
Subject [BeanUtils] Calling a static method via MethodUtils
Date Sun, 21 Jul 2002 05:41:35 GMT
I posted the following to commons-user, but it turned out to be more of a
-dev message, so I thought I'd repost here.

Since writing this, I've discovered that invoking a static method which has
primitive types as parameters doesn't seem to work, but I haven't yet
determined whether that's because of the static method, the primitive types,
or something else.

--
Martin Cooper


-----Original Message-----
From: Martin Cooper [mailto:martin.cooper@tumbleweed.com]
Sent: Friday, July 19, 2002 8:39 PM
To: 'commons-user@jakarta.apache.org'
Subject: [BeanUtils] Calling a static method via MethodUtils


As far as I can tell, there is no way to use MethodUtils to call a static
method. This is because the 'object' parameter to invokeMethod() (and
invokeExactMethod()) is used both to obtain the class object and to pass to
Method.invoke(). To call a static method, the value passed to
Method.invoke() should be null, but if I pass null to invokeMethod(), the
call to object.getClass() is going to fail with an NPE.

Is this something that might be fixed/added in MethodUtils? Or does invoking
static methods fall outside the scope of the BeanUtils package? I've
appended some code to consider at the end of this message.

Also, since I can't use invokeMethod() to do what I need, I thought perhaps
I could at least leverage getMatchingAccessibleMethod(), but that method is
private. (Just to complicate matters, I need to call a static method which
has primitive types in its parameter list. :) Since this method does some
handy stuff, it might be worth considering making it public.

Thanks for your consideration.

--
Martin Cooper


One way to enable calling static methods would be to factor the lowest-level
invokeMethod() method as follows:

    // This is the existing method, refactored to call the new one.
    public static Object invokeMethod(
            Object object,
            String methodName,
            Object[] args,
            Class[] parameterTypes)
                throws
                    NoSuchMethodException,
                    IllegalAccessException,
                    InvocationTargetException {
        
        return invokeMethod(object.getClass(), object, methodName,
                            args, parameterTypes);
    }

    // This is the new method, which takes a Class parameter.
    public static Object invokeMethod(
            Class clazz,
            Object object,
            String methodName,
            Object[] args,
            Class[] parameterTypes)
                throws
                    NoSuchMethodException,
                    IllegalAccessException,
                    InvocationTargetException {
        

        Method method = getMatchingAccessibleMethod(
                clazz,
                methodName,
                parameterTypes);
        if (method == null)
            throw new NoSuchMethodException("No such accessible method: " +
                    methodName + "() on object: " +
object.getClass().getName());
        return method.invoke(object, args);
    }

This way, the new method uses the 'object' parameter only to pass to
method.invoke(), so that I can pass a value for 'clazz' and null for
'object' in order to invoke a static method.


--
To unsubscribe, e-mail:
<mailto:commons-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail:
<mailto:commons-user-help@jakarta.apache.org>



--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message