return null;
}
public Object plant(Object[] fields) {
- try {
- Object ret = _class.newInstance();
- Field[] f = _class.getFields();
- int j = 0;
- for(int i=0; i<f.length; i++) {
- Object tgt = Reflection.lub(fields[i]);
- if (f[i].getType() == String.class) tgt = stringify(tgt);
- // FUGLY
- tgt = coerce(tgt, f[i].getType());
- System.err.println("setting a " + f[i].getType().getName() + " to " + Reflection.show(tgt));
- f[i].set(ret, tgt);
- }
- return ret;
- } catch (Exception e) {
- e.printStackTrace();
- throw new RuntimeException(e);
- }
+ return Reflection.impose(_class, fields);
}
- }
-
- public static String stringify(Object o) {
- if (o==null) return "";
- if (!(o instanceof Object[])) return o.toString();
- Object[] arr = (Object[])o;
- StringBuffer ret = new StringBuffer();
- for(int i=0; i<arr.length; i++)
- ret.append(arr[i]);
- return ret.toString();
+
}
public static class TargetConstructor extends Target {
int j = 0;
for(int i=0; i<args.length; i++) {
Object tgt = Reflection.lub(fields[i]);
- if (argTypes[i] == String.class) tgt = stringify(tgt);
+ if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
// FUGLY
- tgt = coerce(tgt, argTypes[i]);
+ tgt = Reflection.coerce(tgt, argTypes[i]);
System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
args[i] = tgt;
}
int j = 0;
for(int i=0; i<args.length; i++) {
Object tgt = Reflection.lub(fields[i]);
- if (argTypes[i] == String.class) tgt = stringify(tgt);
+ if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
// FUGLY
- tgt = coerce(tgt, argTypes[i]);
+ tgt = Reflection.coerce(tgt, argTypes[i]);
System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
args[i] = tgt;
}
}
}
- public static Object coerce(Object o, Class c) {
- if (o==null) return null;
- if (c.isInstance(o)) return o;
- if (c == char.class) {
- return o.toString().charAt(0);
- }
-
- if (o.getClass().isArray() &&
- o.getClass().getComponentType().isArray() &&
- o.getClass().getComponentType().getComponentType() == String.class &&
- c.isArray() &&
- c.getComponentType() == String.class) {
- String[] ret = new String[((Object[])o).length];
- for(int i=0; i<ret.length; i++) {
- StringBuffer sb = new StringBuffer();
- for(Object ob : (Object[])(((Object[])o)[i]))
- sb.append(ob);
- ret[i] = sb.toString();
- }
- return ret;
- }
-
- if (c.isArray() && (c.getComponentType().isInstance(o))) {
- Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
- ret[0] = o;
- return ret;
- }
-
- if (o.getClass().isArray() && c.isArray()) {
- boolean ok = true;
- for(int i=0; i<((Object[])o).length; i++) {
- Object ob = (((Object[])o)[i]);
- if (ob != null) {
- System.err.println("no hit with " + c.getComponentType().getName() + " on " + Reflection.show(((Object[])o)[i]));
- ok = false;
- }
- }
- if (ok) {
- System.err.println("hit with " + c.getComponentType().getName());
- return Array.newInstance(c.getComponentType(), ((Object[])o).length);
- }
- }
- return o;
- }
-
public static Union cached = null;
public static Union make() {
if (cached != null) return cached;
public static interface Show {
}
+
+ public static Object impose(Class _class, Object[] fields) {
+ try {
+ Object ret = _class.newInstance();
+ Field[] f = _class.getFields();
+ int j = 0;
+ for(int i=0; i<f.length; i++) {
+ Object tgt = Reflection.lub(fields[i]);
+ if (f[i].getType() == String.class) tgt = stringify(tgt);
+ // FUGLY
+ tgt = coerce(tgt, f[i].getType());
+ System.err.println("setting a " + f[i].getType().getName() + " to " + Reflection.show(tgt));
+ f[i].set(ret, tgt);
+ }
+ return ret;
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static String stringify(Object o) {
+ if (o==null) return "";
+ if (!(o instanceof Object[])) return o.toString();
+ Object[] arr = (Object[])o;
+ StringBuffer ret = new StringBuffer();
+ for(int i=0; i<arr.length; i++)
+ ret.append(arr[i]);
+ return ret.toString();
+ }
+
+ public static Object coerce(Object o, Class c) {
+ if (o==null) return null;
+ if (c.isInstance(o)) return o;
+ if (c == char.class) {
+ return o.toString().charAt(0);
+ }
+
+ if (o.getClass().isArray() &&
+ o.getClass().getComponentType().isArray() &&
+ o.getClass().getComponentType().getComponentType() == String.class &&
+ c.isArray() &&
+ c.getComponentType() == String.class) {
+ String[] ret = new String[((Object[])o).length];
+ for(int i=0; i<ret.length; i++) {
+ StringBuffer sb = new StringBuffer();
+ for(Object ob : (Object[])(((Object[])o)[i]))
+ sb.append(ob);
+ ret[i] = sb.toString();
+ }
+ return ret;
+ }
+
+ if (c.isArray() && (c.getComponentType().isInstance(o))) {
+ Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
+ ret[0] = o;
+ return ret;
+ }
+
+ if (o.getClass().isArray() && c.isArray()) {
+ boolean ok = true;
+ for(int i=0; i<((Object[])o).length; i++) {
+ Object ob = (((Object[])o)[i]);
+ if (ob != null) {
+ System.err.println("no hit with " + c.getComponentType().getName() + " on " + Reflection.show(((Object[])o)[i]));
+ ok = false;
+ }
+ }
+ if (ok) {
+ System.err.println("hit with " + c.getComponentType().getName());
+ return Array.newInstance(c.getComponentType(), ((Object[])o).length);
+ }
+ }
+ return o;
+ }
+
}