69fb9b37b55846468f80bf47a87c48432ed61bc8
[org.ibex.core.git] / src / org / ibex / js / JSReflection.java
1 // Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] 
2 package org.ibex.js; 
3
4 import org.ibex.util.*; 
5 import java.io.*;
6 import java.util.*;
7 import java.lang.reflect.*;
8
9 /** Automatic JS-ification via Reflection (not for use in the core) */
10 public class JSReflection extends JS {
11
12     public static Object wrap(Object o) throws JSExn {
13         if (o == null) return null;
14         if (o instanceof String) return o;
15         if (o instanceof Boolean) return o;
16         if (o instanceof Number) return o;
17         if (o instanceof JS) return o;
18         if (o instanceof Object[]) {
19             // FIXME: get element type here
20         }
21         throw new JSExn("Reflection object tried to return a " + o.getClass().getName());
22     }
23
24     public static class Array extends JS {
25         final Object[] arr;
26         public Array(Object[] arr) { this.arr = arr; }
27         public Enumeration keys() throws JSExn { return new CounterEnumeration(arr.length); }
28         public Object get(Object key) throws JSExn { return wrap(arr[toInt(key)]); }
29         public void put(Object key, Object val) throws JSExn { throw new JSExn("can't write to org.ibex.js.Reflection.Array's"); }
30     }
31
32     // FIXME public static class Hash { }
33     // FIXME public Enumeration keys() throws JSExn {  }
34
35     public Object get(Object key) throws JSExn {
36         String k = toString(key);
37         try {
38             Field f = this.getClass().getField(k);
39             return wrap(f.get(this));
40         } catch (NoSuchFieldException nfe) {
41         } catch (IllegalAccessException nfe) {
42         } catch (SecurityException nfe) { }
43
44         try {
45             Method[] methods = this.getClass().getMethods();
46             for(int i=0; i<methods.length; i++) if (methods[i].getName().equals(k)) return METHOD;
47         } catch (SecurityException nfe) { }
48         return null;
49     }
50
51     public void put(Object key, Object val) throws JSExn {
52         throw new JSExn("put() not supported yet");
53     }
54
55     public Object callMethod(Object method, Object a0, Object a1, Object a2, Object[] rest, int nargs) throws JSExn {
56         String k = toString(method);
57         try {
58             Method[] methods = this.getClass().getMethods();
59             for(int j=0; j<methods.length; j++) {
60                 if (methods[j].getName().equals(k) && methods[j].getParameterTypes().length == nargs) {
61                     Object[] args = new Object[nargs];
62                     for(int i = 0; i<args.length; i++) {
63                         if (i==0) args[i] = a0;
64                         else if (i==1) args[i] = a1;
65                         else if (i==2) args[i] = a2;
66                         else args[i] = rest[i-3];
67                     }
68                     return wrap(methods[j].invoke(this, args));
69                 }
70             }
71         } catch (IllegalAccessException nfe) {
72         } catch (InvocationTargetException it) {
73             Throwable ite = it.getTargetException();
74             if (ite instanceof JSExn) throw ((JSExn)ite);
75             JS.warn(ite);
76             throw new JSExn("unhandled reflected exception: " + ite.toString());
77         } catch (SecurityException nfe) { }
78         throw new JSExn("called a reflection method with the wrong number of arguments");
79     }    
80