comparison src/luan/lib/JavaLib.java @ 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
comparison
equal deleted inserted replaced
60:a68ccb7aaa9c 61:183d470be7e8
5 import java.lang.reflect.Member; 5 import java.lang.reflect.Member;
6 import java.lang.reflect.Field; 6 import java.lang.reflect.Field;
7 import java.lang.reflect.Method; 7 import java.lang.reflect.Method;
8 import java.lang.reflect.Constructor; 8 import java.lang.reflect.Constructor;
9 import java.lang.reflect.Modifier; 9 import java.lang.reflect.Modifier;
10 import java.lang.reflect.InvocationHandler;
11 import java.lang.reflect.Proxy;
10 import java.util.Map; 12 import java.util.Map;
11 import java.util.HashMap; 13 import java.util.HashMap;
12 import java.util.List; 14 import java.util.List;
13 import java.util.ArrayList; 15 import java.util.ArrayList;
14 import java.util.Iterator; 16 import java.util.Iterator;
31 LuanTable global = luan.global(); 33 LuanTable global = luan.global();
32 global.put("java",module); 34 global.put("java",module);
33 try { 35 try {
34 global.put( "import", new LuanJavaFunction(JavaLib.class.getMethod("importClass",LuanState.class,String.class),null) ); 36 global.put( "import", new LuanJavaFunction(JavaLib.class.getMethod("importClass",LuanState.class,String.class),null) );
35 module.put( "class", new LuanJavaFunction(JavaLib.class.getMethod("getClass",LuanState.class,String.class),null) ); 37 module.put( "class", new LuanJavaFunction(JavaLib.class.getMethod("getClass",LuanState.class,String.class),null) );
38 add( module, "proxy", LuanState.class, Static.class, LuanTable.class );
36 } catch(NoSuchMethodException e) { 39 } catch(NoSuchMethodException e) {
37 throw new RuntimeException(e); 40 throw new RuntimeException(e);
38 } 41 }
39 } 42 }
40 43
338 } catch(NoSuchMethodException e) { 341 } catch(NoSuchMethodException e) {
339 throw new RuntimeException(e); 342 throw new RuntimeException(e);
340 } 343 }
341 } 344 }
342 345
346
347 public static Object proxy(final LuanState luan,Static st,final LuanTable t) throws LuanException {
348 return Proxy.newProxyInstance(
349 st.cls.getClassLoader(),
350 new Class[]{st.cls},
351 new InvocationHandler() {
352 public Object invoke(Object proxy,Method method, Object[] args)
353 throws Throwable
354 {
355 String name = method.getName();
356 LuanFunction fn = luan.checkFunction(LuanElement.JAVA,t.get(name));
357 return Luan.first(luan.call(fn,LuanElement.JAVA,name,args));
358 }
359 }
360 );
361 }
343 } 362 }