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.BinaryFunctor; 39 import net.sf.jga.fn.EvaluationException; 40 import net.sf.jga.fn.UnaryFunctor; 41 import net.sf.jga.fn.adaptor.ChainBinary; 42 import net.sf.jga.fn.adaptor.ChainUnary; 43 44 56 57 public class InvokeNoArgMethod<T,R> extends UnaryFunctor<T,R> { 58 59 static final long serialVersionUID = -2651164047444243205L; 60 61 private String _methName; 63 64 private Class <T> _objclass; 66 67 private transient Method _meth; 69 70 74 75 public InvokeNoArgMethod(Class <T> objclass, String methName) { 76 if (methName == null || methName.length() == 0) { 77 String msg = "Must supply method name"; 78 throw new IllegalArgumentException (msg); 79 } 80 81 if (objclass == null) { 82 String msg = "Must supply object class"; 83 throw new IllegalArgumentException (msg); 84 } 85 86 _methName = methName; 87 _objclass = objclass; 88 89 try { 90 _meth = objclass.getMethod(_methName, new Class [0]); 91 } 92 catch (NoSuchMethodException x) { 93 String msg = "No such method: "+x.getMessage(); 94 IllegalArgumentException iax = new IllegalArgumentException (msg); 95 iax.initCause(x); 96 throw iax; 97 } 98 } 99 100 105 106 public InvokeNoArgMethod(Class <T> objClass, Method method) { 107 if (method == null) { 108 String msg = "Must supply method"; 109 throw new IllegalArgumentException (msg); 110 } 111 112 if (objClass == null) { 113 String msg = "Must supply object class"; 114 throw new IllegalArgumentException (msg); 115 } 116 117 if (method.getParameterTypes().length != 0) { 118 String msg = "Method {0} takes arguments"; 119 Object [] args = { method.getName() }; 120 throw new IllegalArgumentException (MessageFormat.format(msg, args)); 121 } 122 123 if (!method.getDeclaringClass().isAssignableFrom(objClass)) { 124 String msg = "Method {0} not defined for class {1}"; 125 Object [] args = { method.getName(), objClass.getName() }; 126 throw new IllegalArgumentException (MessageFormat.format(msg, args)); 127 } 128 129 _objclass = objClass; 130 _meth = method; 131 _methName = method.getName(); 132 } 133 134 137 public String getMethodName() { 138 return _methName; 139 } 140 141 144 public Class <R> getReturnType() { 145 return (Class <R>) _meth.getReturnType(); 146 } 147 148 151 public synchronized Method getMethod() throws NoSuchMethodException { 152 if (_meth == null) 153 _meth = _objclass.getMethod(_methName, new Class [0]); 154 155 return _meth; 156 } 157 158 160 166 public R fn(T obj) { 167 try { 168 return (R) getMethod().invoke(obj, new Object [0]); 172 } 173 catch (NoSuchMethodException x) { 174 String msg = "No method {0} for class {1} that takes no arguments"; 175 Object [] args = new Object []{_methName, _objclass.getName()}; 176 throw new EvaluationException(MessageFormat.format(msg,args), x); 177 } 178 catch (ClassCastException x) { 179 String msg = "ClassCastException: " +_objclass +"." +_methName +"()"; 180 throw new EvaluationException(msg, x); 181 } 182 catch (IllegalAccessException x) { 183 String msg = _objclass +"." +_methName +" is not accessible"; 184 throw new EvaluationException(msg, x); 185 } 186 catch (InvocationTargetException x) { 187 String xmsg = x.getMessage(); 188 String msg = "InvocationException: " +_objclass +"." +_methName 189 +"()"+(xmsg != null ? (":"+xmsg) : ""); 190 throw new EvaluationException(msg, x); 191 } 192 } 193 194 195 199 public void accept(net.sf.jga.fn.Visitor v) { 200 if (v instanceof InvokeNoArgMethod.Visitor) 201 ((InvokeNoArgMethod.Visitor)v).visit(this); 202 else 203 v.visit(this); 204 } 205 206 208 public String toString() { 209 return "InvokeNoArgMethod("+_meth+")"; 210 } 211 212 214 218 public interface Visitor extends net.sf.jga.fn.Visitor { 219 public void visit(InvokeNoArgMethod host); 220 } 221 } 222 | Popular Tags |