added JSReflection.Wrap
[org.ibex.js.git] / src / org / ibex / js / JSReflection.java
index a1db9a8..d9c241f 100644 (file)
 
 package org.ibex.js; 
 
-import org.ibex.util.*; 
-import java.io.*;
-import java.util.*;
 import java.lang.reflect.*;
 
 /** Automatic JS-ification via Reflection (not for use in the core) */
-public class JSReflection extends JS {
+public class JSReflection extends JS.Immutable {
+    private static final JS.Method METHOD = new JS.Method();
 
     public static JS wrap(Object o) throws JSExn {
         if (o == null) return null;
-        if (o instanceof String) return JS.S((String)o);
-        if (o instanceof Boolean) return JS.B(((Boolean)o).booleanValue());
-        if (o instanceof Number) return JS.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 {
+    public static class Array extends JS.Immutable {
         final Object[] arr;
         public Array(Object[] arr) { this.arr = arr; }
         // FEATURE: Add a JSCounterEnumeration
         public Enumeration keys() throws JSExn {
             return new Enumeration(null) {
                 private int n = 0;
-                public boolean _hasMoreElements() { return n < arr.length; }
-                public JS _nextElement() {
-                    return n >= arr.length ? null : JS.N(n++);
-                }
+                public boolean _hasNext() { return n < arr.length; }
+                public JS _next() { return JSU.N(n++); }
             };
         }
-        public JS get(JS key) throws JSExn { return wrap(arr[toInt(key)]); }
-        public void put(JS key, JS val) throws JSExn { throw new JSExn("can't write to org.ibex.js.Reflection.Array's"); }
+        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 = 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 {
-            Method[] methods = this.getClass().getMethods();
+            java.lang.reflect.Method[] methods = this.getClass().getMethods();
             for(int i=0; i<methods.length; i++) if (methods[i].getName().equals(k)) return METHOD;
         } catch (SecurityException nfe) { }
         return null;
@@ -64,27 +108,21 @@ public class JSReflection extends JS {
         throw new JSExn("put() not supported yet");
     }
 
-    public JS callMethod(JS method, JS a0, JS a1, JS a2, JS[] rest, int nargs) throws JSExn {
-        String k = toString(method);
+    public JS call(JS method, JS[] args) throws JSExn {
+        String k = JSU.toString(method);
         try {
-            Method[] methods = this.getClass().getMethods();
+            java.lang.reflect.Method[] methods = this.getClass().getMethods();
             for(int j=0; j<methods.length; j++) {
-                if (methods[j].getName().equals(k) && methods[j].getParameterTypes().length == nargs) {
-                    Object[] args = new Object[nargs];
-                    for(int i = 0; i<args.length; i++) {
-                        if (i==0) args[i] = a0;
-                        else if (i==1) args[i] = a1;
-                        else if (i==2) args[i] = a2;
-                        else args[i] = rest[i-3];
-                    }
-                    return wrap(methods[j].invoke(this, args));
+                if (methods[j].getName().equals(k) &&
+                    methods[j].getParameterTypes().length == args.length) {
+                    return wrap(methods[j].invoke(this, (Object[])args));
                 }
             }
         } catch (IllegalAccessException nfe) {
         } catch (InvocationTargetException it) {
             Throwable ite = it.getTargetException();
             if (ite instanceof JSExn) throw ((JSExn)ite);
-            JS.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");