comparison core/src/luan/LuanJavaFunction.java @ 646:cdc70de628b5

simplify LuanException
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 29 Mar 2016 19:58:39 -0600
parents 60c549d43988
children
comparison
equal deleted inserted replaced
645:859c0dedc8b6 646:cdc70de628b5
68 } 68 }
69 69
70 @Override public Object call(LuanState luan,Object[] args) throws LuanException { 70 @Override public Object call(LuanState luan,Object[] args) throws LuanException {
71 try { 71 try {
72 args = fixArgs(luan,args); 72 args = fixArgs(luan,args);
73 return doCall(luan,args); 73 return doCall(args);
74 } catch(IllegalArgumentException e) { 74 } catch(IllegalArgumentException e) {
75 checkArgs(luan,args); 75 checkArgs(args);
76 throw e; 76 throw e;
77 } 77 }
78 } 78 }
79 79
80 public Object rawCall(LuanState luan,Object[] args) throws LuanException { 80 public Object rawCall(LuanState luan,Object[] args) throws LuanException {
81 args = fixArgs(luan,args); 81 args = fixArgs(luan,args);
82 return doCall(luan,args); 82 return doCall(args);
83 } 83 }
84 84
85 private Object doCall(LuanState luan,Object[] args) throws LuanException { 85 private Object doCall(Object[] args) throws LuanException {
86 Object rtn; 86 Object rtn;
87 try { 87 try {
88 rtn = method.invoke(obj,args); 88 rtn = method.invoke(obj,args);
89 } catch(IllegalAccessException e) { 89 } catch(IllegalAccessException e) {
90 throw new RuntimeException("method = "+method,e); 90 throw new RuntimeException("method = "+method,e);
92 Throwable cause = e.getCause(); 92 Throwable cause = e.getCause();
93 if( cause instanceof Error ) 93 if( cause instanceof Error )
94 throw (Error)cause; 94 throw (Error)cause;
95 if( cause instanceof LuanException ) 95 if( cause instanceof LuanException )
96 throw (LuanException)cause; 96 throw (LuanException)cause;
97 throw new LuanException(luan,cause); 97 throw new LuanException(cause);
98 } catch(InstantiationException e) { 98 } catch(InstantiationException e) {
99 throw new RuntimeException(e); 99 throw new RuntimeException(e);
100 } 100 }
101 return rtnConverter.convert(rtn); 101 return rtnConverter.convert(rtn);
102 } 102 }
112 primitiveMap.put(Float.TYPE,Float.class); 112 primitiveMap.put(Float.TYPE,Float.class);
113 primitiveMap.put(Double.TYPE,Double.class); 113 primitiveMap.put(Double.TYPE,Double.class);
114 primitiveMap.put(Void.TYPE,Void.class); 114 primitiveMap.put(Void.TYPE,Void.class);
115 } 115 }
116 116
117 private void checkArgs(LuanState luan,Object[] args) throws LuanException { 117 private void checkArgs(Object[] args) throws LuanException {
118 Class[] a = getParameterTypes(); 118 Class[] a = getParameterTypes();
119 int start = takesLuaState ? 1 : 0; 119 int start = takesLuaState ? 1 : 0;
120 for( int i=start; i<a.length; i++ ) { 120 for( int i=start; i<a.length; i++ ) {
121 Class paramType = a[i]; 121 Class paramType = a[i];
122 Class type = paramType; 122 Class type = paramType;
129 expected = fixType(paramType.getComponentType().getSimpleName())+"..."; 129 expected = fixType(paramType.getComponentType().getSimpleName())+"...";
130 else 130 else
131 expected = fixType(paramType.getSimpleName()); 131 expected = fixType(paramType.getSimpleName());
132 if( arg==null ) { 132 if( arg==null ) {
133 if( paramType.isPrimitive() ) 133 if( paramType.isPrimitive() )
134 throw new LuanException(luan,"bad argument #"+(i+1-start)+" ("+expected+" expected, got nil)"); 134 throw new LuanException("bad argument #"+(i+1-start)+" ("+expected+" expected, got nil)");
135 } else { 135 } else {
136 String got = fixType(arg.getClass().getSimpleName()); 136 String got = fixType(arg.getClass().getSimpleName());
137 throw new LuanException(luan,"bad argument #"+(i+1-start)+" ("+expected+" expected, got "+got+")"); 137 throw new LuanException("bad argument #"+(i+1-start)+" ("+expected+" expected, got "+got+")");
138 } 138 }
139 } 139 }
140 } 140 }
141 } 141 }
142 142