[prev in list] [next in list] [prev in thread] [next in thread] 

List:       jakarta-commons-dev
Subject:    [PATCH] org.apache.commons.rupert.christoph.ClassTool
From:       "Paulo Gaspar" <paulo.gaspar () krankikom ! de>
Date:       2001-09-26 12:13:35
[Download RAW message or body]

Attached goes the patch with those improvements to the ClassTool.

Thanks Daniel. Building the patch was completely easy after reading 
that page. Also thanks to Jon for the tip (I would get there with 
that too).

Have fun,
Paulo Gaspar

http://www.krankikom.de
http://www.ruhronline.de
 

> -----Original Message-----
> From: dlr@despot.finemaltcoding.com
> [mailto:dlr@despot.finemaltcoding.com]On Behalf Of Daniel Rall
> Sent: Wednesday, September 26, 2001 8:29 AM
> To: Jakarta Commons Developers
> Cc: paulo.gaspar@krankikom.de
> Subject: Re: BUG in Sandbox's Rupert project - ClassTool.getMethod()
> 
> 
> "Paulo Gaspar" <paulo.gaspar@krankikom.de> writes:
> 
> > I will do it but I need some more time. I have to port back the code
> > and change the style back too... and then finally learn how to make
> > a patch with CVS. =;o)
> 
> Hi Paulo.  This should help:
> 
> http://jakarta.apache.org/site/source.html
> 
["patchClassTool.txt" (text/plain)]

Index: rupert/src/java/org/apache/commons/rupert/christoph/ClassTool.java
===================================================================
RCS file: /home/cvspublic/jakarta-commons-sandbox/rupert/src/java/org/apache/commons/rupert/christoph/ClassTool.java,v
 retrieving revision 1.1
diff -u -r1.1 ClassTool.java
--- rupert/src/java/org/apache/commons/rupert/christoph/ClassTool.java	2001/06/19 \
                11:43:12	1.1
+++ rupert/src/java/org/apache/commons/rupert/christoph/ClassTool.java	2001/09/26 \
11:51:23 @@ -109,8 +109,8 @@
         {
             Constructor constructor = cls.getConstructor(params);
             return constructor.newInstance( new Object[0] );
-        } 
-        catch (Exception ex) 
+        }
+        catch (Exception ex)
         {
             Constructor constructor = cls.getDeclaredConstructor(params);
             if ( Modifier.isPrivate( constructor.getModifiers() ) )
@@ -164,118 +164,186 @@
         return constructor.newInstance(params);
     }
 
+
+    /**
+     *  For a given <code>Class</code> finds the <code>Constructor</code>
+     *  matching the given parameters.
+     *
+     *  This is an 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  clazz       The <code>Class</code> whose
+     * <code>Constructor</code> is searched.
+     * @param  params      The parameters that will be used to invoke the
+     * constructor.
+     * @return             The matching <code>Constructor</code>.
+     * @see                java.lang.Class#getConstructor(Class[])
+     */
+    public static Constructor getConstructor( Class clazz, Object[] params )
+    {
+        return getConstructor(clazz.getConstructors(), params);
+    }
+
+
     /**
-     * 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 cls        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              java.lang.Class#getConstructor(Class[])
-     **/
-    public static Constructor getConstructor(Class cls, Object[] params)
+     *  For a given array of <code>Constructor</code>s (possibly from a
+     *  introspection cache) finds the one matching the given parameters.
+     *
+     * @param  constructors  The <code>Constructor</code>s array to be
+     * searched.
+     * @param  params        The parameters that will be used to invoke the
+     * constructor.
+     * @return               The matching <code>Constructor</code>.
+     */
+    public static Constructor getConstructor( Constructor[] constructors,
+                                              Object[] params
+                                            )
     {
-        Constructor[] constructors = cls.getConstructors();
+        if (null == constructors)
+            return null;
 
-        for (int i = 0; i < constructors.length; ++i )
+        int constrCount = constructors.length;
+
+        for ( int i = 0; i < constrCount; ++i )
         {
-            Class[] parameterTypes = constructors[i].getParameterTypes();
+            Class[] paramTypes = constructors[i].getParameterTypes();
+            if ( isMatchingParamlist( paramTypes, params ) )
+                return constructors[i];
 
-            // The methods we are trying to compare must
-            // the same number of arguments.
-            if (parameterTypes.length == params.length)
-            {
-                // Make sure the given parameter is a valid
-                // subclass of the method parameter in question.
-                for (int j = 0; ; j++)
-                {
-                    if (j >= parameterTypes.length)
-                        return constructors[i]; // found
-
-                    Class c = parameterTypes[j];
-                    Object p = params[j];
-                    if ( c.isPrimitive() )
-                    {
-                        try
-                        {
-                            if ( c != p.getClass().getField("TYPE").get(p) )
-                                break;
-                        } catch (Exception ex) {
-                            break; // p is not a primitive derivate
-                        }
-                    }
-                    else if ( (p != null) &&
-                              !c.isAssignableFrom( p.getClass() ) )
-                        break;
-                } // for all parameters
-            } // if same length
-        } // for all contructors
+        }  // for all contructors
 
         return null;
     }
 
