comparison core/src/luan/LuanJavaFunction.java @ 209:239c8d650028

convert returned arrays to tables for java methods git-svn-id: https://luan-java.googlecode.com/svn/trunk@210 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 08 Jul 2014 10:25:35 +0000
parents 4c96cb73dd93
children e84655b4c45e
comparison
equal deleted inserted replaced
208:5ba136769034 209:239c8d650028
3 import java.lang.reflect.Array; 3 import java.lang.reflect.Array;
4 import java.lang.reflect.Method; 4 import java.lang.reflect.Method;
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.ArrayList;
8 import java.util.Map; 9 import java.util.Map;
9 import java.util.HashMap; 10 import java.util.HashMap;
10 import java.util.Set; 11 import java.util.Set;
11 import java.util.Arrays; 12 import java.util.Arrays;
12 13
18 private final boolean takesLuaState; 19 private final boolean takesLuaState;
19 private final ArgConverter[] argConverters; 20 private final ArgConverter[] argConverters;
20 private final Class<?> varArgCls; 21 private final Class<?> varArgCls;
21 22
22 public LuanJavaFunction(Method method,Object obj) { 23 public LuanJavaFunction(Method method,Object obj) {
23 this( JavaMethod.of(method), obj ); 24 this(method,obj,false);
25 }
26
27 public LuanJavaFunction(Method method,Object obj,boolean convertArray) {
28 this( JavaMethod.of(method), obj, convertArray );
24 } 29 }
25 30
26 public LuanJavaFunction(Constructor constr,Object obj) { 31 public LuanJavaFunction(Constructor constr,Object obj) {
27 this( JavaMethod.of(constr), obj ); 32 this( JavaMethod.of(constr), obj, false );
28 } 33 }
29 34
30 LuanJavaFunction(JavaMethod method,Object obj) { 35 private LuanJavaFunction(JavaMethod method,Object obj,boolean convertArray) {
31 this.method = method; 36 this.method = method;
32 this.obj = obj; 37 this.obj = obj;
33 this.rtnConverter = getRtnConverter(method); 38 this.rtnConverter = getRtnConverter(method,convertArray);
34 this.takesLuaState = takesLuaState(method); 39 this.takesLuaState = takesLuaState(method);
35 this.argConverters = getArgConverters(takesLuaState,method); 40 this.argConverters = getArgConverters(takesLuaState,method);
36 if( method.isVarArgs() ) { 41 if( method.isVarArgs() ) {
37 Class<?>[] paramTypes = method.getParameterTypes(); 42 Class<?>[] paramTypes = method.getParameterTypes();
38 this.varArgCls = paramTypes[paramTypes.length-1].getComponentType(); 43 this.varArgCls = paramTypes[paramTypes.length-1].getComponentType();
185 @Override public Object convert(Object obj) { 190 @Override public Object convert(Object obj) {
186 return obj; 191 return obj;
187 } 192 }
188 }; 193 };
189 194
190 private static RtnConverter getRtnConverter(JavaMethod m) { 195 private static final RtnConverter RTN_ARRAY = new RtnConverter() {
196 @Override public Object convert(Object obj) {
197 if( obj == null )
198 return null;
199 Object[] a = new Object[Array.getLength(obj)];
200 for( int i=0; i<a.length; i++ ) {
201 a[i] = Array.get(obj,i);
202 }
203 return new LuanTable(new ArrayList<Object>(Arrays.asList(a)));
204 }
205 };
206
207 private static RtnConverter getRtnConverter(JavaMethod m,boolean convertArray) {
191 Class<?> rtnType = m.getReturnType(); 208 Class<?> rtnType = m.getReturnType();
192 if( rtnType == Void.TYPE ) 209 if( rtnType == Void.TYPE )
193 return RTN_NOTHING; 210 return RTN_NOTHING;
211 if( convertArray && rtnType.isArray() )
212 return RTN_ARRAY;
194 return RTN_SAME; 213 return RTN_SAME;
195 } 214 }
196 215
197 private static boolean isNumber(Class<?> rtnType) { 216 private static boolean isNumber(Class<?> rtnType) {
198 return rtnType == Short.TYPE 217 return rtnType == Short.TYPE