commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Martin Cooper" <martin.coo...@tumbleweed.com>
Subject RE: [BeanUtils] Calling a static method via MethodUtils
Date Sun, 21 Jul 2002 20:59:10 GMT


> -----Original Message-----
> From: Bob Waldrop [mailto:rlwaldrop@yahoo.com]
> Sent: Sunday, July 21, 2002 1:45 PM
> To: Jakarta Commons Developers List
> Subject: Re: [BeanUtils] Calling a static method via MethodUtils
> 
> 
> 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.

The problem I have is that the class with the static method that I need to
call does not have a public constructor, so I *have* to pass null. The
current methods in MethodUtils do not allow for that, because they have no
other way to obtain the class than from the object passed in. I'm not using
BeanUtils, I'm using MethodUtils directly.

If I could only get logging to work, I'm sure this would be simple to figure
out. I'll try some other tests, anyway.

--
Martin Cooper


> 
> --bob w.
> 
> --- Martin Cooper <martin.cooper@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:
> > <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>
> > 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Autos - Get free new car price quotes
> http://autos.yahoo.com
> 
> --
> To unsubscribe, e-mail:   
<mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail:
<mailto:commons-dev-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