X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Futil%2FReflection.java;h=cb45e8f4630d08ccaba01d54cf4bbe9e3f380d74;hb=c8d1c3a25e2bfa9645c4b91d3dde9c4857f636f2;hp=247652a1dd333eaa375ac8a410b6c710a2c28f1d;hpb=3784457891d6a8b17429a16493cd65501e02e174;p=sbp.git diff --git a/src/edu/berkeley/sbp/util/Reflection.java b/src/edu/berkeley/sbp/util/Reflection.java index 247652a..cb45e8f 100644 --- a/src/edu/berkeley/sbp/util/Reflection.java +++ b/src/edu/berkeley/sbp/util/Reflection.java @@ -1,7 +1,9 @@ package edu.berkeley.sbp.util; import java.io.*; import java.lang.reflect.*; +import java.lang.annotation.*; +// FIXME: decent error reporting /** Random reflection-related utilities */ public final class Reflection { @@ -53,19 +55,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((Show)o); if (! (o instanceof Object[])) return o.toString() + " : " + o.getClass().getName(); Object[] arr = (Object[])o; StringBuffer ret = new StringBuffer(); @@ -76,6 +66,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 +201,141 @@ 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) { + Object ret = null; + try { ret = _class.newInstance(); } + catch (Exception e) { rethrow(e, "while trying to instantiate a " + _class.getName()); } + Field[] f = _class.getFields(); + int j = 0; + for(int i=0; i