commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject cvs commit: jakarta-commons/beanutils/src/test/org/apache/commons/beanutils PrimitiveBean.java MethodUtilsTestCase.java
Date Mon, 29 Jul 2002 18:23:45 GMT
rdonkin     2002/07/29 11:23:45

  Modified:    beanutils/src/java/org/apache/commons/beanutils
                        MethodUtils.java
               beanutils/src/test/org/apache/commons/beanutils
                        MethodUtilsTestCase.java
  Added:       beanutils/src/test/org/apache/commons/beanutils
                        PrimitiveBean.java
  Log:
  Changed invokeMethod to support matching primitive parameters via wrapping classes. This
is to support the correct performance of digester's CallMethodRule.
  
  Revision  Changes    Path
  1.10      +63 -1     jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/MethodUtils.java
  
  Index: MethodUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/MethodUtils.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- MethodUtils.java	5 Apr 2002 14:58:15 -0000	1.9
  +++ MethodUtils.java	29 Jul 2002 18:23:45 -0000	1.10
  @@ -104,6 +104,10 @@
        * It loops through all methods with names that match
        * and then executes the first it finds with compatable parameters.</p>
        *
  +     * <p>This method supports calls to methods taking primitive parameters 
  +     * via passing in wrapping classes. So, for example, a <code>Boolean</code>
