Changeset 11858
- Timestamp:
- 05/13/09 18:52:52 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/abcl/src/org/armedbear/lisp/JProxy.java
r11590 r11858 143 143 private static final Map<Object, LispObject> proxyMap = new WeakHashMap<Object, LispObject>(); 144 144 145 public static class LispInvocationHandler implements InvocationHandler { 145 public static class LispInvocationHandler implements InvocationHandler { 146 147 private Function function; 148 private static Method hashCodeMethod; 149 private static Method equalsMethod; 150 private static Method toStringMethod; 151 152 static { 153 try { 154 hashCodeMethod = Object.class.getMethod("hashCode", new Class[] {}); 155 equalsMethod = Object.class.getMethod("equals", new Class[] { Object.class }); 156 toStringMethod = Object.class.getMethod("toString", new Class[] {}); 157 } catch (Exception e) { 158 throw new Error("Something got horribly wrong - can't get a method from Object.class", e); 159 } 160 } 161 162 public LispInvocationHandler(Function function) { 163 this.function = function; 164 } 146 165 147 private Function function; 148 private static Method hashCodeMethod; 149 private static Method equalsMethod; 150 private static Method toStringMethod; 151 152 static { 153 try { 154 hashCodeMethod = Object.class.getMethod("hashCode", new Class[] {}); 155 equalsMethod = Object.class.getMethod("equals", new Class[] { Object.class }); 156 toStringMethod = Object.class.getMethod("toString", new Class[] {}); 157 } catch (Exception e) { 158 throw new Error("Something got horribly wrong - can't get a method from Object.class", e); 159 } 160 } 161 162 public LispInvocationHandler(Function function) { 163 this.function = function; 164 } 165 166 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 167 if(hashCodeMethod.equals(method)) { 168 return System.identityHashCode(proxy); 169 } 170 if(equalsMethod.equals(method)) { 171 return proxy == args[0]; 172 } 173 if(toStringMethod.equals(method)) { 174 return proxy.getClass().getName() + '@' + Integer.toHexString(proxy.hashCode()); 175 } 166 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 167 if(hashCodeMethod.equals(method)) { 168 return System.identityHashCode(proxy); 169 } 170 if(equalsMethod.equals(method)) { 171 return proxy == args[0]; 172 } 173 if(toStringMethod.equals(method)) { 174 return proxy.getClass().getName() + '@' + Integer.toHexString(proxy.hashCode()); 175 } 176 176 177 if(args == null) { 178 args = new Object[0]; 179 } 180 LispObject[] lispArgs = new LispObject[args.length + 2]; 181 synchronized(proxyMap) { 182 lispArgs[0] = toLispObject(proxyMap.get(proxy)); 183 } 184 lispArgs[1] = new SimpleString(method.getName()); 185 for(int i = 0; i < args.length; i++) { 186 lispArgs[i + 2] = toLispObject(args[i]); 187 } 188 Object retVal = (function.execute(lispArgs)).javaInstance(); 189 /* DOES NOT WORK due to autoboxing! 190 if(retVal != null && !method.getReturnType().isAssignableFrom(retVal.getClass())) { 191 return error(new TypeError(new JavaObject(retVal), new JavaObject(method.getReturnType()))); 192 }*/ 193 return retVal; 194 } 195 } 177 if(args == null) { 178 args = new Object[0]; 179 } 180 LispObject[] lispArgs = new LispObject[args.length + 3]; 181 lispArgs[0] = function; 182 synchronized(proxyMap) { 183 lispArgs[1] = toLispObject(proxyMap.get(proxy)); 184 } 185 lispArgs[2] = new SimpleString(method.getName()); 186 for(int i = 0; i < args.length; i++) { 187 lispArgs[i + 3] = toLispObject(args[i]); 188 } 189 Object retVal = 190 LispThread.currentThread().execute(Symbol.FUNCALL, lispArgs).javaInstance(); 191 //(function.execute(lispArgs)).javaInstance(); 192 /* DOES NOT WORK due to autoboxing! 193 if(retVal != null && !method.getReturnType().isAssignableFrom(retVal.getClass())) { 194 return error(new TypeError(new JavaObject(retVal), new JavaObject(method.getReturnType()))); 195 }*/ 196 return retVal; 197 } 198 } 196 199 197 200 private static final Primitive _JMAKE_INVOCATION_HANDLER =
Note: See TracChangeset
for help on using the changeset viewer.