diff options
author | Rich Hickey <richhickey@gmail.com> | 2007-10-07 12:48:26 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2007-10-07 12:48:26 +0000 |
commit | 74a359c2300d218691100a17dc34176010355164 (patch) | |
tree | 2e769ae137eb78efa3c280d0425965723c925f8b /src/jvm/clojure/lang/ProxyHandler.java | |
parent | 23d0289148fa1755ec17cede2e200b32d4f33351 (diff) |
initial proxy support, simple print
Diffstat (limited to 'src/jvm/clojure/lang/ProxyHandler.java')
-rw-r--r-- | src/jvm/clojure/lang/ProxyHandler.java | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/jvm/clojure/lang/ProxyHandler.java b/src/jvm/clojure/lang/ProxyHandler.java new file mode 100644 index 00000000..883b19a9 --- /dev/null +++ b/src/jvm/clojure/lang/ProxyHandler.java @@ -0,0 +1,56 @@ +/** + * Copyright (c) Rich Hickey. All rights reserved. + * The use and distribution terms for this software are covered by the + * Common Public License 1.0 (http://opensource.org/licenses/cpl.php) + * which can be found in the file CPL.TXT at the root of this distribution. + * By using this software in any fashion, you are agreeing to be bound by + * the terms of this license. + * You must not remove this notice, or any other, from this software. + **/ + +/* rich Oct 4, 2007 */ + +package clojure.lang; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; + +public class ProxyHandler implements InvocationHandler{ +//method-name-string->fn +final IPersistentMap fns; + + +public ProxyHandler(IPersistentMap fns){ + this.fns = fns; +} + +public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{ + IFn fn = (IFn) fns.valAt(method.getName()); + if(fn == null) + throw new UnsupportedOperationException(); + Object ret = fn.applyTo(ArraySeq.create(args)); + Class rt = method.getReturnType(); + if(rt == Void.TYPE) + return null; + else if(rt.isPrimitive()) + { + if(rt == Character.TYPE) + return ret; + else if(rt == Integer.TYPE) + return ((Number)ret).intValue(); + else if(rt == Long.TYPE) + return ((Number)ret).longValue(); + else if(rt == Float.TYPE) + return ((Number)ret).floatValue(); + else if(rt == Double.TYPE) + return ((Number)ret).doubleValue(); + else if(rt == Boolean.TYPE) + return ret == null?Boolean.FALSE:Boolean.TRUE; + else if(rt == Byte.TYPE) + return (byte)((Number)ret).intValue(); + else if(rt == Short.TYPE) + return (short)((Number)ret).intValue(); + } + return ret; +} +} |