Using standard jva.bean.Expression API to coerce property value in BeanTransformer.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141262 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark R. Diggory 2004-06-01 23:21:32 +00:00
parent 3dbc735ae1
commit 67d0cea188
2 changed files with 21 additions and 9 deletions

View File

@ -15,26 +15,28 @@
*/ */
package org.apache.commons.math.util; package org.apache.commons.math.util;
import java.beans.Expression;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import org.apache.commons.math.MathException; import org.apache.commons.math.MathException;
import org.apache.commons.beanutils.PropertyUtils;
/** /**
* Uses PropertyUtils to map a Bean getter to a double value. * Uses PropertyUtils to map a Bean getter to a double value.
* @version $Revision: 1.4 $ $Date: 2004/02/21 21:35:18 $ * @version $Revision: 1.5 $ $Date: 2004/06/01 23:21:32 $
*/ */
public class BeanTransformer implements NumberTransformer { public class BeanTransformer implements NumberTransformer {
/** /**
* The propertyName for this Transformer * The propertyName for this Transformer
*/ */
private String propertyName; private String propertyName = null;
private String propertyGetter = null;
/** /**
* Create a BeanTransformer * Create a BeanTransformer
*/ */
public BeanTransformer() { public BeanTransformer() {
this(null); super();
} }
/** /**
@ -59,21 +61,32 @@ public class BeanTransformer implements NumberTransformer {
* @param string The string to set the property to. * @param string The string to set the property to.
*/ */
public void setPropertyName(final String string) { 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) * @see org.apache.commons.math.util.NumberTransformer#transform(java.lang.Object)
*/ */
public double transform(final Object o) throws MathException { public double transform(final Object o) throws MathException {
Expression expr = new Expression(o, propertyGetter, new Object[0]);
Object result;
try { try {
return ((Number) PropertyUtils.getProperty(o, getPropertyName())).doubleValue(); expr.execute();
result = expr.getValue();
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new MathException("IllegalAccessException in Transformation: " + e.getMessage(), e); throw new MathException("IllegalAccessException in Transformation: " + e.getMessage(), e);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
throw new MathException("InvocationTargetException in Transformation: " + e.getMessage(), e); throw new MathException("InvocationTargetException in Transformation: " + e.getMessage(), e);
} catch (NoSuchMethodException 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();
} }
} }

View File

@ -22,7 +22,7 @@ import org.apache.commons.math.TestUtils;
import junit.framework.TestCase; import junit.framework.TestCase;
/** /**
* @version $Revision: 1.11 $ $Date: 2004/05/23 21:34:19 $ * @version $Revision: 1.12 $ $Date: 2004/06/01 23:21:32 $
*/ */
public class BeanTransformerTest extends TestCase { public class BeanTransformerTest extends TestCase {
@ -79,7 +79,6 @@ public class BeanTransformerTest extends TestCase {
} }
/** /**
*
*/ */
public void testTransformInvalidType() throws Exception { public void testTransformInvalidType() throws Exception {
BeanTransformer b = new BeanTransformer("y"); BeanTransformer b = new BeanTransformer("y");