checkpoint
authoradam <adam@megacz.com>
Wed, 5 Jul 2006 02:57:30 +0000 (22:57 -0400)
committeradam <adam@megacz.com>
Wed, 5 Jul 2006 02:57:30 +0000 (22:57 -0400)
darcs-hash:20060705025730-5007d-90c1f21c28c24ee91fd7496828d2e3127dec2bd9.gz

src/edu/berkeley/sbp/bind/Bindable.java [new file with mode: 0644]
src/edu/berkeley/sbp/misc/Demo.java
src/edu/berkeley/sbp/util/Reflection.java

diff --git a/src/edu/berkeley/sbp/bind/Bindable.java b/src/edu/berkeley/sbp/bind/Bindable.java
new file mode 100644 (file)
index 0000000..9e5564e
--- /dev/null
@@ -0,0 +1,76 @@
+package edu.berkeley.sbp.bind;
+
+import edu.berkeley.sbp.util.*;
+import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.chr.*;
+import edu.berkeley.sbp.bind.*;
+import java.util.*;
+import java.lang.annotation.*;
+import java.lang.reflect.*;
+import java.io.*;
+import static edu.berkeley.sbp.util.Reflection.*;
+
+public abstract class Bindable {
+
+    public abstract String getSimpleName();
+    public abstract String toString();
+    public abstract <A extends Annotation> A getAnnotation(Class<A> c);
+    public abstract Object impose(Object[] fields);
+    public boolean isAnnotationPresent(Class<? extends Annotation> c) { return getAnnotation(c) != null; }
+
+    public abstract Annotation[][] getArgAnnotations();
+    public abstract String[]       getArgNames();
+
+    public static Bindable create(Object o) {
+        if (o instanceof Class) return new BindableClass((Class)o);
+        if (o instanceof Method) return new BindableMethod((Method)o);
+        if (o instanceof Constructor) return new BindableConstructor((Constructor)o);
+        return null;
+    }
+
+    private static class BindableMethod extends Bindable {
+        private final Method _method;
+        public String toString() { return "BindableMethod["+_method+"]"; }
+        public BindableMethod(Method _method) { this._method = _method; }
+        public String getSimpleName() { return _method.getName(); }
+        public <A extends Annotation> A getAnnotation(Class<A> c) { return _method.getAnnotation(c); }
+        public Object impose(Object[] fields) { return Reflection.impose(_method, fields); }
+        public Annotation[][] getArgAnnotations() { return _method.getParameterAnnotations(); }
+        public String[]       getArgNames() { return new String[_method.getParameterTypes().length]; }
+    }
+
+    private static class BindableConstructor extends Bindable {
+        private final Constructor _constructor;
+        public String toString() { return "BindableConstructor["+_constructor+"]"; }
+        public BindableConstructor(Constructor _constructor) { this._constructor = _constructor; }
+        public String getSimpleName() { return _constructor.getName(); }
+        public <A extends Annotation> A getAnnotation(Class<A> c) { return _constructor.getAnnotation(c); }
+        public Object impose(Object[] fields) { return Reflection.impose(_constructor, fields); }
+        public Annotation[][] getArgAnnotations() { return _constructor.getParameterAnnotations(); }
+        public String[]       getArgNames() { return new String[_constructor.getParameterTypes().length]; }
+    }
+
+    private static class BindableClass extends Bindable {
+        private final Class _class;
+        public String toString() { return "BindableClass["+_class+"]"; }
+        public BindableClass(Class _class) { this._class = _class; }
+        public String getSimpleName() { return _class.getSimpleName(); }
+        public <A extends Annotation> A getAnnotation(Class<A> c) { return (A)_class.getAnnotation(c); }
+        public Object impose(Object[] fields) { return Reflection.impose(_class, fields); }
+        public Annotation[][] getArgAnnotations() {
+            Field[] fields = _class.getFields();
+            Annotation[][] ret = new Annotation[fields.length][];
+            for(int i=0; i<fields.length; i++)
+                ret[i] = fields[i].getAnnotations();
+            return ret;
+        }
+        public String[]       getArgNames() {
+            Field[] fields = _class.getFields();
+            String[] ret = new String[fields.length];
+            for(int i=0; i<fields.length; i++)
+                ret[i] = fields[i].getName();
+            return ret;
+        }
+    }
+
+}
index 467ff38..fa6d733 100644 (file)
@@ -148,10 +148,10 @@ public class Demo {
                         argtags[i+ofs] = (bind.arg)a;
             return Target.this.buildSequence(p, names, argtags);
         }
-        private Reflection.Bindable _bindable;
+        private Bindable _bindable;
 
-        public Target(Object o) { this(Reflection.Bindable.create(o)); }
-        public Target(Reflection.Bindable b) { this._bindable = b; }
+        public Target(Object o) { this(Bindable.create(o)); }
+        public Target(Bindable b) { this._bindable = b; }
 
         public String getName() { return _bindable.getSimpleName(); }
         public bind.as getBindAs() { return (bind.as)_bindable.getAnnotation(bind.as.class); }
@@ -201,46 +201,44 @@ public class Demo {
                                               p.elements, p.labels, p.drops);
         }
 
