X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Futil%2FReflection.java;h=a1c1dde8a0a759707504a24d256af9a85e16d6f1;hp=cf683f0871140f77def03ce5995d692153d06ca2;hb=e014c992f89fea2185be2d249d894e588524daad;hpb=0a0227b9180534d2a431f3d6e08a398bde2244c4 diff --git a/src/edu/berkeley/sbp/util/Reflection.java b/src/edu/berkeley/sbp/util/Reflection.java index cf683f0..a1c1dde 100644 --- a/src/edu/berkeley/sbp/util/Reflection.java +++ b/src/edu/berkeley/sbp/util/Reflection.java @@ -1,11 +1,15 @@ +// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license + 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 { - - 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)); @@ -23,10 +27,16 @@ public final class Reflection { } if (c == String.class) { boolean ok = true; - for(int i=0; iClass.forName that returns null instead of throwing an exception */ public static Class forNameOrNull(String s) { try { @@ -72,6 +126,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 { @@ -119,4 +174,172 @@ 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; + } + + 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