comparison src/luan/LuanJavaFunction.java @ 68:877288464542

better type checking git-svn-id: https://luan-java.googlecode.com/svn/trunk@69 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 23 Jan 2013 02:24:41 +0000
parents 8ca58ab6919b
children aeedefd3e3f3
comparison
equal deleted inserted replaced
67:8ca58ab6919b 68:877288464542
47 public Class<?>[] getParameterTypes() { 47 public Class<?>[] getParameterTypes() {
48 return method.getParameterTypes(); 48 return method.getParameterTypes();
49 } 49 }
50 50
51 @Override public Object[] call(LuanState luan,Object[] args) throws LuanException { 51 @Override public Object[] call(LuanState luan,Object[] args) throws LuanException {
52 args = fixArgs(luan,args);
52 try { 53 try {
53 return rawCall(luan,args); 54 return doCall(luan,args);
54 } catch(IllegalArgumentException e) { 55 } catch(IllegalArgumentException e) {
55 checkArgs(luan,args); 56 checkArgs(luan,args);
56 throw e; 57 throw e;
57 } 58 }
58 } 59 }
59 60
60 public Object[] rawCall(LuanState luan,Object[] args) throws LuanException { 61 public Object[] rawCall(LuanState luan,Object[] args) throws LuanException {
61 args = fixArgs(luan,args); 62 args = fixArgs(luan,args);
63 return doCall(luan,args);
64 }
65
66 private Object[] doCall(LuanState luan,Object[] args) throws LuanException {
62 Object rtn; 67 Object rtn;
63 try { 68 try {
64 rtn = method.invoke(obj,args); 69 rtn = method.invoke(obj,args);
65 } catch(IllegalAccessException e) { 70 } catch(IllegalAccessException e) {
66 throw new RuntimeException(e); 71 throw new RuntimeException(e);
79 return rtnConverter.convert(rtn); 84 return rtnConverter.convert(rtn);
80 } 85 }
81 86
82 private void checkArgs(LuanState luan,Object[] args) throws LuanException { 87 private void checkArgs(LuanState luan,Object[] args) throws LuanException {
83 Class<?>[] a = getParameterTypes(); 88 Class<?>[] a = getParameterTypes();
84 if( takesLuaState ) {
85 Class<?>[] t = new Class<?>[a.length-1];
86 System.arraycopy(a,1,t,0,t.length);
87 a = t;
88 }
89 for( int i=0; i<a.length; i++ ) { 89 for( int i=0; i<a.length; i++ ) {
90 Class<?> paramType = a[i]; 90 Class<?> paramType = a[i];
91 Object arg = args[i]; 91 Object arg = args[i];
92 if( !paramType.isInstance(arg) ) { 92 if( !paramType.isInstance(arg) ) {
93 String expected = paramType.getSimpleName(); 93 String expected = paramType.getSimpleName();