Mercurial > lasercutter
diff src/clojure/lang/ProxyHandler.java @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/clojure/lang/ProxyHandler.java Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,72 @@ 1.4 +/** 1.5 + * Copyright (c) Rich Hickey. All rights reserved. 1.6 + * The use and distribution terms for this software are covered by the 1.7 + * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 1.8 + * which can be found in the file epl-v10.html at the root of this distribution. 1.9 + * By using this software in any fashion, you are agreeing to be bound by 1.10 + * the terms of this license. 1.11 + * You must not remove this notice, or any other, from this software. 1.12 + **/ 1.13 + 1.14 +/* rich Oct 4, 2007 */ 1.15 + 1.16 +package clojure.lang; 1.17 + 1.18 +import java.lang.reflect.InvocationHandler; 1.19 +import java.lang.reflect.Method; 1.20 + 1.21 +public class ProxyHandler implements InvocationHandler{ 1.22 +//method-name-string->fn 1.23 +final IPersistentMap fns; 1.24 + 1.25 + 1.26 +public ProxyHandler(IPersistentMap fns){ 1.27 + this.fns = fns; 1.28 +} 1.29 + 1.30 +public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{ 1.31 + Class rt = method.getReturnType(); 1.32 + IFn fn = (IFn) fns.valAt(method.getName()); 1.33 + if(fn == null) 1.34 + { 1.35 + if(rt == Void.TYPE) 1.36 + return null; 1.37 + else if(method.getName().equals("equals")) 1.38 + { 1.39 + return proxy == args[0]; 1.40 + } 1.41 + else if(method.getName().equals("hashCode")) 1.42 + { 1.43 + return System.identityHashCode(proxy); 1.44 + } 1.45 + else if(method.getName().equals("toString")) 1.46 + { 1.47 + return "Proxy: " + System.identityHashCode(proxy); 1.48 + } 1.49 + throw new UnsupportedOperationException(); 1.50 + } 1.51 + Object ret = fn.applyTo(ArraySeq.create(args)); 1.52 + if(rt == Void.TYPE) 1.53 + return null; 1.54 + else if(rt.isPrimitive()) 1.55 + { 1.56 + if(rt == Character.TYPE) 1.57 + return ret; 1.58 + else if(rt == Integer.TYPE) 1.59 + return ((Number) ret).intValue(); 1.60 + else if(rt == Long.TYPE) 1.61 + return ((Number) ret).longValue(); 1.62 + else if(rt == Float.TYPE) 1.63 + return ((Number) ret).floatValue(); 1.64 + else if(rt == Double.TYPE) 1.65 + return ((Number) ret).doubleValue(); 1.66 + else if(rt == Boolean.TYPE && !(ret instanceof Boolean)) 1.67 + return ret == null ? Boolean.FALSE : Boolean.TRUE; 1.68 + else if(rt == Byte.TYPE) 1.69 + return (byte) ((Number) ret).intValue(); 1.70 + else if(rt == Short.TYPE) 1.71 + return (short) ((Number) ret).intValue(); 1.72 + } 1.73 + return ret; 1.74 +} 1.75 +}