Mercurial Hosting > luan
comparison src/luan/LuaJavaFunction.java @ 43:80b67b1a653c
implement string lib
git-svn-id: https://luan-java.googlecode.com/svn/trunk@44 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 25 Dec 2012 03:42:42 +0000 |
parents | e3624b7cd603 |
children | 57054fa43189 |
comparison
equal
deleted
inserted
replaced
42:786699c78837 | 43:80b67b1a653c |
---|---|
79 if( varArgCls != null ) { | 79 if( varArgCls != null ) { |
80 n--; | 80 n--; |
81 if( args.length < argConverters.length ) { | 81 if( args.length < argConverters.length ) { |
82 rtn[rtn.length-1] = Array.newInstance(varArgCls,0); | 82 rtn[rtn.length-1] = Array.newInstance(varArgCls,0); |
83 } else { | 83 } else { |
84 Object[] varArgs = (Object[])Array.newInstance(varArgCls,args.length-n); | 84 int len = args.length - n; |
85 Object varArgs = Array.newInstance(varArgCls,len); | |
85 ArgConverter ac = argConverters[n]; | 86 ArgConverter ac = argConverters[n]; |
86 for( int i=0; i<varArgs.length; i++ ) { | 87 for( int i=0; i<len; i++ ) { |
87 varArgs[i] = ac.convert(args[n+i]); | 88 Array.set( varArgs, i, ac.convert(args[n+i]) ); |
88 } | 89 } |
89 rtn[rtn.length-1] = varArgs; | 90 rtn[rtn.length-1] = varArgs; |
90 } | 91 } |
91 } | 92 } |
92 System.arraycopy(args,0,rtn,start,Math.min(args.length,n)); | 93 System.arraycopy(args,0,rtn,start,Math.min(args.length,n)); |
108 } | 109 } |
109 }; | 110 }; |
110 | 111 |
111 private static final RtnConverter RTN_ARRAY = new RtnConverter() { | 112 private static final RtnConverter RTN_ARRAY = new RtnConverter() { |
112 public Object[] convert(Object obj) { | 113 public Object[] convert(Object obj) { |
114 if( obj == null ) | |
115 return NULL_RTN; | |
113 return (Object[])obj; | 116 return (Object[])obj; |
114 } | 117 } |
115 }; | 118 }; |
116 | 119 |
117 private static final RtnConverter RTN_ONE = new RtnConverter() { | 120 private static final RtnConverter RTN_ONE = new RtnConverter() { |
118 public Object[] convert(Object obj) { | 121 public Object[] convert(Object obj) { |
119 return new Object[]{obj}; | 122 return new Object[]{obj}; |
120 } | 123 } |
121 }; | 124 }; |
122 | 125 |
126 private static final Object[] NULL_RTN = new Object[1]; | |
127 | |
123 private static final RtnConverter RTN_NUMBER = new RtnConverter() { | 128 private static final RtnConverter RTN_NUMBER = new RtnConverter() { |
124 public Object[] convert(Object obj) { | 129 public Object[] convert(Object obj) { |
125 if( obj == null ) | 130 if( obj == null ) |
126 return new Object[1]; | 131 return NULL_RTN; |
127 Number n = (Number)obj; | 132 Number n = (Number)obj; |
128 LuaNumber ln = new LuaNumber(n.doubleValue()); | 133 LuaNumber ln = new LuaNumber(n.doubleValue()); |
129 return new Object[]{ln}; | 134 return new Object[]{ln}; |
135 } | |
136 }; | |
137 | |
138 private static final RtnConverter RTN_NUMBER_ARRAY = new RtnConverter() { | |
139 public Object[] convert(Object obj) { | |
140 if( obj == null ) | |
141 return NULL_RTN; | |
142 Object[] rtn = new Object[Array.getLength(obj)]; | |
143 for( int i=0; i<rtn.length; i++ ) { | |
144 Number n = (Number)Array.get(obj,i); | |
145 if( n != null ) | |
146 rtn[i] = new LuaNumber(n.doubleValue()); | |
147 } | |
148 return rtn; | |
130 } | 149 } |
131 }; | 150 }; |
132 | 151 |
133 private static RtnConverter getRtnConverter(JavaMethod m) { | 152 private static RtnConverter getRtnConverter(JavaMethod m) { |
134 Class<?> rtnType = m.getReturnType(); | 153 Class<?> rtnType = m.getReturnType(); |
135 if( rtnType == Void.TYPE ) | 154 if( rtnType == Void.TYPE ) |
136 return RTN_EMPTY; | 155 return RTN_EMPTY; |
137 if( Number.class.isAssignableFrom(rtnType) | 156 if( isNumber(rtnType) ) |
157 return RTN_NUMBER; | |
158 if( rtnType.isArray() ) { | |
159 rtnType = rtnType.getComponentType(); | |
160 if( isNumber(rtnType) ) | |
161 return RTN_NUMBER_ARRAY; | |
162 return RTN_ARRAY; | |
163 } | |
164 return RTN_ONE; | |
165 } | |
166 | |
167 private static boolean isNumber(Class<?> rtnType) { | |
168 return Number.class.isAssignableFrom(rtnType) | |
169 || rtnType == Byte.TYPE | |
138 || rtnType == Short.TYPE | 170 || rtnType == Short.TYPE |
139 || rtnType == Integer.TYPE | 171 || rtnType == Integer.TYPE |
140 || rtnType == Long.TYPE | 172 || rtnType == Long.TYPE |
141 || rtnType == Float.TYPE | 173 || rtnType == Float.TYPE |
142 || rtnType == Double.TYPE | 174 || rtnType == Double.TYPE |
143 ) | 175 ; |
144 return RTN_NUMBER; | |
145 if( rtnType.isArray() ) | |
146 return RTN_ARRAY; | |
147 return RTN_ONE; | |
148 } | 176 } |
149 | 177 |
150 | 178 |
151 private interface ArgConverter { | 179 private interface ArgConverter { |
152 public Object convert(Object obj); | 180 public Object convert(Object obj); |
236 } | 264 } |
237 else if( obj instanceof String ) { | 265 else if( obj instanceof String ) { |
238 String s = (String)obj; | 266 String s = (String)obj; |
239 try { | 267 try { |
240 return Short.valueOf(s); | 268 return Short.valueOf(s); |
269 } catch(NumberFormatException e) {} | |
270 } | |
271 return obj; | |
272 } | |
273 }; | |
274 | |
275 private static final ArgConverter ARG_BYTE = new ArgConverter() { | |
276 public Object convert(Object obj) { | |
277 if( obj instanceof LuaNumber ) { | |
278 LuaNumber ln = (LuaNumber)obj; | |
279 byte i = (byte)ln.n; | |
280 if( i == ln.n ) | |
281 return Byte.valueOf(i); | |
282 } | |
283 else if( obj instanceof String ) { | |
284 String s = (String)obj; | |
285 try { | |
286 return Byte.valueOf(s); | |
241 } catch(NumberFormatException e) {} | 287 } catch(NumberFormatException e) {} |
242 } | 288 } |
243 return obj; | 289 return obj; |
244 } | 290 } |
245 }; | 291 }; |
276 return ARG_LONG; | 322 return ARG_LONG; |
277 if( cls == Integer.TYPE || cls.equals(Integer.class) ) | 323 if( cls == Integer.TYPE || cls.equals(Integer.class) ) |
278 return ARG_INTEGER; | 324 return ARG_INTEGER; |
279 if( cls == Short.TYPE || cls.equals(Short.class) ) | 325 if( cls == Short.TYPE || cls.equals(Short.class) ) |
280 return ARG_SHORT; | 326 return ARG_SHORT; |
327 if( cls == Byte.TYPE || cls.equals(Byte.class) ) | |
328 return ARG_BYTE; | |
281 return ARG_SAME; | 329 return ARG_SAME; |
282 } | 330 } |
283 | 331 |
284 | 332 |
285 | 333 |
290 throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException; | 338 throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException; |
291 abstract Class<?> getReturnType(); | 339 abstract Class<?> getReturnType(); |
292 | 340 |
293 static JavaMethod of(final Method m) { | 341 static JavaMethod of(final Method m) { |
294 return new JavaMethod() { | 342 return new JavaMethod() { |
295 boolean isVarArgs() { | 343 @Override boolean isVarArgs() { |
296 return m.isVarArgs(); | 344 return m.isVarArgs(); |
297 } | 345 } |
298 Class<?>[] getParameterTypes() { | 346 @Override Class<?>[] getParameterTypes() { |
299 return m.getParameterTypes(); | 347 return m.getParameterTypes(); |
300 } | 348 } |
301 Object invoke(Object obj,Object... args) | 349 @Override Object invoke(Object obj,Object... args) |
302 throws IllegalAccessException, IllegalArgumentException, InvocationTargetException | 350 throws IllegalAccessException, IllegalArgumentException, InvocationTargetException |
303 { | 351 { |
304 return m.invoke(obj,args); | 352 return m.invoke(obj,args); |
305 } | 353 } |
306 Class<?> getReturnType() { | 354 @Override Class<?> getReturnType() { |
307 return m.getReturnType(); | 355 return m.getReturnType(); |
356 } | |
357 @Override public String toString() { | |
358 return m.toString(); | |
308 } | 359 } |
309 }; | 360 }; |
310 } | 361 } |
311 | 362 |
312 static JavaMethod of(final Constructor c) { | 363 static JavaMethod of(final Constructor c) { |
313 return new JavaMethod() { | 364 return new JavaMethod() { |
314 boolean isVarArgs() { | 365 @Override boolean isVarArgs() { |
315 return c.isVarArgs(); | 366 return c.isVarArgs(); |
316 } | 367 } |
317 Class<?>[] getParameterTypes() { | 368 @Override Class<?>[] getParameterTypes() { |
318 return c.getParameterTypes(); | 369 return c.getParameterTypes(); |
319 } | 370 } |
320 Object invoke(Object obj,Object... args) | 371 @Override Object invoke(Object obj,Object... args) |
321 throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException | 372 throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException |
322 { | 373 { |
323 return c.newInstance(args); | 374 return c.newInstance(args); |
324 } | 375 } |
325 Class<?> getReturnType() { | 376 @Override Class<?> getReturnType() { |
326 return c.getDeclaringClass(); | 377 return c.getDeclaringClass(); |
327 } | 378 } |
379 @Override public String toString() { | |
380 return c.toString(); | |
381 } | |
328 }; | 382 }; |
329 } | 383 } |
330 | 384 |
331 } | 385 } |
332 | 386 |