comparison src/luan/LuanJavaFunction.java @ 95:9aa7d5f20333

in LuanJavaFunction, fix primitive type checking and add boolean conversion git-svn-id: https://luan-java.googlecode.com/svn/trunk@96 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 08 Mar 2013 06:48:17 +0000
parents 6ca02b188dba
children 3a0ff21f0c96
comparison
equal deleted inserted replaced
94:c6633e5f0cc5 95:9aa7d5f20333
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.Map; 8 import java.util.Map;
9 import java.util.HashMap;
9 import java.util.Set; 10 import java.util.Set;
10 import java.util.Arrays; 11 import java.util.Arrays;
11 12
12 13
13 public final class LuanJavaFunction extends LuanFunction { 14 public final class LuanJavaFunction extends LuanFunction {
82 throw new RuntimeException(e); 83 throw new RuntimeException(e);
83 } 84 }
84 return rtnConverter.convert(rtn); 85 return rtnConverter.convert(rtn);
85 } 86 }
86 87
88 private static final Map<Class,Class> primitiveMap = new HashMap<Class,Class>();
89 static {
90 primitiveMap.put(Boolean.TYPE,Boolean.class);
91 primitiveMap.put(Character.TYPE,Character.class);
92 primitiveMap.put(Byte.TYPE,Byte.class);
93 primitiveMap.put(Short.TYPE,Short.class);
94 primitiveMap.put(Integer.TYPE,Integer.class);
95 primitiveMap.put(Long.TYPE,Long.class);
96 primitiveMap.put(Float.TYPE,Float.class);
97 primitiveMap.put(Double.TYPE,Double.class);
98 primitiveMap.put(Void.TYPE,Void.class);
99 }
100
87 private void checkArgs(LuanState luan,Object[] args) throws LuanException { 101 private void checkArgs(LuanState luan,Object[] args) throws LuanException {
88 Class<?>[] a = getParameterTypes(); 102 Class<?>[] a = getParameterTypes();
89 int start = takesLuaState ? 1 : 0; 103 int start = takesLuaState ? 1 : 0;
90 for( int i=start; i<a.length; i++ ) { 104 for( int i=start; i<a.length; i++ ) {
91 Class<?> paramType = a[i]; 105 Class<?> paramType = a[i];
106 Class<?> type = paramType;
107 if( type.isPrimitive() )
108 type = primitiveMap.get(type);
92 Object arg = args[i]; 109 Object arg = args[i];
93 if( !paramType.isInstance(arg) ) { 110 if( !type.isInstance(arg) ) {
94 String expected = paramType.getSimpleName(); 111 String expected = paramType.getSimpleName();
95 if( arg==null ) { 112 if( arg==null ) {
96 if( paramType.isPrimitive() ) 113 if( paramType.isPrimitive() )
97 throw luan.JAVA.exception("bad argument #"+(i+1-start)+" ("+expected+" expected, got nil)"); 114 throw luan.JAVA.exception("bad argument #"+(i+1-start)+" ("+expected+" expected, got nil)");
98 } else { 115 } else {
209 public Object convert(Object obj) { 226 public Object convert(Object obj) {
210 return obj; 227 return obj;
211 } 228 }
212 }; 229 };
213 230
231 private static final ArgConverter ARG_BOOLEAN = new ArgConverter() {
232 public Object convert(Object obj) {
233 return Luan.toBoolean(obj);
234 }
235 };
236
237 private static final ArgConverter ARG_BOOLEAN_OBJ = new ArgConverter() {
238 public Object convert(Object obj) {
239 return obj==null ? null : Luan.toBoolean(obj);
240 }
241 };
242
214 private static final ArgConverter ARG_DOUBLE = new ArgConverter() { 243 private static final ArgConverter ARG_DOUBLE = new ArgConverter() {
215 public Object convert(Object obj) { 244 public Object convert(Object obj) {
216 if( obj instanceof Double ) 245 if( obj instanceof Double )
217 return obj; 246 return obj;
218 if( obj instanceof Number ) { 247 if( obj instanceof Number ) {
431 } 460 }
432 return a; 461 return a;
433 } 462 }
434 463
435 private static ArgConverter getArgConverter(Class<?> cls) { 464 private static ArgConverter getArgConverter(Class<?> cls) {
465 if( cls == Boolean.TYPE )
466 return ARG_BOOLEAN;
467 if( cls.equals(Boolean.class) )
468 return ARG_BOOLEAN_OBJ;
436 if( cls == Double.TYPE || cls.equals(Double.class) ) 469 if( cls == Double.TYPE || cls.equals(Double.class) )
437 return ARG_DOUBLE; 470 return ARG_DOUBLE;
438 if( cls == Float.TYPE || cls.equals(Float.class) ) 471 if( cls == Float.TYPE || cls.equals(Float.class) )
439 return ARG_FLOAT; 472 return ARG_FLOAT;
440 if( cls == Long.TYPE || cls.equals(Long.class) ) 473 if( cls == Long.TYPE || cls.equals(Long.class) )