class
  +     * would match a <code>boolean</code> primitive.</p>
  +     *
        * <p> This is a convenient wrapper for
        * {@link #invokeMethod(Object object,String methodName,Object [] args)}.
        * </p>
  @@ -141,6 +145,10 @@
        * It loops through all methods with names that match
        * and then executes the first it finds with compatable parameters.</p>
        *
  +     * <p>This method supports calls to methods taking primitive parameters 
  +     * via passing in wrapping classes. So, for example, a <code>Boolean</code>
class
  +     * would match a <code>boolean</code> primitive.</p>
  +     *
        * <p> This is a convenient wrapper for
        * {@link #invokeMethod(Object object,String methodName,Object [] args,Class[] parameterTypes)}.
        * </p>
  @@ -183,6 +191,10 @@
        * It loops through all methods with names that match
        * and then executes the first it finds with compatable parameters.</p>
        *
  +     * <p>This method supports calls to methods taking primitive parameters 
  +     * via passing in wrapping classes. So, for example, a <code>Boolean</code>
class
  +     * would match a <code>boolean</code> primitive.</p>
  +     *
        *
        * @param object invoke method on this object
        * @param methodName get method with this name
  @@ -488,6 +500,10 @@
        * {@link 
        * #invokeMethod(Object object,String methodName,Object [] args,Class[] parameterTypes)}.
        *
  +     * <p>This method can match primitive parameter by passing in wrapper classes.
  +     * For example, a <code>Boolean</code> will match a primitive <code>boolean</code>
  +     * parameter.
  +     *
        * @param clazz find method in this class
        * @param methodName find method with this name
        * @param parameterTypes find method with compatible parameters 
  @@ -522,7 +538,7 @@
                               log.trace("Param=" + parameterTypes[n].getName());
                               log.trace("Method=" + methodsParams[n].getName());
                           }
  -                        if (!methodsParams[n].isAssignableFrom(parameterTypes[n])) {
  +                        if (!isAssignmentCompatible(methodsParams[n], parameterTypes[n]))
{
                               if (log.isTraceEnabled()) {
                                   log.trace(methodsParams[n] + " is not assignable from "

                                               + parameterTypes[n]);
  @@ -554,4 +570,50 @@
           return null;                                        
       }
   
  +    /**
  +     * <p>Determine whether a type can be used as a parameter in a method invocation.
  +     * This method handles primitive conversions correctly.</p>
  +     *
  +     * <p>In order words, it will match a <code>Boolean</code> to a <code>boolean</code>,
  +     * a <code>Long</code> to a <code>long</code>,
  +     * a <code>Float</code> to a <code>float</code>,
  +     * a <code>Integer</code> to a <code>int</code>,
  +     * and a <code>Double</code> to a <code>double</code>.
  +     * Now logic widening matches are allowed.
  +     * For example, a <code>Long</code> will not match a <code>int</code>.
  +     *
  +     * @param parameterType the type of parameter accepted by the method
  +     * @param parameterization the type of parameter being tested 
  +     *
  +     * @return true if the assignement is compatible.
  +     */
  +    private static final boolean isAssignmentCompatible(Class parameterType, Class parameterization)
{
  +        // try plain assignment
  +        if (parameterType.isAssignableFrom(parameterization)) {
  +            return true;
  +        }
  +        
  +        if (parameterType.isPrimitive()) {
  +            // does anyone know a better strategy than comparing names?
  +            // also, this method does *not* do widening - you must specify exactly
  +            // is this the right behaviour?
  +            if (parameterType.getName().equals("boolean")) {
  +                return Boolean.class.equals(parameterization);
  +            }         
  +            if (parameterType.getName().equals("float")) {
  +                return Float.class.equals(parameterization);
  +            }     
  +            if (parameterType.getName().equals("long")) {
  +                return Long.class.equals(parameterization);
  +            }     
  +            if (parameterType.getName().equals("int")) {
  +                return Integer.class.equals(parameterization);
  +            }                
  +            if (parameterType.getName().equals("double")) {
  +                return Double.class.equals(parameterization);
  +            }               
  +        }
  +        
  +        return false;
  +    }
   }
  
  
  
  1.8       +40 -0     jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/MethodUtilsTestCase.java
  
  Index: MethodUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/MethodUtilsTestCase.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- MethodUtilsTestCase.java	22 Jul 2002 00:02:01 -0000	1.7
  +++ MethodUtilsTestCase.java	29 Jul 2002 18:23:45 -0000	1.8
  @@ -299,6 +299,46 @@
           }
       }
   
  +    
  +    /**
  +     * <p> Test <code>invokeMethod</code> with a primitive.
  +     */
  +    public void testInvokeMethodWithPrimitives() throws Exception {
  +        // first test that the bean works 
  +        PrimitiveBean bean = new PrimitiveBean();
  +        bean.setFloat(20.0f);
  +        bean.setLong(10l);
  +        bean.setBoolean(true);
  +        bean.setInt(12);
  +        bean.setDouble(25.5d);
  +        
  +        assertEquals("Bug in PrimitiveBean (1)", 20.0f, bean.getFloat(), 0.01f);
  +        assertEquals("Bug in PrimitiveBean (2)", 10, bean.getLong());
  +        assertEquals("Bug in PrimitiveBean (3)", true, bean.getBoolean());
  +        assertEquals("Bug in PrimitiveBean (4)", 12, bean.getInt());
  +        assertEquals("Bug in PrimitiveBean (5)", 25.5d, bean.getDouble(), 0.01f);
  +        
  +        bean = new PrimitiveBean();
  +        MethodUtils.invokeMethod(bean, "setBoolean", new Boolean(true));
  +        assertEquals("Call boolean property using invokeMethod", true, bean.getBoolean());
  +
  +        bean = new PrimitiveBean();
  +        MethodUtils.invokeMethod(bean, "setFloat", new Float(20.0f));
  +        assertEquals("Call float property using invokeMethod", 20.0f, bean.getFloat(),
0.01f);
  +        
  +        bean = new PrimitiveBean();
  +        MethodUtils.invokeMethod(bean, "setLong", new Long(10));
  +        assertEquals("Call float property using invokeMethod", 10, bean.getLong());
  +        
  +        bean = new PrimitiveBean();
  +        MethodUtils.invokeMethod(bean, "setInt", new Integer(12));
  +        assertEquals("Set float property using invokeMethod", 12, bean.getInt());
  +        
  +        bean = new PrimitiveBean();
  +        MethodUtils.invokeMethod(bean, "setDouble", new Double(25.5d));
  +        assertEquals("Set float property using invokeMethod", 25.5d, bean.getDouble(),
0.01d);
  +    }
  +
   
       /**
        * Simple tests for accessing static methods via invokeMethod().
  
  
  
  1.1                  jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/PrimitiveBean.java
  
  Index: PrimitiveBean.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.beanutils;
  
  /**
   * Bean that has primitive properties
   */
  public class PrimitiveBean {
  
      private float _float;
      private double _double;
      private boolean _boolean;
      private long _long;
      private int _int;
      
      public float getFloat() {
          return _float;
      }
      
      public void setFloat(float _float) {
          this._float = _float;
      }
      
      public double getDouble() {
          return _double;
      }
      
      public void setDouble(double _double) {
          this._double = _double;
      }
      
      public boolean getBoolean() {
          return _boolean;
      }
      
      public void setBoolean(boolean _boolean) {
          this._boolean = _boolean;
      }
      
      public long getLong() {
          return _long;
      }
      
      public void setLong(long _long) {
          this._long = _long;
      }
      
      public int getInt() {
          return _int;
      }
      
      public void setInt(int _int) {
          this._int = _int;
      }
  }
  
  
  

--
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