velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jason van Zyl <jvan...@periapt.com>
Subject Re: org.apache.velocity.Util.invoke(...) enhancement
Date Wed, 30 Aug 2000 08:34:13 GMT
Hi,

Great! Thanks a lot. If you're interested in this sort of
thing you might want to look at the introspection engine
in WebMacro. We like to try and simplify things, but we
would also not have to write things over that work ok.

I said I would post design document by tomorrow night, and
I'm still probably going to make. If you get an idea of
what the parser end does, you may get an idea of how
the WM introspection system could be merged into
Velocity. I've looked at it briefly, but I'm concentrating
on the parser right now.

Bob McWhirter successfully merged the WM introspection
engine with a parser created with Antrl. So we are
going to try and do the same with Velocity. Bob has
some experience there so we should be able to do it
quickly.

Again, your patch is most appreciated. If you want
to look at introspection in general the field is
wide open right now!

Thanks,

jvz.


On Wed, 30 Aug 2000, Christoph Reck wrote:

> I was parsing through the velocity code and saw it had a
> comment on a property #set using Boolean requiring the Object 
> form.
> 
> From a recent project I can provide a more generic approach
> to find the right method allowing primitive java types
> maybe this can be dropped in there:
> 
>     /**
>      * Enhancement of the class objects own getMethods() which takes 
>      * in consideration subclassing and primitives. The params 
>      * parameter is an array of objects that should be matched 
>      * classwise to the method's formal parameter types, in declared 
>      * order. If params is null, it is treated as if it were an empty 
>      * array.
>      *
>      * @param cl         the class to search for a matching method
>      * @param methodName the name of the method
>      * @param params     the array of parameters that will be used to 
>      *                   invoke the method
>      * @return           the Method object that matches the above
>      * @see              Class.getMethods()
>      **/
>     public static Method getMethod(Class  cl, 
>                                    String methodName,
>                                    Object[] params)
>            throws NoSuchMethodException
>     {
>         Method[] methods = cl.getMethods();
>         for (int i = 0; i < methods.length; ++i) 
>         {
>             if ( methods[i].getName().equals(methodName) &&
>                  Modifier.isPublic( methods[i].getModifiers() ) ) 
>             {
>                 int pLength = (params == null) ? 0 : params.length;
>                 int mLength = methods[i].getParameterTypes().length;
>                 if ( (pLength == 0) && (mLength == 0) ) 
>                 {
>                       return methods[i];
>                 } 
>                 else if ( pLength == mLength )
>                 {
>                     try 
>                     {
>                         for (int j = 0; ; ++j) 
>                         {
>                             if (j >= pLength) 
>                             {
>                                 return methods[i];
>                             } 
>                             else 
>                             {
>                                 Class  c = methods[i].getParameterTypes()[j];
>                                 Object p = params[j];
>                                 if ( (c.isPrimitive() && ( c ==
>                                         p.getClass().getField("TYPE").get(p) ))
>                                    || c.isInstance(p) )
>                                 {
>                                     continue; // this param matches, try next
>                                 }
>                             }
>                         }
>                     } 
>                     catch (Exception e) 
>                     {
>                         // ignore (throws own exception below)
>                     }
>                 } // has parameters
>             } // method name match
>         } // for all methods
> 
>         throw new NoSuchMethodException("No such method exception: " +
>                                         methodName + " in " + cl);
>     }
> 
> //Also see a getConstructor that may be useful:
> 
>     /**
>      * Enhancement of the class objects own getConstructor() which takes 
>      * in consideration subclassing and primitives. The params 
>      * parameter is an array of objects that should be matched 
>      * classwise to the constructors formal parameter types, in declared 
>      * order. If params is null, it is treated as if it were an empty 
>      * array.
>      *
>      * @param cl         the class to search for a matching constructor
>      * @param params     the array of parameters that will be used to 
>      *                   invoke the constructor
>      * @return           the Method object of the public constructor that 
>      *                   matches the above
>      * @see              Class.getConstructor()
>      **/
>     public static Constructor getConstructor(Class cl, Object[] params)
>            throws InstantiationException
>     {
>         int pLength = (params == null) ? 0 : params.length;
>         try 
>         {
>             Class[] pClasses = new Class[pLength];
>             for (int i = 0; i < pLength; ++i)
>               pClasses[i] = params[i].getClass();
>   
>             return cl.getConstructor(pClasses); // try exact match
>         } 
>         catch (Exception e) 
>         {
>             // ignore and try constructors with subclasses/primitives below
>         }
> 
>         Constructor[] makers = cl.getConstructors();
>         for (int i = 0; i < makers.length; ++i) 
>         {
>             int mLength = makers[i].getParameterTypes().length;
>             if ( (pLength == 0) && (mLength == 0) ) 
>             {
>                 return makers[i];
>             } 
>             else if (pLength == mLength)
>             {
>                 try 
>                 {
>                     for (int j = 0; ; ++j) 
>                     {
>                         if (j >= pLength) 
>                         {
>                             return makers[i];
>                         } 
>                         else 
>                         {
>                             Class  c = makers[i].getParameterTypes()[j];
>                             Object p = params[j];
>                             if ( (c.isPrimitive() && ( c ==
>                                       p.getClass().getField("TYPE").get(p) ))
>                                || c.isInstance(p) )
>                             {
>                                 continue;
>                             }
>                         }
>                     }
>                 } 
>                 catch (Exception e) 
>                 {
>                     // ignore (throws own exception below)
>                 }
>             } // has parameters
>         } // for all contructors
> 
>         throw new InstantiationException(
>             "No such constructor exception for cl: " + cl);
>     }
> 

-- 

Jason van Zyl
jvanzyl@periapt.com


Mime
View raw message