// 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 java.lang.reflect.*; /** Automatic JS-ification via Reflection (not for use in the core) */ 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 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