You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Martin Cooper <ma...@tumbleweed.com> on 2002/07/21 07:41:35 UTC

[BeanUtils] Calling a static method via MethodUtils

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:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [BeanUtils] Calling a static method via MethodUtils

Posted by Bob Waldrop <rl...@yahoo.com>.
Hi Martin,
    I posted the exact same problem yesterday.  I made
a test for the invokeMethod on a static class and was
able to show that invokeMethod does not require the
object argument to be null to perform static.  Null is
acceptible but not required for static.  So I'm
stumped for some reason as well.  Unless I'm wrong,
something in BeanUtils is the problem.

--bob w.

--- Martin Cooper <ma...@tumbleweed.com>
wrote:
> 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:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 
> 
> 
> --
> To unsubscribe, e-mail:  
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 


__________________________________________________
Do You Yahoo!?
Yahoo! Autos - Get free new car price quotes
http://autos.yahoo.com

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>