comparison src/luan/LuanJavaFunction.java @ 55:9381b23ea9e1

various fixes git-svn-id: https://luan-java.googlecode.com/svn/trunk@56 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 04 Jan 2013 04:50:18 +0000
parents 8ede219cd111
children c84274b18f0c
comparison
equal deleted inserted replaced
54:f5b27ef14d73 55:9381b23ea9e1
5 import java.lang.reflect.Constructor; 5 import java.lang.reflect.Constructor;
6 import java.lang.reflect.InvocationTargetException; 6 import java.lang.reflect.InvocationTargetException;
7 import java.util.List; 7 import java.util.List;
8 import java.util.Map; 8 import java.util.Map;
9 import java.util.Set; 9 import java.util.Set;
10 import java.util.Arrays;
10 11
11 12
12 public final class LuanJavaFunction extends LuanFunction { 13 public final class LuanJavaFunction extends LuanFunction {
13 private final JavaMethod method; 14 private final JavaMethod method;
14 private final Object obj; 15 private final Object obj;
42 public Class<?>[] getParameterTypes() { 43 public Class<?>[] getParameterTypes() {
43 return method.getParameterTypes(); 44 return method.getParameterTypes();
44 } 45 }
45 46
46 @Override public Object[] call(LuanState luan,Object[] args) throws LuanException { 47 @Override public Object[] call(LuanState luan,Object[] args) throws LuanException {
48 try {
49 return rawCall(luan,args);
50 } catch(IllegalArgumentException e) {
51 checkArgs(luan,args);
52 throw e;
53 }
54 }
55
56 public Object[] rawCall(LuanState luan,Object[] args) throws LuanException {
47 args = fixArgs(luan,args); 57 args = fixArgs(luan,args);
48 Object rtn; 58 Object rtn;
49 try { 59 try {
50 rtn = method.invoke(obj,args); 60 rtn = method.invoke(obj,args);
51 } catch(IllegalArgumentException e) {
52 checkArgs(luan,args);
53 throw e;
54 } catch(IllegalAccessException e) { 61 } catch(IllegalAccessException e) {
55 throw new RuntimeException(e); 62 throw new RuntimeException(e);
56 } catch(InvocationTargetException e) { 63 } catch(InvocationTargetException e) {
57 Throwable cause = e.getCause(); 64 Throwable cause = e.getCause();
58 if( cause instanceof Error ) 65 if( cause instanceof Error )
321 } 328 }
322 if( obj instanceof Set ) { 329 if( obj instanceof Set ) {
323 @SuppressWarnings("unchecked") 330 @SuppressWarnings("unchecked")
324 Set<Object> set = (Set<Object>)obj; 331 Set<Object> set = (Set<Object>)obj;
325 return new LuanTable(set); 332 return new LuanTable(set);
333 }
334 Class cls = obj.getClass();
335 if( cls.isArray() && !cls.getComponentType().isPrimitive() ) {
336 Object[] a = (Object[])obj;
337 return new LuanTable(Arrays.asList(a));
326 } 338 }
327 return obj; 339 return obj;
328 } 340 }
329 }; 341 };
330 342