| 1 33 package net.sf.jga.fn.property; 34 35 import java.lang.reflect.InvocationTargetException ; 36 import java.lang.reflect.Method ; 37 import java.text.MessageFormat ; 38 import net.sf.jga.fn.EvaluationException; 39 import net.sf.jga.fn.UnaryFunctor; 40 41 49 50 public class GetProperty<T,R> extends UnaryFunctor<T,R> { 51 52 static final long serialVersionUID = -6249123644692001840L; 53 54 private String _propName; 56 57 private String _methName; 59 60 private Class <T> _argtype; 62 63 private transient Method _meth; 65 66 72 public GetProperty(Class <T> argclass, String propName) { 73 if (propName == null || propName.length() == 0) { 74 throw new IllegalArgumentException ("Must supply property name"); 75 } 76 77 _argtype = argclass; 78 79 if (propName.startsWith("get")) { 80 _methName = propName; 81 _propName = propName.substring(3); 82 } 83 else if (argclass.equals(Boolean .class) && propName.startsWith("is")) { 84 _methName = propName; 85 _propName = propName.substring(2); 86 } 87 else { 88 _propName = propName; 89 _methName = "get" + propName; 90 } 91 92 try { 93 _meth = argclass.getMethod(_methName, new Class [0]); 94 } 95 catch (NoSuchMethodException x) { 96 String msg = "class {0} does not have property \"{1}\""; 97 Object [] args = new Object []{ argclass.getName(), propName }; 98 IllegalArgumentException iax = 99 new IllegalArgumentException (MessageFormat.format(msg,args)); 100 iax.initCause(x); 101 throw iax; 102 } 103 } 104 105 108 public String getPropertyName() { 109 return _propName; 110 } 111 112 114 120 public R fn(T arg) { 121 try { 122 R val = (R) getMethod().invoke(arg, new Object [0]); 126 return val; 127 } 128 catch (ClassCastException x) { 129 String msg = "{0}.{1} returns type {2}"; 130 Method m = getMethod(); 131 Object [] args = new Object []{ _argtype.getName(), m.getName(), 132 m.getReturnType().getName() }; 133 throw new EvaluationException(MessageFormat.format(msg,args), x); 134 } 135 catch (IllegalAccessException x) { 136 String msg = "{0}.{1} is not accessible"; 137 Object [] args = new Object []{ _argtype.getName(), getMethod().getName()}; 138 throw new EvaluationException(MessageFormat.format(msg,args), x); 139 } 140 catch (InvocationTargetException x) { 141 String msg = "{0}.{1} failed : "+x.getMessage(); 142 Object [] args = new Object []{ _argtype.getName(), getMethod().getName()}; 143 throw new EvaluationException(MessageFormat.format(msg,args), x); 144 } 145 } 146 147 private Method getMethod() { 148 if (_meth == null) { 149 try { 150 _meth = _argtype.getMethod(_methName, new Class [0]); 151 } 152 catch (NoSuchMethodException x) { 153 String msg = "class {0} does not have method {1}"; 154 Object [] args = new Object []{ _argtype.getName(), _methName}; 155 throw new EvaluationException(MessageFormat.format(msg,args), x); 156 } 157 } 158 159 return _meth; 160 } 161 162 166 public void accept(net.sf.jga.fn.Visitor v) { 167 if (v instanceof GetProperty.Visitor) 168 ((GetProperty.Visitor)v).visit(this); 169 else 170 v.visit(this); 171 } 172 173 175 public String toString() { 176 return "GetProperty["+_propName+"]"; 177 } 178 179 181 185 public interface Visitor extends net.sf.jga.fn.Visitor { 186 public void visit(GetProperty host); 187 } 188 } 189 | Popular Tags |