9e5564ebbf72fb995abda48ef577eaf3612adc11
[sbp.git] / src / edu / berkeley / sbp / bind / Bindable.java
1 package edu.berkeley.sbp.bind;
2
3 import edu.berkeley.sbp.util.*;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.chr.*;
6 import edu.berkeley.sbp.bind.*;
7 import java.util.*;
8 import java.lang.annotation.*;
9 import java.lang.reflect.*;
10 import java.io.*;
11 import static edu.berkeley.sbp.util.Reflection.*;
12
13 public abstract class Bindable {
14
15     public abstract String getSimpleName();
16     public abstract String toString();
17     public abstract <A extends Annotation> A getAnnotation(Class<A> c);
18     public abstract Object impose(Object[] fields);
19     public boolean isAnnotationPresent(Class<? extends Annotation> c) { return getAnnotation(c) != null; }
20
21     public abstract Annotation[][] getArgAnnotations();
22     public abstract String[]       getArgNames();
23
24     public static Bindable create(Object o) {
25         if (o instanceof Class) return new BindableClass((Class)o);
26         if (o instanceof Method) return new BindableMethod((Method)o);
27         if (o instanceof Constructor) return new BindableConstructor((Constructor)o);
28         return null;
29     }
30
31     private static class BindableMethod extends Bindable {
32         private final Method _method;
33         public String toString() { return "BindableMethod["+_method+"]"; }
34         public BindableMethod(Method _method) { this._method = _method; }
35         public String getSimpleName() { return _method.getName(); }
36         public <A extends Annotation> A getAnnotation(Class<A> c) { return _method.getAnnotation(c); }
37         public Object impose(Object[] fields) { return Reflection.impose(_method, fields); }
38         public Annotation[][] getArgAnnotations() { return _method.getParameterAnnotations(); }
39         public String[]       getArgNames() { return new String[_method.getParameterTypes().length]; }
40     }
41
42     private static class BindableConstructor extends Bindable {
43         private final Constructor _constructor;
44         public String toString() { return "BindableConstructor["+_constructor+"]"; }
45         public BindableConstructor(Constructor _constructor) { this._constructor = _constructor; }
46         public String getSimpleName() { return _constructor.getName(); }
47         public <A extends Annotation> A getAnnotation(Class<A> c) { return _constructor.getAnnotation(c); }
48         public Object impose(Object[] fields) { return Reflection.impose(_constructor, fields); }
49         public Annotation[][] getArgAnnotations() { return _constructor.getParameterAnnotations(); }
50         public String[]       getArgNames() { return new String[_constructor.getParameterTypes().length]; }
51     }
52
53     private static class BindableClass extends Bindable {
54         private final Class _class;
55         public String toString() { return "BindableClass["+_class+"]"; }
56         public BindableClass(Class _class) { this._class = _class; }
57         public String getSimpleName() { return _class.getSimpleName(); }
58         public <A extends Annotation> A getAnnotation(Class<A> c) { return (A)_class.getAnnotation(c); }
59         public Object impose(Object[] fields) { return Reflection.impose(_class, fields); }
60         public Annotation[][] getArgAnnotations() {
61             Field[] fields = _class.getFields();
62             Annotation[][] ret = new Annotation[fields.length][];
63             for(int i=0; i<fields.length; i++)
64                 ret[i] = fields[i].getAnnotations();
65             return ret;
66         }
67         public String[]       getArgNames() {
68             Field[] fields = _class.getFields();
69             String[] ret = new String[fields.length];
70             for(int i=0; i<fields.length; i++)
71                 ret[i] = fields[i].getName();
72             return ret;
73         }
74     }
75
76 }