checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / Demo.java
index 359f715..140af81 100644 (file)
@@ -184,6 +184,10 @@ public class Demo {
         public abstract tag getTag();
         public abstract nonterminal getNonTerminal();
         public abstract int[] buildSequence(Production p);
+
+        private Reflection.Bindable _bindable;
+        public Target(Reflection.Bindable b) { this._bindable = b; }
+
         public boolean isCompatible(Production p) {
             tag t = getTag();
             if (t != null &&
@@ -262,7 +266,7 @@ public class Demo {
 
     public static class TargetClass extends Target {
         public final Class _class;
-        public TargetClass(Class _class) { this._class = _class; }
+        public TargetClass(Class _class) { super(Reflection.Bindable.create(_class)); this._class = _class; }
         public String getName() { return _class.getSimpleName(); }
         public tag getTag() { return (tag)_class.getAnnotation(tag.class); }
         public nonterminal getNonTerminal() { return (nonterminal)_class.getAnnotation(nonterminal.class); }
@@ -290,7 +294,7 @@ public class Demo {
 
     public static class TargetConstructor extends Target {
         public final Constructor _ctor;
-        public TargetConstructor(Constructor _ctor) { this._ctor = _ctor; }
+        public TargetConstructor(Constructor _ctor) { super(Reflection.Bindable.create(_ctor)); this._ctor = _ctor; }
         public String getName() { return _ctor.getName(); }
         public tag getTag() { return (tag)_ctor.getAnnotation(tag.class); }
         public nonterminal getNonTerminal() { return (nonterminal)_ctor.getAnnotation(nonterminal.class); }
@@ -315,27 +319,12 @@ public class Demo {
             return buildSequence(p, names, argtags);
         }
         public Object plant(Object[] fields) {
-            try {
-                Class[] argTypes = _ctor.getParameterTypes();
-                Object[] args = new Object[argTypes.length];
-                int j = 0;
-                for(int i=0; i<args.length; i++) {
-                    Object tgt = Reflection.lub(fields[i]);
-                    if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
-                    // FUGLY
-                    tgt = Reflection.coerce(tgt, argTypes[i]);
-                    System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
-                    args[i] = tgt;
-                }
-                return _ctor.newInstance(args);
-            } catch (Exception e) {
-                throw new RuntimeException(e);
-            }
+            return Reflection.impose(_ctor, fields);
         }
     }
     public static class TargetMethod extends Target {
         public final Method _method;
-        public TargetMethod(Method _method) { this._method = _method; }
+        public TargetMethod(Method _method) { super(Reflection.Bindable.create(_method)); this._method = _method; }
         public String getName() { return _method.getName(); }
         public String toString() { return _method.getName(); }
         public tag getTag() { return (tag)_method.getAnnotation(tag.class); }
@@ -353,31 +342,12 @@ public class Demo {
         }
         public boolean isRaw() { return _method.isAnnotationPresent(raw.class); }
         public Object invokeRaw(Iterable<Tree<Object>> t) {
-            try {
-                return _method.invoke(null, new Object[] { t });
-            } catch (Exception e) {
-                throw new RuntimeException(e);
-            }
+            return Reflection.impose(_method, new Object[] { t });
         }
         public Object plant(Object[] fields) {
-            try {
-                Class[] argTypes = _method.getParameterTypes();
-                Object[] args = new Object[argTypes.length];
-                int j = 0;
-                for(int i=0; i<args.length; i++) {
-                    Object tgt = Reflection.lub(fields[i]);
-                    if (argTypes[i] == String.class) tgt = Reflection.stringify(tgt);
-                    // FUGLY
-                    tgt = Reflection.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);
-            }
+            return Reflection.impose(_method, fields);
         }
+        
     }
 
     public static Union cached = null;