Mercurial Hosting > luan
comparison core/src/luan/LuanJavaFunction.java @ 434:472fc70853cd
remove more generics
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 03 May 2015 16:21:25 -0600 |
parents | d9df6d6cb927 |
children | bf5e62a9090c |
comparison
equal
deleted
inserted
replaced
433:c6bcb8859b93 | 434:472fc70853cd |
---|---|
17 private final JavaMethod method; | 17 private final JavaMethod method; |
18 private Object obj; | 18 private Object obj; |
19 private final RtnConverter rtnConverter; | 19 private final RtnConverter rtnConverter; |
20 private final boolean takesLuaState; | 20 private final boolean takesLuaState; |
21 private final ArgConverter[] argConverters; | 21 private final ArgConverter[] argConverters; |
22 private final Class<?> varArgCls; | 22 private final Class varArgCls; |
23 | 23 |
24 public LuanJavaFunction(Method method,Object obj) { | 24 public LuanJavaFunction(Method method,Object obj) { |
25 this( JavaMethod.of(method), obj ); | 25 this( JavaMethod.of(method), obj ); |
26 } | 26 } |
27 | 27 |
34 this.obj = obj; | 34 this.obj = obj; |
35 this.rtnConverter = getRtnConverter(method); | 35 this.rtnConverter = getRtnConverter(method); |
36 this.takesLuaState = takesLuaState(method); | 36 this.takesLuaState = takesLuaState(method); |
37 this.argConverters = getArgConverters(takesLuaState,method); | 37 this.argConverters = getArgConverters(takesLuaState,method); |
38 if( method.isVarArgs() ) { | 38 if( method.isVarArgs() ) { |
39 Class<?>[] paramTypes = method.getParameterTypes(); | 39 Class[] paramTypes = method.getParameterTypes(); |
40 this.varArgCls = paramTypes[paramTypes.length-1].getComponentType(); | 40 this.varArgCls = paramTypes[paramTypes.length-1].getComponentType(); |
41 } else { | 41 } else { |
42 this.varArgCls = null; | 42 this.varArgCls = null; |
43 } | 43 } |
44 } | 44 } |
61 */ | 61 */ |
62 @Override public String toString() { | 62 @Override public String toString() { |
63 return "java-function: " + method; | 63 return "java-function: " + method; |
64 } | 64 } |
65 | 65 |
66 public Class<?>[] getParameterTypes() { | 66 public Class[] getParameterTypes() { |
67 return method.getParameterTypes(); | 67 return method.getParameterTypes(); |
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 { |
101 throw new RuntimeException(e); | 101 throw new RuntimeException(e); |
102 } | 102 } |
103 return rtnConverter.convert(rtn); | 103 return rtnConverter.convert(rtn); |
104 } | 104 } |
105 | 105 |
106 private static final Map<Class,Class> primitiveMap = new HashMap<Class,Class>(); | 106 private static final Map primitiveMap = new HashMap(); |
107 static { | 107 static { |
108 primitiveMap.put(Boolean.TYPE,Boolean.class); | 108 primitiveMap.put(Boolean.TYPE,Boolean.class); |
109 primitiveMap.put(Character.TYPE,Character.class); | 109 primitiveMap.put(Character.TYPE,Character.class); |
110 primitiveMap.put(Byte.TYPE,Byte.class); | 110 primitiveMap.put(Byte.TYPE,Byte.class); |
111 primitiveMap.put(Short.TYPE,Short.class); | 111 primitiveMap.put(Short.TYPE,Short.class); |
115 primitiveMap.put(Double.TYPE,Double.class); | 115 primitiveMap.put(Double.TYPE,Double.class); |
116 primitiveMap.put(Void.TYPE,Void.class); | 116 primitiveMap.put(Void.TYPE,Void.class); |
117 } | 117 } |
118 | 118 |
119 private void checkArgs(LuanState luan,Object[] args) throws LuanException { | 119 private void checkArgs(LuanState luan,Object[] args) throws LuanException { |
120 Class<?>[] a = getParameterTypes(); | 120 Class[] a = getParameterTypes(); |
121 int start = takesLuaState ? 1 : 0; | 121 int start = takesLuaState ? 1 : 0; |
122 for( int i=start; i<a.length; i++ ) { | 122 for( int i=start; i<a.length; i++ ) { |
123 Class<?> paramType = a[i]; | 123 Class paramType = a[i]; |
124 Class<?> type = paramType; | 124 Class type = paramType; |
125 if( type.isPrimitive() ) | 125 if( type.isPrimitive() ) |
126 type = primitiveMap.get(type); | 126 type = (Class)primitiveMap.get(type); |
127 Object arg = args[i]; | 127 Object arg = args[i]; |
128 if( !type.isInstance(arg) ) { | 128 if( !type.isInstance(arg) ) { |
129 String expected = paramType.getSimpleName(); | 129 String expected = paramType.getSimpleName(); |
130 if( i==a.length-1 && method.isVarArgs() ) | 130 if( i==a.length-1 && method.isVarArgs() ) |
131 expected = paramType.getComponentType().getSimpleName()+"..."; | 131 expected = paramType.getComponentType().getSimpleName()+"..."; |
204 return new LuanTable(new ArrayList<Object>(Arrays.asList(a))); | 204 return new LuanTable(new ArrayList<Object>(Arrays.asList(a))); |
205 } | 205 } |
206 }; | 206 }; |
207 | 207 |
208 private static RtnConverter getRtnConverter(JavaMethod m) { | 208 private static RtnConverter getRtnConverter(JavaMethod m) { |
209 Class<?> rtnType = m.getReturnType(); | 209 Class rtnType = m.getReturnType(); |
210 if( rtnType == Void.TYPE ) | 210 if( rtnType == Void.TYPE ) |
211 return RTN_NOTHING; | 211 return RTN_NOTHING; |
212 if( !m.isLuan() && rtnType.isArray() && !rtnType.getComponentType().isPrimitive() ) { | 212 if( !m.isLuan() && rtnType.isArray() && !rtnType.getComponentType().isPrimitive() ) { |
213 //System.out.println("qqqqqq "+m); | 213 //System.out.println("qqqqqq "+m); |
214 return RTN_ARRAY; | 214 return RTN_ARRAY; |
215 } | 215 } |
216 return RTN_SAME; | 216 return RTN_SAME; |
217 } | 217 } |
218 | 218 |
219 private static boolean isNumber(Class<?> rtnType) { | 219 private static boolean isNumber(Class rtnType) { |
220 return rtnType == Short.TYPE | 220 return rtnType == Short.TYPE |
221 || rtnType == Integer.TYPE | 221 || rtnType == Integer.TYPE |
222 || rtnType == Long.TYPE | 222 || rtnType == Long.TYPE |
223 || rtnType == Float.TYPE | 223 || rtnType == Float.TYPE |
224 || rtnType == Double.TYPE | 224 || rtnType == Double.TYPE |
393 private static final ArgConverter ARG_TABLE = new ArgConverter() { | 393 private static final ArgConverter ARG_TABLE = new ArgConverter() { |
394 public Object convert(LuanState luan,Object obj) { | 394 public Object convert(LuanState luan,Object obj) { |
395 if( obj == null ) | 395 if( obj == null ) |
396 return null; | 396 return null; |
397 if( obj instanceof List ) { | 397 if( obj instanceof List ) { |
398 @SuppressWarnings("unchecked") | 398 return new LuanTable((List)obj); |
399 List<Object> list = (List<Object>)obj; | |
400 return new LuanTable(list); | |
401 } | 399 } |
402 if( obj instanceof Map ) { | 400 if( obj instanceof Map ) { |
403 @SuppressWarnings("unchecked") | 401 return new LuanTable((Map)obj); |
404 Map<Object,Object> map = (Map<Object,Object>)obj; | |
405 return new LuanTable(map); | |
406 } | 402 } |
407 if( obj instanceof Set ) { | 403 if( obj instanceof Set ) { |
408 @SuppressWarnings("unchecked") | 404 return new LuanTable((Set)obj); |
409 Set<Object> set = (Set<Object>)obj; | |
410 return new LuanTable(set); | |
411 } | 405 } |
412 Class cls = obj.getClass(); | 406 Class cls = obj.getClass(); |
413 if( cls.isArray() && !cls.getComponentType().isPrimitive() ) { | 407 if( cls.isArray() && !cls.getComponentType().isPrimitive() ) { |
414 Object[] a = (Object[])obj; | 408 Object[] a = (Object[])obj; |
415 return new LuanTable(Arrays.asList(a)); | 409 return new LuanTable(Arrays.asList(a)); |
497 return obj; | 491 return obj; |
498 } | 492 } |
499 } | 493 } |
500 | 494 |
501 private static boolean takesLuaState(JavaMethod m) { | 495 private static boolean takesLuaState(JavaMethod m) { |
502 Class<?>[] paramTypes = m.getParameterTypes(); | 496 Class[] paramTypes = m.getParameterTypes(); |
503 return paramTypes.length > 0 && paramTypes[0].equals(LuanState.class); | 497 return paramTypes.length > 0 && paramTypes[0].equals(LuanState.class); |
504 } | 498 } |
505 | 499 |
506 private static ArgConverter[] getArgConverters(boolean takesLuaState,JavaMethod m) { | 500 private static ArgConverter[] getArgConverters(boolean takesLuaState,JavaMethod m) { |
507 final boolean isVarArgs = m.isVarArgs(); | 501 final boolean isVarArgs = m.isVarArgs(); |
508 Class<?>[] paramTypes = m.getParameterTypes(); | 502 Class[] paramTypes = m.getParameterTypes(); |
509 if( takesLuaState ) { | 503 if( takesLuaState ) { |
510 Class<?>[] t = new Class<?>[paramTypes.length-1]; | 504 Class[] t = new Class[paramTypes.length-1]; |
511 System.arraycopy(paramTypes,1,t,0,t.length); | 505 System.arraycopy(paramTypes,1,t,0,t.length); |
512 paramTypes = t; | 506 paramTypes = t; |
513 } | 507 } |
514 ArgConverter[] a = new ArgConverter[paramTypes.length]; | 508 ArgConverter[] a = new ArgConverter[paramTypes.length]; |
515 for( int i=0; i<a.length; i++ ) { | 509 for( int i=0; i<a.length; i++ ) { |
516 Class<?> paramType = paramTypes[i]; | 510 Class paramType = paramTypes[i]; |
517 if( isVarArgs && i == a.length-1 ) | 511 if( isVarArgs && i == a.length-1 ) |
518 paramType = paramType.getComponentType(); | 512 paramType = paramType.getComponentType(); |
519 a[i] = getArgConverter(paramType); | 513 a[i] = getArgConverter(paramType); |
520 } | 514 } |
521 return a; | 515 return a; |
522 } | 516 } |
523 | 517 |
524 private static ArgConverter getArgConverter(Class<?> cls) { | 518 private static ArgConverter getArgConverter(Class cls) { |
525 if( cls == Boolean.TYPE ) | 519 if( cls == Boolean.TYPE ) |
526 return ARG_BOOLEAN; | 520 return ARG_BOOLEAN; |
527 if( cls.equals(Boolean.class) ) | 521 if( cls.equals(Boolean.class) ) |
528 return ARG_BOOLEAN_OBJ; | 522 return ARG_BOOLEAN_OBJ; |
529 if( cls == Double.TYPE || cls.equals(Double.class) ) | 523 if( cls == Double.TYPE || cls.equals(Double.class) ) |
555 | 549 |
556 | 550 |
557 | 551 |
558 private static abstract class JavaMethod { | 552 private static abstract class JavaMethod { |
559 abstract boolean isVarArgs(); | 553 abstract boolean isVarArgs(); |
560 abstract Class<?>[] getParameterTypes(); | 554 abstract Class[] getParameterTypes(); |
561 abstract Object invoke(Object obj,Object... args) | 555 abstract Object invoke(Object obj,Object... args) |
562 throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException; | 556 throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException; |
563 abstract Class<?> getReturnType(); | 557 abstract Class getReturnType(); |
564 abstract boolean isLuan(); | 558 abstract boolean isLuan(); |
565 | 559 |
566 static JavaMethod of(final Method m) { | 560 static JavaMethod of(final Method m) { |
567 return new JavaMethod() { | 561 return new JavaMethod() { |
568 @Override boolean isVarArgs() { | 562 @Override boolean isVarArgs() { |
569 return m.isVarArgs(); | 563 return m.isVarArgs(); |
570 } | 564 } |
571 @Override Class<?>[] getParameterTypes() { | 565 @Override Class[] getParameterTypes() { |
572 return m.getParameterTypes(); | 566 return m.getParameterTypes(); |
573 } | 567 } |
574 @Override Object invoke(Object obj,Object... args) | 568 @Override Object invoke(Object obj,Object... args) |
575 throws IllegalAccessException, IllegalArgumentException, InvocationTargetException | 569 throws IllegalAccessException, IllegalArgumentException, InvocationTargetException |
576 { | 570 { |
577 return m.invoke(obj,args); | 571 return m.invoke(obj,args); |
578 } | 572 } |
579 @Override Class<?> getReturnType() { | 573 @Override Class getReturnType() { |
580 return m.getReturnType(); | 574 return m.getReturnType(); |
581 } | 575 } |
582 @Override boolean isLuan() { | 576 @Override boolean isLuan() { |
583 return m.getAnnotation(LuanMethod.class) != null; | 577 return m.getAnnotation(LuanMethod.class) != null; |
584 } | 578 } |
591 static JavaMethod of(final Constructor c) { | 585 static JavaMethod of(final Constructor c) { |
592 return new JavaMethod() { | 586 return new JavaMethod() { |
593 @Override boolean isVarArgs() { | 587 @Override boolean isVarArgs() { |
594 return c.isVarArgs(); | 588 return c.isVarArgs(); |
595 } | 589 } |
596 @Override Class<?>[] getParameterTypes() { | 590 @Override Class[] getParameterTypes() { |
597 return c.getParameterTypes(); | 591 return c.getParameterTypes(); |
598 } | 592 } |
599 @Override Object invoke(Object obj,Object... args) | 593 @Override Object invoke(Object obj,Object... args) |
600 throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException | 594 throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException |
601 { | 595 { |
602 return c.newInstance(args); | 596 return c.newInstance(args); |
603 } | 597 } |
604 @Override Class<?> getReturnType() { | 598 @Override Class getReturnType() { |
605 return c.getDeclaringClass(); | 599 return c.getDeclaringClass(); |
606 } | 600 } |
607 @Override boolean isLuan() { | 601 @Override boolean isLuan() { |
608 return false; | 602 return false; |
609 } | 603 } |