| 3068 | } |
| 3069 | |
| 3070 | public static class NewExpr implements Expr{ |
| 3071 | public final IPersistentVector args; |
| 3072 | public final Constructor ctor; |
| 3073 | public final Class c; |
| 3074 | final static Method invokeConstructorMethod = |
| 3075 | Method.getMethod("Object invokeConstructor(Class,Object[])"); |
| 3076 | final static Method forNameMethod = Method.getMethod("Class classForName(String)"); |
| 3077 | |
| 3078 | public NewExpr(Class c, Constructor preferredConstructor, IPersistentVector args, int line, int column) { |
| 3079 | checkMethodArity(preferredConstructor, RT.count(args)); |
| 3080 | |
| 3081 | this.args = args; |
| 3082 | this.c = c; |
| 3083 | this.ctor = preferredConstructor; |
| 3084 | } |
| 3085 | |
| 3086 | public NewExpr(Class c, IPersistentVector args, int line, int column) { |
| 3087 | this.args = args; |
| 3088 | this.c = c; |
| 3089 | Constructor[] allctors = c.getConstructors(); |
| 3090 | ArrayList ctors = new ArrayList(); |
| 3091 | ArrayList<Class[]> params = new ArrayList(); |
| 3092 | ArrayList<Class> rets = new ArrayList(); |
| 3093 | for(int i = 0; i < allctors.length; i++) |
| 3094 | { |
| 3095 | Constructor ctor = allctors[i]; |
| 3096 | if(ctor.getParameterTypes().length == args.count()) |
| 3097 | { |
| 3098 | ctors.add(ctor); |
| 3099 | params.add(ctor.getParameterTypes()); |
| 3100 | rets.add(c); |
| 3101 | } |
| 3102 | } |
| 3103 | if(ctors.isEmpty()) |
| 3104 | throw new IllegalArgumentException("No matching ctor found for " + c); |
| 3105 | |
| 3106 | int ctoridx = 0; |
| 3107 | if(ctors.size() > 1) |
| 3108 | { |
| 3109 | ctoridx = getMatchingParams(c.getName(), params, args, rets); |
| 3110 | } |
| 3111 | |
| 3112 | this.ctor = ctoridx >= 0 ? (Constructor) ctors.get(ctoridx) : null; |
| 3113 | if(ctor == null && RT.booleanCast(RT.WARN_ON_REFLECTION.deref())) |
| 3114 | { |
| 3115 | RT.errPrintWriter() |
| 3116 | .format("Reflection warning, %s:%d:%d - call to %s ctor can't be resolved.\n", |
| 3117 | SOURCE_PATH.deref(), line, column, c.getName()); |
| 3118 | } |
| 3119 | } |
| 3120 | |
| 3121 | public Object eval() { |
| 3122 | Object[] argvals = new Object[args.count()]; |
| 3123 | for(int i = 0; i < args.count(); i++) |
| 3124 | argvals[i] = ((Expr) args.nth(i)).eval(); |
| 3125 | if(this.ctor != null) |
| 3126 | { |
| 3127 | try |