X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fbind%2FBindable.java;fp=src%2Fedu%2Fberkeley%2Fsbp%2Fbind%2FBindable.java;h=9e5564ebbf72fb995abda48ef577eaf3612adc11;hp=0000000000000000000000000000000000000000;hb=0bf2fced924400a496fb154be71188864296564c;hpb=3ba82cd7dde22a9ef2c9c094a51ea5df6bcf687e diff --git a/src/edu/berkeley/sbp/bind/Bindable.java b/src/edu/berkeley/sbp/bind/Bindable.java new file mode 100644 index 0000000..9e5564e --- /dev/null +++ b/src/edu/berkeley/sbp/bind/Bindable.java @@ -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 getAnnotation(Class c); + public abstract Object impose(Object[] fields); + public boolean isAnnotationPresent(Class 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 getAnnotation(Class 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 getAnnotation(Class 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 getAnnotation(Class 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