checkpoint
[sbp.git] / src / edu / berkeley / sbp / util / Reflection.java
index 5c13668..42267ce 100644 (file)
@@ -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());
         }
@@ -145,6 +146,10 @@ public final class Reflection {
         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 {
@@ -152,6 +157,18 @@ public final class Reflection {
                 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;
     }