X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FJSReflection.java;h=d9c241f628161c1701f3fa9f411e2c7d5e63858d;hb=1da9851e5aa0c1aa6063847a4a7f3f874266af3a;hp=69fb9b37b55846468f80bf47a87c48432ed61bc8;hpb=b1fa73c17b31f268fca5695d0876d7314fbacce3;p=org.ibex.js.git diff --git a/src/org/ibex/js/JSReflection.java b/src/org/ibex/js/JSReflection.java index 69fb9b3..d9c241f 100644 --- a/src/org/ibex/js/JSReflection.java +++ b/src/org/ibex/js/JSReflection.java @@ -1,80 +1,130 @@ -// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] +// Copyright 2000-2005 the Contributors, as shown in the revision logs. +// Licensed under the Apache Public Source License 2.0 ("the License"). +// You may not use this file except in compliance with the License. + 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 Object wrap(Object o) throws JSExn { + public static JS wrap(Object o) throws JSExn { if (o == null) return null; - if (o instanceof String) return o; - if (o instanceof Boolean) return o; - if (o instanceof Number) return o; - if (o instanceof JS) return o; - if (o instanceof Object[]) { - // FIXME: get element type here + 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[]) 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