1 33 package net.sf.jga.fn.property; 34 35 import java.lang.reflect.Constructor ; 36 import java.lang.reflect.InvocationTargetException ; 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 ConstructUnary<T,R> extends UnaryFunctor<T,R> { 51 52 static final long serialVersionUID = -6046849006655934333L; 53 54 private Class <R> _objclass; 56 57 private Class <T> _argclass; 59 60 private transient Constructor <R> _ctor; 62 63 75 76 public ConstructUnary(Class <T> argclass, Class <R> ctorclass) { 77 if (argclass == null) { 78 String msg = "Argument Class must be specified"; 79 throw new IllegalArgumentException (msg); 80 } 81 82 if (ctorclass == null) { 83 String msg = "Class to be constructed must be specified"; 84 throw new IllegalArgumentException (msg); 85 } 86 87 try { 88 _argclass = argclass; 89 _objclass = ctorclass; 90 _ctor = getConstructor(); 91 } 92 catch (NoSuchMethodException x) { 93 String msg = "No constructor found for class {0} that takes an argument of class {1}"; 94 Object args[] = new Object [] { ctorclass, argclass }; 95 96 IllegalArgumentException x1 = 97 new IllegalArgumentException (MessageFormat.format(msg, args)); 98 x1.initCause(x); 99 throw x1; 100 } 101 } 102 103 104 107 public synchronized Constructor <R> getConstructor() throws NoSuchMethodException { 108 if (_ctor == null) 109 _ctor = _objclass.getConstructor(new Class []{_argclass}); 110 111 return _ctor; 112 } 113 114 115 117 122 public R fn(T arg) { 123 124 try { 125 R val = (R) getConstructor().newInstance(new Object []{arg}); 126 return val; 127 } 128 catch (NoSuchMethodException x) { 129 String msg = "No constructor for class {0} that takes an argument of type {1}"; 130 Object [] args = new Object []{_objclass.getName(), _argclass.getName()}; 131 throw new EvaluationException(MessageFormat.format(msg,args), x); 132 } 133 catch (InstantiationException x) { 134 String msg = "class {0} is abstract: cannot be constructed"; 135 Object [] args = new Object []{_objclass.getName()}; 136 throw new EvaluationException(MessageFormat.format(msg,args), x); 137 } 138 catch (IllegalArgumentException x) { 139 String msg = "class {0} ctor({1}) cannot be called with {2}"; 140 Object [] args = new Object []{_objclass.getName(), _argclass.getName(), arg}; 141 throw new EvaluationException(MessageFormat.format(msg,args), x); 142 } 143 catch (IllegalAccessException x) { 144 String msg = "class {0} ctor({1}) is not accessible"; 145 Object [] args = new Object []{_objclass.getName(), _argclass.getName()}; 146 throw new EvaluationException(MessageFormat.format(msg,args), x); 147 } 148 catch (InvocationTargetException x) { 149 String msg = "class {0} ctor({1}) failed: "+x.getMessage(); 150 Object [] args = new Object []{_objclass.getName(), _argclass.getName()}; 151 throw new EvaluationException(MessageFormat.format(msg,args), x); 152 } 153 } 154 155 156 160 public void accept(net.sf.jga.fn.Visitor v) { 161 if (v instanceof ConstructUnary.Visitor) 162 ((ConstructUnary.Visitor)v).visit(this); 163 else 164 v.visit(this); 165 } 166 167 169 public String toString() { 170 return "new "+_objclass.getName()+"(["+_argclass.getName()+"])"; 171 } 172 173 175 179 public interface Visitor extends net.sf.jga.fn.Visitor { 180 public void visit(ConstructUnary host); 181 } 182 } 183 | Popular Tags |