X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Futil%2FReflection.java;h=cb45e8f4630d08ccaba01d54cf4bbe9e3f380d74;hb=f09d2abb95f106197aea99c345282c3bf0cd3717;hp=da16336265460f02540e50e264483187e6927fe9;hpb=eec21f64634c3a76436a263254fc890b56b5b34d;p=sbp.git diff --git a/src/edu/berkeley/sbp/util/Reflection.java b/src/edu/berkeley/sbp/util/Reflection.java index da16336..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,7 +55,7 @@ public final class Reflection { public static String show(Object o) { if (o==null) return "null"; - if (o instanceof Show) return ((Show)o).toString(); + 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(); @@ -201,4 +203,139 @@ public final class Reflection { 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