From: adam Date: Sun, 2 May 2004 08:10:40 +0000 (+0000) Subject: added org.ibex.js.JSReflection X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=commitdiff_plain;h=613e2ed0a735981d0280a4338b969b9e557e82cc added org.ibex.js.JSReflection darcs-hash:20040502081040-5007d-9673391116d491a39b6e40eb388f4878f0c5640b.gz --- diff --git a/src/org/ibex/js/JSReflection.java b/src/org/ibex/js/JSReflection.java new file mode 100644 index 0000000..070a35b --- /dev/null +++ b/src/org/ibex/js/JSReflection.java @@ -0,0 +1,79 @@ +// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] +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 static Object wrap(Object o) throws JSExn { + 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 + } + throw new JSExn("Reflection object tried to return a " + o.getClass().getName()); + } + + public static class Array extends JS { + final Object[] arr; + public Array(Object[] arr) { this.arr = arr; } + public Enumeration keys() throws JSExn { return new CounterEnumeration(arr.length); } + public Object get(Object key) throws JSExn { return wrap(arr[toInt(key)]); } + public void put(Object key, Object val) throws JSExn { throw new JSExn("can't write to org.ibex.js.Reflection.Array's"); } + } + + // FIXME public static class Hash { } + // FIXME public Enumeration keys() throws JSExn { } + + public Object get(Object 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) { } + + try { + Method[] methods = this.getClass().getMethods(); + for(int i=0; i