- public boolean isRaw() { return _method.isAnnotationPresent(raw.class); }
- 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;
- }
-
- 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;