comparison src/luan/lib/JavaLib.java @ 62:7c05f53ef6cb

improve java.proxy git-svn-id: https://luan-java.googlecode.com/svn/trunk@63 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 08 Jan 2013 04:29:24 +0000
parents 183d470be7e8
children ebe578282363
comparison
equal deleted inserted replaced
61:183d470be7e8 62:7c05f53ef6cb
33 LuanTable global = luan.global(); 33 LuanTable global = luan.global();
34 global.put("java",module); 34 global.put("java",module);
35 try { 35 try {
36 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) );
37 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 ); 38 add( module, "proxy", LuanState.class, Static.class, LuanTable.class, Object.class );
39 } catch(NoSuchMethodException e) { 39 } catch(NoSuchMethodException e) {
40 throw new RuntimeException(e); 40 throw new RuntimeException(e);
41 } 41 }
42 } 42 }
43 43
342 throw new RuntimeException(e); 342 throw new RuntimeException(e);
343 } 343 }
344 } 344 }
345 345
346 346
347 public static Object proxy(final LuanState luan,Static st,final LuanTable t) throws LuanException { 347 public static Object proxy(final LuanState luan,Static st,final LuanTable t,final Object base) throws LuanException {
348 return Proxy.newProxyInstance( 348 return Proxy.newProxyInstance(
349 st.cls.getClassLoader(), 349 st.cls.getClassLoader(),
350 new Class[]{st.cls}, 350 new Class[]{st.cls},
351 new InvocationHandler() { 351 new InvocationHandler() {
352 public Object invoke(Object proxy,Method method, Object[] args) 352 public Object invoke(Object proxy,Method method, Object[] args)
353 throws Throwable 353 throws Throwable
354 { 354 {
355 String name = method.getName(); 355 String name = method.getName();
356 LuanFunction fn = luan.checkFunction(LuanElement.JAVA,t.get(name)); 356 Object fnObj = t.get(name);
357 if( fnObj==null && base!=null )
358 return method.invoke(base,args);
359 LuanFunction fn = luan.checkFunction(LuanElement.JAVA,fnObj);
357 return Luan.first(luan.call(fn,LuanElement.JAVA,name,args)); 360 return Luan.first(luan.call(fn,LuanElement.JAVA,name,args));
358 } 361 }
359 } 362 }
360 ); 363 );
361 } 364 }