-        public static class TargetReducer implements Tree.TreeFunctor<Object,Object> {
-            private Production p;
-            private int[] map;
-            private String name;
-            private Reflection.Bindable _bindable;
-            private boolean _israw;
-            public TargetReducer(Production p, int[] map, String name, Reflection.Bindable b, boolean raw) {
-                this.p = p;
-                this.map = map;
-                this.name = name;
-                this._bindable = b;
-                this._israw = raw;
-            }
-            public String toString() { return name; }
-            public Object invoke(Iterable<Tree<Object>> t) {
-                if (_israw) return _bindable.impose(new Object[] { t });
-                ArrayList ret = new ArrayList();
-                for(Tree tc : t) {
-                    if (tc.head() != null && tc.head() instanceof Functor)
-                        ret.add(((Tree.TreeFunctor<Object,Object>)tc.head()).invoke(tc.children()));
-                    else if (tc.numChildren() == 0)
-                        ret.add(tc.head());
-                    else {
-                        System.err.println("FIXME: don't know what to do about " + tc);
-                        ret.add(null);
-                    }
+    }
+
+    public static class TargetReducer implements Tree.TreeFunctor<Object,Object> {
+        private int[] map;
+        private String name;
+        private Bindable _bindable;
+        private boolean _israw;
+        public TargetReducer(Production p, int[] map, String name, Bindable b, boolean raw) {
+            this.map = map;
+            this.name = name;
+            this._bindable = b;
+            this._israw = raw;
+        }
+        public String toString() { return name; }
+        public Object invoke(Iterable<Tree<Object>> t) {
+            if (_israw) return _bindable.impose(new Object[] { t });
+            ArrayList ret = new ArrayList();
+            for(Tree tc : t) {
+                if (tc.head() != null && tc.head() instanceof Functor)
+                    ret.add(((Tree.TreeFunctor<Object,Object>)tc.head()).invoke(tc.children()));
+                else if (tc.numChildren() == 0)
+                    ret.add(tc.head());
+                else {
+                    System.err.println("FIXME: don't know what to do about " + tc);
+                    ret.add(null);
                 }
-                System.err.println("input tree: " + t);
-                Object[] o = (Object[])ret.toArray(new Object[0]);
-                int max = 0;
-                for(int i=0; i<map.length; i++) max = Math.max(map[i], max);
-                Object[] o2 = new Object[max+1];
-                for(int i=0; i<o.length; i++) o2[map[i]] = o[i];
-                return _bindable.impose(o2);
             }
+            System.err.println("input tree: " + t);
+            Object[] o = (Object[])ret.toArray(new Object[0]);
+            int max = 0;
+            for(int i=0; i<map.length; i++) max = Math.max(map[i], max);
+            Object[] o2 = new Object[max+1];
+            for(int i=0; i<o.length; i++) o2[map[i]] = o[i];
+            return _bindable.impose(o2);
         }
     }
 
 
-
     public static Union cached = null;
     public static Union make() {
         if (cached != null) return cached;
index 3a129d4..05de7dc 100644 (file)
@@ -323,65 +323,4 @@ public final class Reflection {
         return o;
     }
 
-    public static abstract class Bindable {
-        public abstract String getSimpleName();
-        public abstract String toString();
-        public abstract <A extends Annotation> A getAnnotation(Class<A> c);
-        public abstract Object impose(Object[] fields);
-        public boolean isAnnotationPresent(Class<? extends Annotation> c) { return getAnnotation(c) != null; }
-
-        public abstract Annotation[][] getArgAnnotations();
-        public abstract String[]       getArgNames();
-
-        public static Bindable create(Object o) {
-            if (o instanceof Class) return new BindableClass((Class)o);
-            if (o instanceof Method) return new BindableMethod((Method)o);
-            if (o instanceof Constructor) return new BindableConstructor((Constructor)o);
-            return null;
-        }
-    }
-
-    private static class BindableMethod extends Bindable {
-        private final Method _method;
-        public String toString() { return "BindableMethod["+_method+"]"; }
-        public BindableMethod(Method _method) { this._method = _method; }
-        public String getSimpleName() { return _method.getName(); }
-        public <A extends Annotation> A getAnnotation(Class<A> c) { return _method.getAnnotation(c); }
-        public Object impose(Object[] fields) { return Reflection.impose(_method, fields); }
-        public Annotation[][] getArgAnnotations() { return _method.getParameterAnnotations(); }
-        public String[]       getArgNames() { return new String[_method.getParameterTypes().length]; }
-    }
-    private static class BindableConstructor extends Bindable {
-        private final Constructor _constructor;
-        public String toString() { return "BindableConstructor["+_constructor+"]"; }
-        public BindableConstructor(Constructor _constructor) { this._constructor = _constructor; }
-        public String getSimpleName() { return _constructor.getName(); }
-        public <A extends Annotation> A getAnnotation(Class<A> c) { return _constructor.getAnnotation(c); }
-        public Object impose(Object[] fields) { return Reflection.impose(_constructor, fields); }
-        public Annotation[][] getArgAnnotations() { return _constructor.getParameterAnnotations(); }
-        public String[]       getArgNames() { return new String[_constructor.getParameterTypes().length]; }
-    }
-    private static class BindableClass extends Bindable {
-        private final Class _class;
-        public String toString() { return "BindableClass["+_class+"]"; }
-        public BindableClass(Class _class) { this._class = _class; }
-        public String getSimpleName() { return _class.getSimpleName(); }
-        public <A extends Annotation> A getAnnotation(Class<A> c) { return (A)_class.getAnnotation(c); }
-        public Object impose(Object[] fields) { return Reflection.impose(_class, fields); }
-        public Annotation[][] getArgAnnotations() {
-            Field[] fields = _class.getFields();
-            Annotation[][] ret = new Annotation[fields.length][];
-            for(int i=0; i<fields.length; i++)
-                ret[i] = fields[i].getAnnotations();
-            return ret;
-        }
-        public String[]       getArgNames() {
-            Field[] fields = _class.getFields();
-            String[] ret = new String[fields.length];
-            for(int i=0; i<fields.length; i++)
-                ret[i] = fields[i].getName();
-            return ret;
-        }
-    }
-
 }