changeset 61:183d470be7e8

add java.proxy git-svn-id: https://luan-java.googlecode.com/svn/trunk@62 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 08 Jan 2013 03:06:23 +0000
parents a68ccb7aaa9c
children 7c05f53ef6cb
files src/luan/lib/JavaLib.java
diffstat 1 files changed, 19 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/lib/JavaLib.java	Mon Jan 07 05:41:59 2013 +0000
+++ b/src/luan/lib/JavaLib.java	Tue Jan 08 03:06:23 2013 +0000
@@ -7,6 +7,8 @@
 import java.lang.reflect.Method;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Modifier;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Proxy;
 import java.util.Map;
 import java.util.HashMap;
 import java.util.List;
@@ -33,6 +35,7 @@
 		try {
 			global.put( "import", new LuanJavaFunction(JavaLib.class.getMethod("importClass",LuanState.class,String.class),null) );
 			module.put( "class", new LuanJavaFunction(JavaLib.class.getMethod("getClass",LuanState.class,String.class),null) );
+			add( module, "proxy", LuanState.class, Static.class, LuanTable.class );
 		} catch(NoSuchMethodException e) {
 			throw new RuntimeException(e);
 		}
@@ -340,4 +343,20 @@
 		}
 	}
 
+
+	public static Object proxy(final LuanState luan,Static st,final LuanTable t) throws LuanException {
+		return Proxy.newProxyInstance(
+			st.cls.getClassLoader(),
+			new Class[]{st.cls},
+			new InvocationHandler() {
+				public Object invoke(Object proxy,Method method, Object[] args)
+					throws Throwable
+				{
+					String name = method.getName();
+					LuanFunction fn = luan.checkFunction(LuanElement.JAVA,t.get(name));
+					return Luan.first(luan.call(fn,LuanElement.JAVA,name,args));
+				}
+			}
+		);
+	}
 }