X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fjs%2FJSReflection.java;h=4712d0829616ac93292b68dda0de78710cf42994;hb=f63da7aeac2f942be41aefde91002d14117a8573;hp=52f7f9cc954cbb9e2af6904d602add3890b9d0f0;hpb=19d66e161db458135518efd3539048f44e1e5622;p=org.ibex.js.git diff --git a/src/org/ibex/js/JSReflection.java b/src/org/ibex/js/JSReflection.java index 52f7f9c..4712d08 100644 --- a/src/org/ibex/js/JSReflection.java +++ b/src/org/ibex/js/JSReflection.java @@ -1,19 +1,20 @@ -// 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 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 @@ -21,37 +22,38 @@ public class JSReflection extends JS { 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 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