X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Futil%2FReflection.java;h=3a129d48db8b89c21515aba9102a63b0f2a93a87;hp=247652a1dd333eaa375ac8a410b6c710a2c28f1d;hb=0f0ed458838eb080e12ad9a52a46a4060d40df43;hpb=3784457891d6a8b17429a16493cd65501e02e174 diff --git a/src/edu/berkeley/sbp/util/Reflection.java b/src/edu/berkeley/sbp/util/Reflection.java index 247652a..3a129d4 100644 --- a/src/edu/berkeley/sbp/util/Reflection.java +++ b/src/edu/berkeley/sbp/util/Reflection.java @@ -1,6 +1,7 @@ package edu.berkeley.sbp.util; import java.io.*; import java.lang.reflect.*; +import java.lang.annotation.*; /** Random reflection-related utilities */ public final class Reflection { @@ -53,19 +54,7 @@ public final class Reflection { public static String show(Object o) { if (o==null) return "null"; - if (o instanceof Show) { - StringBuffer ret = new StringBuffer(); - for(Field f : o.getClass().getFields()) { - ret.append("\n" + f.getName() + " = "); - try { - ret.append(show(f.get(o))); - } catch (Exception e) { - ret.append("**"+e+"**"); - throw new RuntimeException(e); - } - } - return o.getClass().getName() + " {" + StringUtil.indent(ret.toString(), 4) + "\n}"; - } + if (o instanceof Show) return ((Show)o).toString(); if (! (o instanceof Object[])) return o.toString() + " : " + o.getClass().getName(); Object[] arr = (Object[])o; StringBuffer ret = new StringBuffer(); @@ -76,6 +65,20 @@ public final class Reflection { return ret.toString(); } + public static String show(Show o) { + StringBuffer ret = new StringBuffer(); + for(Field f : o.getClass().getFields()) { + ret.append("\n" + f.getName() + " = "); + try { + ret.append(show(f.get(o))); + } catch (Exception e) { + ret.append("**"+e+"**"); + throw new RuntimeException(e); + } + } + return o.getClass().getName() + " {" + StringUtil.indent(ret.toString(), 4) + "\n}"; + } + public static Object lub(Object argo) { if (argo instanceof Object[]) return lub((Object[])argo); return argo; @@ -197,5 +200,188 @@ public final class Reflection { return null; } - public static interface Show { } + public static interface Show { + } + + public static Object impose(Object o, Object[] fields) { + if (o instanceof Class) return impose((Class)o, fields); + if (o instanceof Method) return impose((Method)o, fields); + if (o instanceof Constructor) return impose((Constructor)o, fields); + return null; + } + public static Object impose(Class _class, Object[] fields) { + try { + Object ret = _class.newInstance(); + Field[] f = _class.getFields(); + int j = 0; + for(int i=0; i 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