X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FJSReflection.java;h=d9c241f628161c1701f3fa9f411e2c7d5e63858d;hb=1da9851e5aa0c1aa6063847a4a7f3f874266af3a;hp=a1db9a821bf2835dfc09282317aa08d394d78fc5;hpb=73131826a18c93af4fb04672bc3ec820e1197ad1;p=org.ibex.js.git diff --git a/src/org/ibex/js/JSReflection.java b/src/org/ibex/js/JSReflection.java index a1db9a8..d9c241f 100644 --- a/src/org/ibex/js/JSReflection.java +++ b/src/org/ibex/js/JSReflection.java @@ -4,57 +4,101 @@ 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= 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