added JSReflection.Wrap
[org.ibex.js.git] / src / org / ibex / js / JSReflection.java
index d6ecd75..d9c241f 100644 (file)
@@ -12,14 +12,60 @@ public class JSReflection extends JS.Immutable {
 
     public static JS wrap(Object o) throws JSExn {
         if (o == null) return null;
-        if (o instanceof String) return Script.S((String)o);
-        if (o instanceof Boolean) return Script.B(((Boolean)o).booleanValue());
-        if (o instanceof Number) return Script.N((Number)o);
+        if (o instanceof String) return JSU.S((String)o);
+        if (o instanceof Boolean) return JSU.B(((Boolean)o).booleanValue());
+        if (o instanceof Number) return JSU.N((Number)o);
         if (o instanceof JS) return (JS)o;
-        if (o instanceof Object[]) {
-            // FIXME: get element type here
+        if (o instanceof Object[]) throw new JSExn("Reflection onto Object[] not supported yet");
+        return new Wrapper(o);
+    }
+
+    public static class Wrapper extends JS.Immutable {
+        private final Object o;
+        public Wrapper(Object o) { this.o = o; }
+        public Object unwrap() { return o; }
+        public Enumeration keys() throws JSExn { throw new JSExn("JSReflection.keys() not supported yet"); }
+        public JS get(JS key) throws JSExn {
+            String k = JSU.toString(key);
+            Class c = o.getClass();
+            while(c != null) {
+                try {
+                    Field f = c.getField(k);
+                    if (f != null) return wrap(f.get(o));
+                } catch (NoSuchFieldException nfe) {
+                } catch (IllegalAccessException nfe) {
+                } catch (SecurityException nfe) { }
+                c = c.getSuperclass();
+            }
+            
+            try {
+                java.lang.reflect.Method[] methods = o.getClass().getMethods();
+                for(int i=0; i<methods.length; i++) if (methods[i].getName().equals(k)) return METHOD;
+            } catch (SecurityException nfe) { }
+            return null;
+        }
+        
+        public void put(JS key, JS val) throws JSExn { throw new JSExn("put() not supported yet"); }
+        
+        public JS call(JS method, JS[] args) throws JSExn {
+            String k = JSU.toString(method);
+            try {
+                java.lang.reflect.Method[] methods = o.getClass().getMethods();
+                for(int j=0; j<methods.length; j++) {
+                    if (methods[j].getName().equals(k) &&
+                        methods[j].getParameterTypes().length == args.length) {
+                        return wrap(methods[j].invoke(o, (Object[])args));
+                    }
+                }
+            } catch (IllegalAccessException nfe) {
+            } catch (InvocationTargetException it) {
+                Throwable ite = it.getTargetException();
+                if (ite instanceof JSExn) throw ((JSExn)ite);
+                JSU.warn(ite);
+                throw new JSExn("unhandled reflected exception: " + ite.toString());
+            } catch (SecurityException nfe) { }
+            throw new JSExn("called a reflection method with the wrong number of arguments");
         }
-        throw new JSExn("Reflection object tried to return a " + o.getClass().getName());
     }
 
     public static class Array extends JS.Immutable {
@@ -30,23 +76,26 @@ public class JSReflection extends JS.Immutable {
             return new Enumeration(null) {
                 private int n = 0;
                 public boolean _hasNext() { return n < arr.length; }
-                public JS _next() { return Script.N(n++); }
+                public JS _next() { return JSU.N(n++); }
             };
         }
-        public JS get(JS key) throws JSExn { return wrap(arr[Script.toInt(key)]); }
+        public JS get(JS key) throws JSExn { return wrap(arr[JSU.toInt(key)]); }
     }
 
-    // FIXME public static class Hash { }
-    // FIXME public Enumeration keys() throws JSExn {  }
+    public Enumeration keys() throws JSExn { throw new JSExn("JSReflection.keys() not supported yet"); }
 
     public JS get(JS key) throws JSExn {
-        String k = Script.toString(key);
-        try {
-            Field f = this.getClass().getField(k);
-            return wrap(f.get(this));
-        } catch (NoSuchFieldException nfe) {
-        } catch (IllegalAccessException nfe) {
-        } catch (SecurityException nfe) { }
+        String k = JSU.toString(key);
+        Class c = this.getClass();
+        while(c != null) {
+            try {
+                Field f = c.getField(k);
+                if (f != null) return wrap(f.get(this));
+            } catch (NoSuchFieldException nfe) {
+            } catch (IllegalAccessException nfe) {
+            } catch (SecurityException nfe) { }
+            c = c.getSuperclass();
+        }
 
         try {
             java.lang.reflect.Method[] methods = this.getClass().getMethods();
@@ -60,7 +109,7 @@ public class JSReflection extends JS.Immutable {
     }
 
     public JS call(JS method, JS[] args) throws JSExn {
-        String k = Script.toString(method);
+        String k = JSU.toString(method);
         try {
             java.lang.reflect.Method[] methods = this.getClass().getMethods();
             for(int j=0; j<methods.length; j++) {
@@ -73,7 +122,7 @@ public class JSReflection extends JS.Immutable {
         } catch (InvocationTargetException it) {
             Throwable ite = it.getTargetException();
             if (ite instanceof JSExn) throw ((JSExn)ite);
-            Script.warn(ite);
+            JSU.warn(ite);
             throw new JSExn("unhandled reflected exception: " + ite.toString());
         } catch (SecurityException nfe) { }
         throw new JSExn("called a reflection method with the wrong number of arguments");