comparison core/src/luan/modules/JavaLuan.java @ 502:d3183a330ff5

improve the __index metamethod to work with any type; simplify luan_proxy to eliminate base;
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 19 May 2015 17:57:20 -0600
parents 34a4ed3597ea
children 92c3d22745b8
comparison
equal deleted inserted replaced
501:f26485a3692c 502:d3183a330ff5
305 305
306 @Override public boolean isSynthetic() { 306 @Override public boolean isSynthetic() {
307 return cls.isSynthetic(); 307 return cls.isSynthetic();
308 } 308 }
309 309
310 public Object luan_proxy(final LuanState luan,final LuanTable t,final Object base) throws LuanException { 310 public Object luan_proxy(final LuanState luan,final LuanTable t) throws LuanException {
311 return Proxy.newProxyInstance( 311 return Proxy.newProxyInstance(
312 cls.getClassLoader(), 312 cls.getClassLoader(),
313 new Class[]{cls}, 313 new Class[]{cls},
314 new InvocationHandler() { 314 new InvocationHandler() {
315 public Object invoke(Object proxy,Method method, Object[] args) 315 public Object invoke(Object proxy,Method method, Object[] args)
317 { 317 {
318 if( args==null ) 318 if( args==null )
319 args = new Object[0]; 319 args = new Object[0];
320 String name = method.getName(); 320 String name = method.getName();
321 Object fnObj = t.get(luan,name); 321 Object fnObj = t.get(luan,name);
322 if( fnObj==null && base!=null ) 322 if( fnObj == null )
323 return method.invoke(base,args); 323 throw new NullPointerException("luan_proxy couldn't find method '"+name+"'");
324 LuanFunction fn = luan.checkFunction(fnObj); 324 LuanFunction fn = luan.checkFunction(fnObj);
325 return Luan.first(luan.call(fn,name,args)); 325 return Luan.first(luan.call(fn,name,args));
326 } 326 }
327 } 327 }
328 ); 328 );
329 } 329 }
330 } 330 }
331 private static final Method luan_proxyMethod; 331 private static final Method luan_proxyMethod;
332 static { 332 static {
333 try { 333 try {
334 luan_proxyMethod = Static.class.getMethod("luan_proxy",LuanState.class,LuanTable.class,Object.class); 334 luan_proxyMethod = Static.class.getMethod("luan_proxy",LuanState.class,LuanTable.class);
335 luan_proxyMethod.setAccessible(true); 335 luan_proxyMethod.setAccessible(true);
336 } catch(NoSuchMethodException e) { 336 } catch(NoSuchMethodException e) {
337 throw new RuntimeException(e); 337 throw new RuntimeException(e);
338 } 338 }
339 } 339 }