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