1 16 package org.apache.commons.math.util; 17 18 import java.beans.Expression ; 19 import java.lang.reflect.InvocationTargetException ; 20 import org.apache.commons.math.MathException; 21 22 26 public class BeanTransformer implements NumberTransformer { 27 28 31 private String propertyName = null; 32 33 private String propertyGetter = null; 34 35 38 public BeanTransformer() { 39 super(); 40 } 41 42 46 public BeanTransformer(final String property) { 47 super(); 48 setPropertyName(property); 49 } 50 51 55 public String getPropertyName() { 56 return propertyName; 57 } 58 59 63 public void setPropertyName(final String string) { 64 this.propertyName = string; 65 this.propertyGetter = "get" + string.substring(0,1).toUpperCase() + string.substring(1); 66 } 67 68 69 72 public double transform(final Object o) throws MathException { 73 Expression expr = new Expression (o, propertyGetter, new Object [0]); 74 Object result; 75 try { 76 expr.execute(); 77 result = expr.getValue(); 78 } catch (IllegalAccessException e) { 79 throw new MathException("IllegalAccessException in Transformation: " + e.getMessage(), e); 80 } catch (InvocationTargetException e) { 81 throw new MathException("InvocationTargetException in Transformation: " + e.getMessage(), e); 82 } catch (NoSuchMethodException e) { 83 throw new MathException("NoSuchMethodException in Transformation: " + e.getMessage(), e); 84 } catch (ClassCastException e) { 85 throw new MathException("ClassCastException in Transformation: " + e.getMessage(), e); 86 } catch (Exception e) { 87 throw new MathException("Exception in Transformation: " + e.getMessage(), e); 88 } 89 90 return ((Number ) result).doubleValue(); 91 } 92 } | Popular Tags |