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