+
     /**
-     * Get a <code>Method</code> from a <code>Class</code> with the
-     * requested name which accepts the specified parameters.
+     *  For a given <code>Class</code> finds the <code>Method</code> matching
+     *  the given name and parameters.
      *
-     * @param clazz      The <code>Class</code> to whose method is searched.
-     * @param methodName The name of the desired method.
-     * @param params     The parameters that will be passed when calling
-     *                   the method.
-     */
-    public static Method getMethod( Class clazz,
-                                    String methodName,
-                                    Object[] params )
-    {
-        Method[] methods = clazz.getMethods();
-        Class[] parameterTypes = null;
-        Method  method = null;
-        int numMethods = methods.length;
+     * @param  clazz       The <code>Class</code> whose <code>Method</code> is
+     * searched.
+     * @param  methodName  The name of the desired method.
+     * @param  params      The parameters that will be passed when calling the
+     * method.
+     * @return             The matching <code>Method</code>.
+     */
+    public final static Method getMethod( final Class clazz,
+                                          final String methodName,
+                                          final Object[] params
+                                        )
+    {
+        return getMethod(clazz.getMethods(), methodName, params);
+    }
 
-        for (int i = 0; i < numMethods; ++i)
-        {
-            method = methods[i];
-            parameterTypes = method.getParameterTypes();
+
+    /**
+     *  For a given array of <code>Method</code>s (possibly from a
+     *  introspection cache) finds the one matching the given name and
+     *  parameters.
+     *
+     * @param  methods     The <code>Method</code>s array to be searched.
+     * @param  methodName  The name of the desired method.
+     * @param  params      The parameters that will be passed when calling the
+     * method.
+     * @return             The matching <code>Method</code>.
+     */
+    public static Method getMethod( final Method[] methods,
+                                    final String methodName,
+                                    final Object[] params
+                                  )
+    {
+        if (null == methods)
+            return null;
 
-            // The methods we are trying to compare must
-            // the same number of arguments.
-            if (parameterTypes.length == params.length)
-            {
-                // Make sure the given parameter is a valid
-                // subclass of the method parameter in question.
-                for (int j = 0; ; j++)
-                {
-                    if (j >= parameterTypes.length)
-                        return method;
-
-                    Class c = parameterTypes[j];
-                    Object p = params[j];
-                    if ( c.isPrimitive() )
-                    {
-                        try
-                        {
-                            if ( c != p.getClass().getField("TYPE").get(p) )
-                                break;
-                        }
-                        catch (Exception ex)
-                        {
-                            break; // p is not a primitive derivate
-                        }
-                    }
-                    else if ( (p != null) &&
-                              !c.isAssignableFrom( p.getClass() ) )
-                        break;
-                }
-            }
+        int methodCount = methods.length;
+
+        for ( int i = 0; i < methodCount; ++i )
+        {   Method m = methods[i];
+            if ( methodName.equals(m.getName())
+                 && isMatchingParamlist(m.getParameterTypes(), params)
+               )
+                return m;
+
+        }
+
+        return null;
+    }
+
+
+    /**
+     *  For a given array of <code>Method</code>s (possibly from a
+     *  introspection cache) finds the one matching the given parameters.
+     *  NO name match here.
+     *
+     * @param  methods     The <code>Method</code>s array to be searched.
+     * @param  params      The parameters that will be passed when calling the
+     * method.
+     * @return             The matching <code>Method</code>.
+     */
+    public static Method getMethod( final Method[] methods,
+                                    final Object[] params
+                                  )
+    {
+        if (null == methods)
+            return null;
+
+        int methodCount = methods.length;
+
+        for ( int i = 0; i < methodCount; ++i )
+        {   if ( isMatchingParamlist(methods[i].getParameterTypes(), params) )
+                return methods[i];
         }
 
         return null;
+    }
+
+
+    /**
+     *  Checks if a list of parameter values matches a given list of parameter
+     *  types.
+     *
+     * @param  paramTypes    The array of parameter types do be matched.
+     * @param  params        The array of parameter values.
+     * @return               <code>true<code> if there is a match and
+     * <code>false<code> otherwise.
+     */
+    public static boolean isMatchingParamlist( Class[] paramTypes,
+                                               Object[] params
+                                             )
+    {
+        // The methods we are trying to compare must
+        // the same number of arguments.
+        int paramTypeCount = (null == paramTypes) ? 0 : paramTypes.length;
+        int paramCount     = (null == params)     ? 0 : params.length;
+        if ( paramCount != paramTypeCount )
+            return false;
+
+          // Make sure the given parameter is a valid
+          // subclass of the method parameter in question.
+          for ( int i = 0; i < paramCount ; i++ )
+          {
+              Class   c  = paramTypes[i];
+              Object  p  = params[i];
+              if ( c.isPrimitive() )
+              {
+                  try
+                  {
+                      if ( c != p.getClass().getField( "TYPE" ).get( p ) )
+                          return false;
+                  }
+                  catch ( Exception ex )
+                  {
+                      return false;        // p is not a primitive derivate
+                  }
+              }
+              else if ( ( p != null ) && !c.isAssignableFrom( p.getClass() ) )
+                  return false;
+          }
+
+        return true;
     }
 
 }



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic