1 33 package net.sf.jga.fn.property; 34 35 import java.lang.reflect.InvocationTargetException ; 36 import java.lang.reflect.Field ; 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 GetField<T,R> extends UnaryFunctor<T,R> { 51 52 static final long serialVersionUID = 5733484457689881481L; 53 54 private String _fieldName; 56 57 private Class <T> _argtype; 59 60 private transient Field _field; 62 63 68 public GetField(Field field) { 69 if (field == null) { 70 throw new IllegalArgumentException ("Must supply field"); 71 } 72 73 _field = field; 74 _fieldName = field.getName(); 75 _argtype = (Class <T>) field.getDeclaringClass(); 79 } 80 81 86 public GetField(Class <T> argclass, Field field) { 87 if (field == null) { 88 throw new IllegalArgumentException ("Must supply field"); 89 } 90 91 if (!field.getDeclaringClass().isAssignableFrom(argclass)) { 92 throw new IllegalArgumentException (buildNoSuchFieldMessage(field.getName(), argclass)); 93 } 94 95 _field = field; 96 _fieldName = field.getName(); 97 _argtype = argclass; 98 } 99 100 106 public GetField(Class <T> argclass, String fieldName) { 107 if (fieldName == null || fieldName.length() == 0) { 108 throw new IllegalArgumentException ("Must supply field name"); 109 } 110 111 _fieldName = fieldName; 112 _argtype = argclass; 113 114 try { 115 _field = argclass.getField(_fieldName); 116 } 117 catch (NoSuchFieldException x) { 118 IllegalArgumentException iax = 119 new IllegalArgumentException (buildNoSuchFieldMessage(fieldName, argclass)); 120 121 iax.initCause(x); 122 throw iax; 123 } 124 } 125 126 129 public Class <T> getObjectType() { 131 return _argtype; 132 } 133 134 137 public String getFieldName() { 138 return _fieldName; 139 } 140 141 144 145 public Class <R> getFieldType() { 146 return (Class <R>) _field.getType(); 147 } 148 149 150 153 154 158 public synchronized Field getField() throws NoSuchFieldException { 159 if (_field == null) 160 _field = _argtype.getField(_fieldName); 161 162 return _field; 163 } 164 165 167 173 public R fn(T arg) { 174 try { 175 R val = (R) getField().get(arg); 179 return val; 180 } 181 catch (NoSuchFieldException x) { 182 throw new EvaluationException(buildNoSuchFieldMessage(_fieldName, _argtype)); 183 } 184 catch (ClassCastException x) { 185 String msg = "{0}.{1} is of type {2}"; 186 Object [] args = new Object []{ arg.getClass().getName(), _field.getName(), 187 _field.getType().getName() }; 188 throw new EvaluationException(MessageFormat.format(msg,args), x); 189 } 190 catch (IllegalAccessException x) { 191 String msg = "{0}.{1} is not accessible"; 192 Object [] args = new Object []{ _argtype.getName(), _fieldName}; 193 throw new EvaluationException(MessageFormat.format(msg,args), x); 194 } 195 } 196 197 198 200 private String buildNoSuchFieldMessage(String fieldname, Class clasz) { 201 String msg = "class {0} does not have field {1}"; 202 Object [] args = new Object []{ clasz.getName(), fieldname }; 203 return MessageFormat.format(msg,args); 204 } 205 206 210 public void accept(net.sf.jga.fn.Visitor v) { 211 if (v instanceof GetField.Visitor) 212 ((GetField.Visitor)v).visit(this); 213 else 214 v.visit(this); 215 } 216 217 219 public String toString() { 220 return "GetField["+_argtype.getName()+"."+_fieldName+"]"; 221 } 222 223 225 229 public interface Visitor extends net.sf.jga.fn.Visitor { 230 public void visit(GetField host); 231 } 232 } 233 | Popular Tags |