- public Object invokeRaw(Iterable<Tree> t) {
- try {
- return _method.invoke(null, new Object[] { t });
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- public Object plant(Object[] fields, int[] map) {
- try {
- Class[] argTypes = _method.getParameterTypes();
- Object[] args = new Object[argTypes.length];
- int j = 0;
- for(int i=0; i<args.length; i++)
- if (map[i] != -1) {
- Object tgt = Reflection.lub(fields[map[i]]);
- if (argTypes[i] == String.class) tgt = stringify(tgt);
- // FUGLY
- tgt = coerce(tgt, argTypes[i]);
- System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
- args[i] = tgt;
- }
- System.err.println("invoking " + _method + " with " + Reflection.show(args));
- return _method.invoke(null, args);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- }
-
- 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;