commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Hubert Rabago <hrab...@gmail.com>
Subject Re: [beanutils] Problem with MethodUtils.invokeExactMethod
Date Wed, 09 Mar 2005 00:46:52 GMT
Response inline (at the risk of confusing people by making them search
for one line responses within your prev message):

On Tue, 08 Mar 2005 19:24:50 -0500, Frank W. Zammetti
<fzlists@omnytex.com> wrote:
> Odd... Here's an interesting observation... If you look at the code I
> just posted that DID work, it is interesting that in order for it to
> work, the request object MUST be fully-qualified in the forName() call.

Yup, or else it won't know which class you mean.  You can also use
HttpServletRequest.class.

>  Also, I tried replacing that single line with:
> 
> pTypes[0] = request.getClass();
> 
> ...which by all rights should work, but no, it doesn't (which is
> basically all beanutils is doing).

getClass() returns the runtime class, which would be the
implementation of the HttpServletRequest interface that your container
is using.

> 
> It seems that beanutils, and more generically the getClass() method,
> isn't returning the proper type... or something (in my best Butthead
> voice)...

Method.invoke doesn't try to find a matching method by looking at
method signatures that are compatible with the supplied parameter
classes.

In the sample I gave earlier, I tried these:

try {
    Method m = SampleClass.class.getMethod("method",new Class[] {Object.class});
    m.invoke(sc, new Object[] {s});
} catch (NoSuchMethodException e) {
    System.out.println("Can't find method.");
}

try {
    Method m = SampleClass.class.getMethod("method",new Class[] {String.class});
    m.invoke(sc, new Object[] {s});
} catch (NoSuchMethodException e) {
    System.out.println("Can't find method.");
}

The first one matched, the second one didn't.

If you still want to use MethodUtils, try the invokeMethod()
variation.  Going back to my SampleClass, 
"MethodUtils.invokeExactMethod(sc, "method", s);" fails but
"MethodUtils.invokeMethod(sc, "method", s);" succeeds.

hth,

Hubert

> 
> Hubert Rabago wrote:
> > Your guess matches mine.
> > It seems invokeExactMethod() uses getClass() to determine the argument
> > types, so for your call, it'll use the actual (runtime) class of your
> > request object, and it won't find setupMethod1 which declares
> > HttpServletRequest.
> >
> > I wrote a quick test:
> > public class SampleClass {
> >
> >     public void method(Object a) {
> >         System.out.println("inside method() where a=" + a);
> >     }
> >
> >     public static void main(String[] args)
> >             throws Exception {
> >         SampleClass sc = new SampleClass();
> >         String s = "hello";
> >         MethodUtils.invokeExactMethod(sc, "method",
> >                 new Object[] {s}, new Class[] {Object.class});
> >         MethodUtils.invokeExactMethod(sc, "method", s);
> >     }
> > }
> >
> > The first call ran fine, the second failed.  This seems to match the
> > problem you're seeing.
> >
> > Hubert

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


Mime
View raw message