You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by md...@apache.org on 2004/06/02 01:21:32 UTC

cvs commit: jakarta-commons/math/src/test/org/apache/commons/math/util BeanTransformerTest.java BeanTransformer.java

mdiggory    2004/06/01 16:21:32

  Modified:    math/src/test/org/apache/commons/math/util
                        BeanTransformerTest.java BeanTransformer.java
  Log:
  Using standard jva.bean.Expression API to coerce property value in BeanTransformer.
  
  Revision  Changes    Path
  1.12      +1 -2      jakarta-commons/math/src/test/org/apache/commons/math/util/BeanTransformerTest.java
  
  Index: BeanTransformerTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/util/BeanTransformerTest.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- BeanTransformerTest.java	23 May 2004 21:34:19 -0000	1.11
  +++ BeanTransformerTest.java	1 Jun 2004 23:21:32 -0000	1.12
  @@ -79,7 +79,6 @@
       }
       
       /**
  -     * 
        */
       public void testTransformInvalidType() throws Exception {
           BeanTransformer b = new BeanTransformer("y");
  
  
  
  1.5       +20 -7     jakarta-commons/math/src/test/org/apache/commons/math/util/BeanTransformer.java
  
  Index: BeanTransformer.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/util/BeanTransformer.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- BeanTransformer.java	21 Feb 2004 21:35:18 -0000	1.4
  +++ BeanTransformer.java	1 Jun 2004 23:21:32 -0000	1.5
  @@ -15,9 +15,9 @@
    */
   package org.apache.commons.math.util;
   
  +import java.beans.Expression;
   import java.lang.reflect.InvocationTargetException;
   import org.apache.commons.math.MathException;
  -import org.apache.commons.beanutils.PropertyUtils;
   
   /**
    * Uses PropertyUtils to map a Bean getter to a double value.
  @@ -28,13 +28,15 @@
       /**
        * The propertyName for this Transformer
        */
  -    private String propertyName;
  +    private String propertyName = null;
  +    
  +    private String propertyGetter = null;
   
       /**
        * Create a BeanTransformer
        */
       public BeanTransformer() {
  -        this(null);
  +        super();
       }
   
       /**
  @@ -59,21 +61,32 @@
        * @param string The string to set the property to.
        */
       public void setPropertyName(final String string) {
  -        propertyName = string;
  +        this.propertyName = string;
  +        this.propertyGetter = "get" + string.substring(0,1).toUpperCase() + string.substring(1);
       }
   
  +    
       /**
        * @see org.apache.commons.math.util.NumberTransformer#transform(java.lang.Object)
        */
       public double transform(final Object o) throws MathException {
  +        Expression expr = new Expression(o, propertyGetter, new Object[0]);
  +        Object result;
           try {
  -			return ((Number) PropertyUtils.getProperty(o, getPropertyName())).doubleValue();
  +            expr.execute();
  +            result = expr.getValue();
           } catch (IllegalAccessException e) {
   			throw new MathException("IllegalAccessException in Transformation: " + e.getMessage(), e);
           } catch (InvocationTargetException e) {
   			throw new MathException("InvocationTargetException in Transformation: " + e.getMessage(), e);
           } catch (NoSuchMethodException e) {
  -			throw new MathException("oSuchMethodException in Transformation: " + e.getMessage(), e);
  +			throw new MathException("NoSuchMethodException in Transformation: " + e.getMessage(), e);
  +        } catch (ClassCastException e) {
  +            throw new MathException("ClassCastException in Transformation: " + e.getMessage(), e);
  +        } catch (Exception e) {
  +            throw new MathException("Exception in Transformation: " + e.getMessage(), e);
           }
  +        
  +        return ((Number) result).doubleValue();
       }
   }
  
  
  

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