X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Futil%2FReflection.java;h=fd43a454fce23b41c5ade3192337aa65a0537ead;hp=fea79e36a57b616300d6fd6966e9400ff8c80096;hb=74e53cbf7227f5958ff7495abe31cb0462d62c4e;hpb=d79a5062fbe0211d0ad3459d6a545a5270f6efbd diff --git a/src/edu/berkeley/sbp/util/Reflection.java b/src/edu/berkeley/sbp/util/Reflection.java index fea79e3..fd43a45 100644 --- a/src/edu/berkeley/sbp/util/Reflection.java +++ b/src/edu/berkeley/sbp/util/Reflection.java @@ -4,8 +4,8 @@ import java.lang.reflect.*; /** Random reflection-related utilities */ public final class Reflection { - - private static Object rebuild(Object o, Class c) { + + public static Object rebuild(Object o, Class c) { //System.out.println("rebuild " + o + " (a " + (o==null?null:o.getClass().getName()) + ") " + c); if (o==null || c.isAssignableFrom(o.getClass())) return o; if ((c == Character.class || c == Character.TYPE) && o instanceof String && ((String)o).length()==1) return new Character(((String)o).charAt(0)); @@ -40,6 +40,7 @@ public final class Reflection { Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1); ret[0] = o; return ret; + } else if (c==int.class && o instanceof Number) { return o; } else { throw new Error("unable to cast " + o + " from " + o.getClass().getName() + " to " + c.getName()); } @@ -50,10 +51,43 @@ public final class Reflection { return (Object[])Array.newInstance(c, i); } + public static String indent(String s, int indent) { + if (s.indexOf('\n')==-1) return s; + StringBuffer ret = new StringBuffer(); + for(int i=0; iClass.forName that returns null instead of throwing an exception */ public static Class forNameOrNull(String s) { try { @@ -78,6 +118,7 @@ public final class Reflection { } } + public static Object fuzzyInvoke(Object o, Member m) { return fuzzyInvoke(o, m, new Object[0]); } /** invoke method/constructor m on object o with arguments args, and perform reasonable coercions as needed */ public static Object fuzzyInvoke(Object o, Member m, Object[] args) { try { @@ -125,4 +166,35 @@ public final class Reflection { return o+""; } + public static boolean isConcrete(Class c) { + if ((c.getModifiers() & Modifier.ABSTRACT) != 0) return false; + if ((c.getModifiers() & Modifier.INTERFACE) != 0) return false; + return true; + } + public static boolean isStatic(Field f) { + if ((f.getModifiers() & Modifier.STATIC) != 0) return true; + return false; + } + + public static Field getField(Class c, String s) { + try { + for(Field f : c.getDeclaredFields()) + if (f.getName().equals(s)) + return f; + } catch (Exception e) { } + if (c.getSuperclass()==null || c.getSuperclass()==c) return null; + return getField(c.getSuperclass(), s); + } + public static Field getField(Class c, int i) { + try { + for(Field f : c.getDeclaredFields()) { + if (isStatic(f)) continue; + return f; + } + if (c.getSuperclass()==null || c.getSuperclass()==c) return null; + return getField(c.getSuperclass(), i); + } catch (Exception e) { } + return null; + } + }