| 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 import net.sf.jga.util.ArrayUtils; 41 42 50 51 public class Construct<R> extends UnaryFunctor<Object [],R> { 52 53 static final long serialVersionUID = -4682718030847292345L; 54 55 private Class <R> _objclass; 57 58 private Class [] _argclasses; 60 61 private transient Constructor <R> _ctor; 63 64 65 72 public Construct(Class <R> ctorclass, Class ... argclasses) { 73 this(argclasses, ctorclass); 74 } 75 76 83 public Construct(Class [] argclasses, Class <R> ctorclass) { 84 if (argclasses == null) { 85 String msg = "Argument Classes must be specified"; 86 throw new IllegalArgumentException (msg); 87 } 88 89 if (ctorclass == null) { 90 String msg = "Class to be constructed must be specified"; 91 throw new IllegalArgumentException (msg); 92 } 93 94 _objclass = ctorclass; 95 _argclasses = argclasses; 96 97 try { 98 _ctor = ctorclass.getConstructor(argclasses); 99 } 100 catch (NoSuchMethodException x) { 101 String msg = "No constructor for class {0} takes argument(s) of type(s) {1}"; 102 Object [] msgargs = new Object []{_objclass.getName(), ArrayUtils.toString(_argclasses)}; 103 IllegalArgumentException x1 = 104 new IllegalArgumentException (MessageFormat.format(msg,msgargs)); 105 x1.initCause(x); 106 throw x1; 107 } 108 } 109 110 114 public Construct(Constructor <R> ctor) { 115 if (ctor == null) { 116 String msg = "constructor must be specified"; 117 throw new IllegalArgumentException (msg); 118 } 119 120 _ctor = ctor; 121 _objclass = ctor.getDeclaringClass(); 122 _argclasses = ctor.getParameterTypes(); 123 } 124 125 129 public synchronized Constructor <R> getConstructor() throws NoSuchMethodException { 130 if (_ctor == null) 131 _ctor = _objclass.getConstructor(_argclasses); 132 133 return _ctor; 134 } 135 136 138 144 public R fn(Object [] args) { 145 try { 146 return (R) getConstructor().newInstance(args); 147 } 148 catch (NoSuchMethodException x) { 149 String msg = "No constructor for class {0} takes argument(s) of type(s) {1}"; 150 Object [] msgargs = new Object []{_objclass.getName(), ArrayUtils.toString(_argclasses)}; 151 throw new EvaluationException(MessageFormat.format(msg,msgargs), x); 152 } 153 catch (InstantiationException x) { 154 String msg = "class {0} is abstract: cannot be constructed"; 155 Object [] msgargs = new Object []{_objclass.getName()}; 156 throw new EvaluationException(MessageFormat.format(msg,msgargs), x); 157 } 158 catch (IllegalAccessException x) { 159 String msg = "class {0} ctor({1}) is not accessible"; 160 Object [] msgargs = new Object []{_objclass.getName(), ArrayUtils.toString(_argclasses)}; 161 throw new EvaluationException(MessageFormat.format(msg,msgargs), x); 162 } 163 catch (InvocationTargetException x) { 164 String msg = "class {0} ctor({1}) failed: "+x.getMessage(); 165 Object [] msgargs = new Object []{_objclass.getName(), ArrayUtils.toString(_argclasses)}; 166 throw new EvaluationException(MessageFormat.format(msg,msgargs), x); 167 } 168 catch (ClassCastException x) { 169 String msg = "ClassCastException: " +_objclass +"("+ArrayUtils.toString(args)+")"; 170 throw new EvaluationException(msg, x); 171 } 172 catch (IllegalArgumentException x) { 173 String msg = "class {0} ctor({1}) cannot be called with {2}"; 174 Object [] msgargs = new Object []{_objclass.getName(), ArrayUtils.toString(_argclasses), 175 ArrayUtils.toString(args)}; 176 throw new EvaluationException(MessageFormat.format(msg,msgargs), x); 177 } 178 } 179 180 184 public void accept(net.sf.jga.fn.Visitor v) { 185 if (v instanceof Construct.Visitor) 186 ((Construct.Visitor)v).visit(this); 187 else 188 v.visit(this); 189 } 190 191 193 public String toString() { 194 return "Construct("+_objclass.getName()+".getConstructor("+ArrayUtils.toString(_argclasses)+"))"; 195 } 196 197 199 203 public interface Visitor extends net.sf.jga.fn.Visitor { 204 public void visit(Construct host); 205 } 206 } 207 | Popular Tags |