1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
|
26
|
|
|
27
|
|
|
28
|
|
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
|
|
33
|
|
|
34
|
|
|
35
|
|
|
36
|
|
|
37
|
|
|
38
|
|
|
39
|
|
|
40
|
|
|
41
|
|
|
42
|
|
|
43
|
|
|
44
|
|
|
45
|
|
|
46
|
|
package org.codehaus.groovy.runtime;
|
47
|
|
|
48
|
|
import groovy.lang.Binding;
|
49
|
|
import groovy.lang.GroovyObject;
|
50
|
|
import groovy.lang.GroovyRuntimeException;
|
51
|
|
import groovy.lang.IntRange;
|
52
|
|
import groovy.lang.MetaClass;
|
53
|
|
import groovy.lang.ObjectRange;
|
54
|
|
import groovy.lang.Script;
|
55
|
|
import groovy.lang.Tuple;
|
56
|
|
import groovy.lang.Writable;
|
57
|
|
|
58
|
|
import java.io.IOException;
|
59
|
|
import java.io.InputStream;
|
60
|
|
import java.io.InputStreamReader;
|
61
|
|
import java.io.Reader;
|
62
|
|
import java.io.Writer;
|
63
|
|
import java.lang.reflect.Array;
|
64
|
|
import java.math.BigDecimal;
|
65
|
|
import java.math.BigInteger;
|
66
|
|
import java.util.ArrayList;
|
67
|
|
import java.util.Collection;
|
68
|
|
import java.util.HashMap;
|
69
|
|
import java.util.Iterator;
|
70
|
|
import java.util.List;
|
71
|
|
import java.util.Map;
|
72
|
|
import java.util.regex.Matcher;
|
73
|
|
import java.util.regex.Pattern;
|
74
|
|
|
75
|
|
|
76
|
|
|
77
|
|
|
78
|
|
|
79
|
|
|
80
|
|
|
81
|
|
public class InvokerHelper {
|
82
|
|
public static final Object[] EMPTY_ARGS = {
|
83
|
|
};
|
84
|
|
|
85
|
|
private static final Object[] EMPTY_MAIN_ARGS = new Object[] { new String[0] };
|
86
|
|
|
87
|
|
private static final Invoker singleton = new Invoker();
|
88
|
|
|
89
|
|
private static final Integer ZERO = new Integer(0);
|
90
|
|
private static final Integer MINUS_ONE = new Integer(-1);
|
91
|
|
private static final Integer ONE = new Integer(1);
|
92
|
|
|
93
|
0
|
public static MetaClass getMetaClass(Object object) {
|
94
|
0
|
return getInstance().getMetaClass(object);
|
95
|
|
}
|
96
|
|
|
97
|
0
|
public static Invoker getInstance() {
|
98
|
0
|
return singleton;
|
99
|
|
}
|
100
|
|
|
101
|
0
|
public static Object invokeNoArgumentsMethod(Object object, String methodName) {
|
102
|
0
|
return getInstance().invokeMethod(object, methodName, EMPTY_ARGS);
|
103
|
|
}
|
104
|
|
|
105
|
0
|
public static Object invokeMethod(Object object, String methodName, Object arguments) {
|
106
|
0
|
return getInstance().invokeMethod(object, methodName, arguments);
|
107
|
|
}
|
108
|
|
|
109
|
0
|
public static Object invokeSuperMethod(Object object, String methodName, Object arguments) {
|
110
|
0
|
return getInstance().invokeSuperMethod(object, methodName, arguments);
|
111
|
|
}
|
112
|
|
|
113
|
0
|
public static Object invokeMethodSafe(Object object, String methodName, Object arguments) {
|
114
|
0
|
if (object != null) {
|
115
|
0
|
return getInstance().invokeMethod(object, methodName, arguments);
|
116
|
|
}
|
117
|
0
|
return null;
|
118
|
|
}
|
119
|
|
|
120
|
0
|
public static Object invokeStaticMethod(String type, String methodName, Object arguments) {
|
121
|
0
|
return getInstance().invokeStaticMethod(type, methodName, arguments);
|
122
|
|
}
|
123
|
|
|
124
|
0
|
public static Object invokeStaticNoArgumentsMethod(String type, String methodName) {
|
125
|
0
|
return getInstance().invokeStaticMethod(type, methodName, EMPTY_ARGS);
|
126
|
|
}
|
127
|
|
|
128
|
0
|
public static Object invokeConstructor(String type, Object arguments) {
|
129
|
0
|
return getInstance().invokeConstructor(type, arguments);
|
130
|
|
}
|
131
|
|
|
132
|
0
|
public static Object invokeConstructorOf(Class type, Object arguments) {
|
133
|
0
|
return getInstance().invokeConstructorOf(type, arguments);
|
134
|
|
}
|
135
|
|
|
136
|
0
|
public static Object invokeNoArgumentsConstructorOf(Class type) {
|
137
|
0
|
return getInstance().invokeConstructorOf(type, EMPTY_ARGS);
|
138
|
|
}
|
139
|
|
|
140
|
0
|
public static Object invokeClosure(Object closure, Object arguments) {
|
141
|
0
|
return getInstance().invokeMethod(closure, "doCall", arguments);
|
142
|
|
}
|
143
|
|
|
144
|
0
|
public static Iterator asIterator(Object collection) {
|
145
|
0
|
return getInstance().asIterator(collection);
|
146
|
|
}
|
147
|
|
|
148
|
0
|
public static Collection asCollection(Object collection) {
|
149
|
0
|
return getInstance().asCollection(collection);
|
150
|
|
}
|
151
|
|
|
152
|
0
|
public static List asList(Object args) {
|
153
|
0
|
return getInstance().asList(args);
|
154
|
|
}
|
155
|
|
|
156
|
0
|
public static String toString(Object arguments) {
|
157
|
0
|
return getInstance().toString(arguments);
|
158
|
|
}
|
159
|
|
|
160
|
0
|
public static String toTypeString(Object[] arguments) {
|
161
|
0
|
return getInstance().toTypeString(arguments);
|
162
|
|
}
|
163
|
|
|
164
|
0
|
public static String inspect(Object self) {
|
165
|
0
|
return getInstance().inspect(self);
|
166
|
|
}
|
167
|
|
|
168
|
0
|
public static Object getProperty(Object object, String property) {
|
169
|
0
|
return getInstance().getProperty(object, property);
|
170
|
|
}
|
171
|
|
|
172
|
0
|
public static Object getPropertySafe(Object object, String property) {
|
173
|
0
|
if (object != null) {
|
174
|
0
|
return getInstance().getProperty(object, property);
|
175
|
|
}
|
176
|
0
|
return null;
|
177
|
|
}
|
178
|
|
|
179
|
0
|
public static void setProperty(Object object, String property, Object newValue) {
|
180
|
0
|
getInstance().setProperty(object, property, newValue);
|
181
|
|
}
|
182
|
|
|
183
|
|
|
184
|
|
|
185
|
|
|
186
|
|
|
187
|
0
|
public static void setProperty2(Object newValue, Object object, String property) {
|
188
|
0
|
getInstance().setProperty(object, property, newValue);
|
189
|
|
}
|
190
|
|
|
191
|
|
|
192
|
|
|
193
|
|
|
194
|
|
|
195
|
|
|
196
|
0
|
public static void setGroovyObjectProperty(Object newValue, GroovyObject object, String property) {
|
197
|
0
|
object.setProperty(property, newValue);
|
198
|
|
}
|
199
|
|
|
200
|
0
|
public static Object getGroovyObjectProperty(GroovyObject object, String property) {
|
201
|
0
|
return object.getProperty(property);
|
202
|
|
}
|
203
|
|
|
204
|
|
|
205
|
|
|
206
|
|
|
207
|
|
|
208
|
|
|
209
|
0
|
public static void setPropertySafe2(Object newValue, Object object, String property) {
|
210
|
0
|
if (object != null) {
|
211
|
0
|
setProperty2(newValue, object, property);
|
212
|
|
}
|
213
|
|
}
|
214
|
|
|
215
|
|
|
216
|
|
|
217
|
|
|
218
|
|
|
219
|
|
|
220
|
|
|
221
|
0
|
public static Object asType(Object object, Class type) {
|
222
|
0
|
return getInstance().asType(object, type);
|
223
|
|
}
|
224
|
|
|
225
|
0
|
public static boolean asBool(Object object) {
|
226
|
0
|
return getInstance().asBool(object);
|
227
|
|
}
|
228
|
|
|
229
|
0
|
public static boolean notObject(Object object) {
|
230
|
0
|
return !asBool(object);
|
231
|
|
}
|
232
|
|
|
233
|
0
|
public static boolean notBoolean(boolean bool) {
|
234
|
0
|
return !bool;
|
235
|
|
}
|
236
|
|
|
237
|
0
|
public static Object negate(Object value) {
|
238
|
0
|
if (value instanceof Integer) {
|
239
|
0
|
Integer number = (Integer) value;
|
240
|
0
|
return integerValue(-number.intValue());
|
241
|
|
}
|
242
|
0
|
else if (value instanceof Long) {
|
243
|
0
|
Long number = (Long) value;
|
244
|
0
|
return new Long(-number.longValue());
|
245
|
|
}
|
246
|
0
|
else if (value instanceof BigInteger) {
|
247
|
0
|
return ((BigInteger)value).negate();
|
248
|
|
}
|
249
|
0
|
else if (value instanceof BigDecimal) {
|
250
|
0
|
return ((BigDecimal)value).negate();
|
251
|
|
}
|
252
|
0
|
else if (value instanceof Double) {
|
253
|
0
|
Double number = (Double) value;
|
254
|
0
|
return new Double(-number.doubleValue());
|
255
|
|
}
|
256
|
0
|
else if (value instanceof Float) {
|
257
|
0
|
Float number = (Float) value;
|
258
|
0
|
return new Float(-number.floatValue());
|
259
|
|
}
|
260
|
|
else {
|
261
|
0
|
throw new GroovyRuntimeException(
|
262
|
|
"Cannot negate type " + value.getClass().getName() + ", value "+value);
|
263
|
|
}
|
264
|
|
}
|
265
|
|
|
266
|
0
|
public static boolean isCase(Object switchValue, Object caseExpression) {
|
267
|
0
|
return asBool(invokeMethod(caseExpression, "isCase", new Object[] { switchValue }));
|
268
|
|
}
|
269
|
|
|
270
|
0
|
public static boolean compareIdentical(Object left, Object right) {
|
271
|
0
|
return left == right;
|
272
|
|
}
|
273
|
|
|
274
|
0
|
public static boolean compareEqual(Object left, Object right) {
|
275
|
0
|
return getInstance().objectsEqual(left, right);
|
276
|
|
}
|
277
|
|
|
278
|
0
|
public static Matcher findRegex(Object left, Object right) {
|
279
|
0
|
return getInstance().objectFindRegex(left, right);
|
280
|
|
}
|
281
|
|
|
282
|
0
|
public static boolean matchRegex(Object left, Object right) {
|
283
|
0
|
return getInstance().objectMatchRegex(left, right);
|
284
|
|
}
|
285
|
|
|
286
|
0
|
public static Pattern regexPattern(Object regex) {
|
287
|
0
|
return getInstance().regexPattern(regex);
|
288
|
|
}
|
289
|
|
|
290
|
0
|
public static boolean compareNotEqual(Object left, Object right) {
|
291
|
0
|
return !getInstance().objectsEqual(left, right);
|
292
|
|
}
|
293
|
|
|
294
|
0
|
public static boolean compareLessThan(Object left, Object right) {
|
295
|
0
|
return getInstance().compareTo(left, right) < 0;
|
296
|
|
}
|
297
|
|
|
298
|
0
|
public static boolean compareLessThanEqual(Object left, Object right) {
|
299
|
0
|
return getInstance().compareTo(left, right) <= 0;
|
300
|
|
}
|
301
|
|
|
302
|
0
|
public static boolean compareGreaterThan(Object left, Object right) {
|
303
|
0
|
return getInstance().compareTo(left, right) > 0;
|
304
|
|
}
|
305
|
|
|
306
|
0
|
public static boolean compareGreaterThanEqual(Object left, Object right) {
|
307
|
0
|
return getInstance().compareTo(left, right) >= 0;
|
308
|
|
}
|
309
|
|
|
310
|
0
|
public static Integer compareTo(Object left, Object right) {
|
311
|
0
|
int answer = getInstance().compareTo(left, right);
|
312
|
0
|
if (answer == 0) {
|
313
|
0
|
return ZERO;
|
314
|
|
}
|
315
|
|
else {
|
316
|
0
|
return answer > 0 ? ONE : MINUS_ONE;
|
317
|
|
}
|
318
|
|
}
|
319
|
|
|
320
|
0
|
public static Tuple createTuple(Object[] array) {
|
321
|
0
|
return new Tuple(array);
|
322
|
|
}
|
323
|
|
|
324
|
0
|
public static List createList(Object[] values) {
|
325
|
0
|
ArrayList answer = new ArrayList(values.length);
|
326
|
0
|
for (int i = 0; i < values.length; i++) {
|
327
|
0
|
answer.add(values[i]);
|
328
|
|
}
|
329
|
0
|
return answer;
|
330
|
|
}
|
331
|
|
|
332
|
0
|
public static Map createMap(Object[] values) {
|
333
|
0
|
Map answer = new HashMap(values.length / 2);
|
334
|
0
|
int i = 0;
|
335
|
0
|
while (i < values.length) {
|
336
|
0
|
answer.put(values[i++], values[i++]);
|
337
|
|
}
|
338
|
0
|
return answer;
|
339
|
|
}
|
340
|
|
|
341
|
0
|
public static List createRange(Object from, Object to, boolean inclusive) {
|
342
|
0
|
if (!inclusive) {
|
343
|
0
|
if (compareGreaterThan(from, to)) {
|
344
|
0
|
to = invokeMethod(to, "next", EMPTY_ARGS);
|
345
|
|
}
|
346
|
|
else {
|
347
|
0
|
to = invokeMethod(to, "previous", EMPTY_ARGS);
|
348
|
|
}
|
349
|
|
}
|
350
|
0
|
if (from instanceof Integer && to instanceof Integer) {
|
351
|
0
|
return new IntRange(asInt(from), asInt(to));
|
352
|
|
}
|
353
|
|
else {
|
354
|
0
|
return new ObjectRange((Comparable) from, (Comparable) to);
|
355
|
|
}
|
356
|
|
}
|
357
|
|
|
358
|
0
|
public static int asInt(Object value) {
|
359
|
0
|
return getInstance().asInt(value);
|
360
|
|
}
|
361
|
|
|
362
|
0
|
public static void assertFailed(Object expression, Object message) {
|
363
|
0
|
if (message == null || "".equals(message)) {
|
364
|
0
|
throw new AssertionError("Expression: " + expression);
|
365
|
|
}
|
366
|
|
else {
|
367
|
0
|
throw new AssertionError("" + message + ". Expression: " + expression);
|
368
|
|
}
|
369
|
|
}
|
370
|
|
|
371
|
0
|
public static Object runScript(Class scriptClass, String[] args) {
|
372
|
0
|
Binding context = new Binding(args);
|
373
|
0
|
Script script = createScript(scriptClass, context);
|
374
|
0
|
return invokeMethod(script, "run", EMPTY_ARGS);
|
375
|
|
}
|
376
|
|
|
377
|
0
|
public static Script createScript(Class scriptClass, Binding context) {
|
378
|
0
|
try {
|
379
|
0
|
final GroovyObject object = (GroovyObject) scriptClass.newInstance();
|
380
|
0
|
Script script = null;
|
381
|
0
|
if (object instanceof Script) {
|
382
|
0
|
script = (Script) object;
|
383
|
|
}
|
384
|
|
else {
|
385
|
|
|
386
|
|
|
387
|
0
|
script = new Script() {
|
388
|
0
|
public Object run() {
|
389
|
0
|
object.invokeMethod("main", EMPTY_MAIN_ARGS);
|
390
|
0
|
return null;
|
391
|
|
}
|
392
|
|
};
|
393
|
0
|
setProperties(object, context.getVariables());
|
394
|
|
}
|
395
|
0
|
script.setBinding(context);
|
396
|
0
|
return script;
|
397
|
|
}
|
398
|
|
catch (Exception e) {
|
399
|
0
|
throw new GroovyRuntimeException(
|
400
|
|
"Failed to create Script instance for class: " + scriptClass + ". Reason: " + e,
|
401
|
|
e);
|
402
|
|
}
|
403
|
|
}
|
404
|
|
|
405
|
|
|
406
|
|
|
407
|
|
|
408
|
|
|
409
|
|
|
410
|
|
|
411
|
0
|
public static void setProperties(Object object, Map map) {
|
412
|
0
|
getMetaClass(object).setProperties(object, map);
|
413
|
|
}
|
414
|
|
|
415
|
0
|
public static String getVersion() {
|
416
|
0
|
String version = null;
|
417
|
0
|
Package p = Package.getPackage("groovy.lang");
|
418
|
0
|
if (p != null) {
|
419
|
0
|
version = p.getImplementationVersion();
|
420
|
|
}
|
421
|
0
|
if (version == null) {
|
422
|
0
|
version = "";
|
423
|
|
}
|
424
|
0
|
return version;
|
425
|
|
}
|
426
|
|
|
427
|
|
|
428
|
|
|
429
|
|
|
430
|
|
|
431
|
|
|
432
|
0
|
protected static List primitiveArrayToList(Object array) {
|
433
|
0
|
int size = Array.getLength(array);
|
434
|
0
|
List list = new ArrayList(size);
|
435
|
0
|
for (int i = 0; i < size; i++) {
|
436
|
0
|
list.add(Array.get(array, i));
|
437
|
|
}
|
438
|
0
|
return list;
|
439
|
|
}
|
440
|
|
|
441
|
|
|
442
|
|
|
443
|
|
|
444
|
0
|
public static void write(Writer out, Object object) throws IOException {
|
445
|
0
|
if (object instanceof String) {
|
446
|
0
|
out.write((String) object);
|
447
|
|
}
|
448
|
0
|
else if (object instanceof Writable) {
|
449
|
0
|
Writable writable = (Writable) object;
|
450
|
0
|
writable.writeTo(out);
|
451
|
|
}
|
452
|
0
|
else if (object instanceof InputStream || object instanceof Reader) {
|
453
|
|
|
454
|
0
|
Reader reader;
|
455
|
0
|
if (object instanceof InputStream) {
|
456
|
0
|
reader = new InputStreamReader((InputStream)object);
|
457
|
|
} else {
|
458
|
0
|
reader = (Reader) object;
|
459
|
|
}
|
460
|
0
|
char[] chars = new char[8192];
|
461
|
0
|
int i;
|
462
|
0
|
while ((i = reader.read(chars)) != -1) {
|
463
|
0
|
out.write(chars, 0, i);
|
464
|
|
}
|
465
|
0
|
reader.close();
|
466
|
|
}
|
467
|
|
else {
|
468
|
0
|
out.write(toString(object));
|
469
|
|
}
|
470
|
|
}
|
471
|
|
|
472
|
0
|
public static Object box(boolean value) {
|
473
|
0
|
return value ? Boolean.TRUE : Boolean.FALSE;
|
474
|
|
}
|
475
|
|
|
476
|
0
|
public static Object box(byte value) {
|
477
|
0
|
return new Byte(value);
|
478
|
|
}
|
479
|
|
|
480
|
0
|
public static Object box(char value) {
|
481
|
0
|
return new Character(value);
|
482
|
|
}
|
483
|
|
|
484
|
0
|
public static Object box(short value) {
|
485
|
0
|
return new Short(value);
|
486
|
|
}
|
487
|
|
|
488
|
0
|
public static Object box(int value) {
|
489
|
0
|
return integerValue(value);
|
490
|
|
}
|
491
|
|
|
492
|
0
|
public static Object box(long value) {
|
493
|
0
|
return new Long(value);
|
494
|
|
}
|
495
|
|
|
496
|
0
|
public static Object box(float value) {
|
497
|
0
|
return new Float(value);
|
498
|
|
}
|
499
|
|
|
500
|
0
|
public static Object box(double value) {
|
501
|
0
|
return new Double(value);
|
502
|
|
}
|
503
|
|
|
504
|
0
|
public static byte byteUnbox(Object value) {
|
505
|
0
|
Number n = (Number) asType(value, Byte.class);
|
506
|
0
|
return n.byteValue();
|
507
|
|
}
|
508
|
|
|
509
|
0
|
public static char charUnbox(Object value) {
|
510
|
0
|
Character n = (Character) asType(value, Character.class);
|
511
|
0
|
return n.charValue();
|
512
|
|
}
|
513
|
|
|
514
|
0
|
public static short shortUnbox(Object value) {
|
515
|
0
|
Number n = (Number) asType(value, Short.class);
|
516
|
0
|
return n.shortValue();
|
517
|
|
}
|
518
|
|
|
519
|
0
|
public static int intUnbox(Object value) {
|
520
|
0
|
Number n = (Number) asType(value, Integer.class);
|
521
|
0
|
return n.intValue();
|
522
|
|
}
|
523
|
|
|
524
|
0
|
public static boolean booleanUnbox(Object value) {
|
525
|
0
|
Boolean n = (Boolean) asType(value, Boolean.class);
|
526
|
0
|
return n.booleanValue();
|
527
|
|
}
|
528
|
|
|
529
|
0
|
public static long longUnbox(Object value) {
|
530
|
0
|
Number n = (Number) asType(value, Long.class);
|
531
|
0
|
return n.longValue();
|
532
|
|
}
|
533
|
|
|
534
|
0
|
public static float floatUnbox(Object value) {
|
535
|
0
|
Number n = (Number) asType(value, Float.class);
|
536
|
0
|
return n.floatValue();
|
537
|
|
}
|
538
|
|
|
539
|
0
|
public static double doubleUnbox(Object value) {
|
540
|
0
|
Number n = (Number) asType(value, Double.class);
|
541
|
0
|
return n.doubleValue();
|
542
|
|
}
|
543
|
|
|
544
|
|
|
545
|
|
|
546
|
|
|
547
|
|
|
548
|
|
|
549
|
|
|
550
|
0
|
public static Object[] convertPrimitiveArray(Object a, Class type) {
|
551
|
|
|
552
|
0
|
Object[] ans = null;
|
553
|
0
|
String elemType = type.getName();
|
554
|
0
|
if (elemType.equals("int")) {
|
555
|
|
|
556
|
0
|
if (a.getClass().getName().equals("[Ljava.lang.Integer;")) {
|
557
|
0
|
ans = (Integer[])a;
|
558
|
|
}
|
559
|
|
else {
|
560
|
0
|
int[] ia = (int[])a;
|
561
|
0
|
ans = new Integer[ia.length];
|
562
|
0
|
for (int i = 0; i < ia.length; i++) {
|
563
|
0
|
int e = ia[i];
|
564
|
0
|
ans[i] = integerValue(e);
|
565
|
|
}
|
566
|
|
}
|
567
|
|
}
|
568
|
0
|
else if(elemType.equals("char")) {
|
569
|
0
|
if (a.getClass().getName().equals("[Ljava.lang.Character;")) {
|
570
|
0
|
ans = (Character[])a;
|
571
|
|
}
|
572
|
|
else {
|
573
|
0
|
char[] ia = (char[])a;
|
574
|
0
|
ans = new Character[ia.length];
|
575
|
0
|
for (int i = 0; i < ia.length; i++) {
|
576
|
0
|
char e = ia[i];
|
577
|
0
|
ans[i] = new Character(e);
|
578
|
|
}
|
579
|
|
}
|
580
|
|
}
|
581
|
0
|
else if(elemType.equals("boolean")) {
|
582
|
0
|
if (a.getClass().getName().equals("[Ljava.lang.Boolean;")) {
|
583
|
0
|
ans = (Boolean[])a;
|
584
|
|
}
|
585
|
|
else {
|
586
|
0
|
boolean[] ia = (boolean[])a;
|
587
|
0
|
ans = new Boolean[ia.length];
|
588
|
0
|
for (int i = 0; i < ia.length; i++) {
|
589
|
0
|
boolean e = ia[i];
|
590
|
0
|
ans[i] = new Boolean(e);
|
591
|
|
}
|
592
|
|
}
|
593
|
|
}
|
594
|
0
|
else if(elemType.equals("byte")) {
|
595
|
0
|
if (a.getClass().getName().equals("[Ljava.lang.Byte;")) {
|
596
|
0
|
ans = (Byte[])a;
|
597
|
|
}
|
598
|
|
else {
|
599
|
0
|
byte[] ia = (byte[])a;
|
600
|
0
|
ans = new Byte[ia.length];
|
601
|
0
|
for (int i = 0; i < ia.length; i++) {
|
602
|
0
|
byte e = ia[i];
|
603
|
0
|
ans[i] = new Byte(e);
|
604
|
|
}
|
605
|
|
}
|
606
|
|
}
|
607
|
0
|
else if(elemType.equals("short")) {
|
608
|
0
|
if (a.getClass().getName().equals("[Ljava.lang.Short;")) {
|
609
|
0
|
ans = (Short[])a;
|
610
|
|
}
|
611
|
|
else {
|
612
|
0
|
short[] ia = (short[])a;
|
613
|
0
|
ans = new Short[ia.length];
|
614
|
0
|
for (int i = 0; i < ia.length; i++) {
|
615
|
0
|
short e = ia[i];
|
616
|
0
|
ans[i] = new Short(e);
|
617
|
|
}
|
618
|
|
}
|
619
|
|
}
|
620
|
0
|
else if(elemType.equals("float")) {
|
621
|
0
|
if (a.getClass().getName().equals("[Ljava.lang.Float;")) {
|
622
|
0
|
ans = (Float[])a;
|
623
|
|
}
|
624
|
|
else {
|
625
|
0
|
float[] ia = (float[])a;
|
626
|
0
|
ans = new Float[ia.length];
|
627
|
0
|
for (int i = 0; i < ia.length; i++) {
|
628
|
0
|
float e = ia[i];
|
629
|
0
|
ans[i] = new Float(e);
|
630
|
|
}
|
631
|
|
}
|
632
|
|
}
|
633
|
0
|
else if(elemType.equals("long")) {
|
634
|
0
|
if (a.getClass().getName().equals("[Ljava.lang.Long;")) {
|
635
|
0
|
ans = (Long[])a;
|
636
|
|
}
|
637
|
|
else {
|
638
|
0
|
long[] ia = (long[])a;
|
639
|
0
|
ans = new Long[ia.length];
|
640
|
0
|
for (int i = 0; i < ia.length; i++) {
|
641
|
0
|
long e = ia[i];
|
642
|
0
|
ans[i] = new Long(e);
|
643
|
|
}
|
644
|
|
}
|
645
|
|
}
|
646
|
0
|
else if(elemType.equals("double")) {
|
647
|
0
|
if (a.getClass().getName().equals("[Ljava.lang.Double;")) {
|
648
|
0
|
ans = (Double[])a;
|
649
|
|
}
|
650
|
|
else {
|
651
|
0
|
double[] ia = (double[])a;
|
652
|
0
|
ans = new Double[ia.length];
|
653
|
0
|
for (int i = 0; i < ia.length; i++) {
|
654
|
0
|
double e = ia[i];
|
655
|
0
|
ans[i] = new Double(e);
|
656
|
|
}
|
657
|
|
}
|
658
|
|
}
|
659
|
0
|
return ans;
|
660
|
|
}
|
661
|
|
|
662
|
0
|
public static int[] convertToIntArray(Object a){
|
663
|
0
|
int[] ans = null;
|
664
|
|
|
665
|
|
|
666
|
0
|
if (a.getClass().getName().equals("[I")) {
|
667
|
0
|
ans = (int[])a;
|
668
|
|
}
|
669
|
|
else {
|
670
|
0
|
Object[] ia = (Object[])a;
|
671
|
0
|
ans = new int[ia.length];
|
672
|
0
|
for (int i = 0; i < ia.length; i++) {
|
673
|
0
|
ans[i] = ((Number)ia[i]).intValue();
|
674
|
|
}
|
675
|
|
}
|
676
|
0
|
return ans;
|
677
|
|
}
|
678
|
|
|
679
|
0
|
public static boolean[] convertToBooleanArray(Object a){
|
680
|
0
|
boolean[] ans = null;
|
681
|
|
|
682
|
|
|
683
|
0
|
if (a.getClass().getName().equals("[Z")) {
|
684
|
0
|
ans = (boolean[])a;
|
685
|
|
}
|
686
|
|
else {
|
687
|
0
|
Object[] ia = (Object[])a;
|
688
|
0
|
ans = new boolean[ia.length];
|
689
|
0
|
for (int i = 0; i < ia.length; i++) {
|
690
|
0
|
ans[i] = ((Boolean)ia[i]).booleanValue();
|
691
|
|
}
|
692
|
|
}
|
693
|
0
|
return ans;
|
694
|
|
}
|
695
|
0
|
public static byte[] convertToByteArray(Object a){
|
696
|
0
|
byte[] ans = null;
|
697
|
|
|
698
|
|
|
699
|
0
|
if (a.getClass().getName().equals("[B")) {
|
700
|
0
|
ans = (byte[])a;
|
701
|
|
}
|
702
|
|
else {
|
703
|
0
|
Object[] ia = (Object[])a;
|
704
|
0
|
ans = new byte[ia.length];
|
705
|
0
|
for (int i = 0; i < ia.length; i++) {
|
706
|
0
|
ans[i] = ((Number)ia[i]).byteValue();
|
707
|
|
}
|
708
|
|
}
|
709
|
0
|
return ans;
|
710
|
|
}
|
711
|
0
|
public static short[] convertToShortArray(Object a){
|
712
|
0
|
short[] ans = null;
|
713
|
|
|
714
|
|
|
715
|
0
|
if (a.getClass().getName().equals("[S")) {
|
716
|
0
|
ans = (short[])a;
|
717
|
|
}
|
718
|
|
else {
|
719
|
0
|
Object[] ia = (Object[])a;
|
720
|
0
|
ans = new short[ia.length];
|
721
|
0
|
for (int i = 0; i < ia.length; i++) {
|
722
|
0
|
ans[i] = ((Number)ia[i]).shortValue();
|
723
|
|
}
|
724
|
|
}
|
725
|
0
|
return ans;
|
726
|
|
}
|
727
|
|
|
728
|
0
|
public static char[] convertToCharArray(Object a){
|
729
|
0
|
char[] ans = null;
|
730
|
|
|
731
|
|
|
732
|
0
|
if (a.getClass().getName().equals("[C")) {
|
733
|
0
|
ans = (char[])a;
|
734
|
|
}
|
735
|
|
else {
|
736
|
0
|
Object[] ia = (Object[])a;
|
737
|
0
|
ans = new char[ia.length];
|
738
|
0
|
for (int i = 0; i < ia.length; i++) {
|
739
|
0
|
ans[i] = ((Character)ia[i]).charValue();
|
740
|
|
}
|
741
|
|
}
|
742
|
0
|
return ans;
|
743
|
|
}
|
744
|
|
|
745
|
0
|
public static long[] convertToLongArray(Object a){
|
746
|
0
|
long[] ans = null;
|
747
|
|
|
748
|
|
|
749
|
0
|
if (a.getClass().getName().equals("[J")) {
|
750
|
0
|
ans = (long[])a;
|
751
|
|
}
|
752
|
|
else {
|
753
|
0
|
Object[] ia = (Object[])a;
|
754
|
0
|
ans = new long[ia.length];
|
755
|
0
|
for (int i = 0; i < ia.length; i++) {
|
756
|
0
|
ans[i] = ((Number)ia[i]).longValue();
|
757
|
|
}
|
758
|
|
}
|
759
|
0
|
return ans;
|
760
|
|
}
|
761
|
|
|
762
|
0
|
public static float[] convertToFloatArray(Object a){
|
763
|
0
|
float[] ans = null;
|
764
|
|
|
765
|
|
|
766
|
0
|
if (a.getClass().getName().equals("[F")) {
|
767
|
0
|
ans = (float[])a;
|
768
|
|
}
|
769
|
|
else {
|
770
|
0
|
Object[] ia = (Object[])a;
|
771
|
0
|
ans = new float[ia.length];
|
772
|
0
|
for (int i = 0; i < ia.length; i++) {
|
773
|
0
|
ans[i] = ((Number)ia[i]).floatValue();
|
774
|
|
}
|
775
|
|
}
|
776
|
0
|
return ans;
|
777
|
|
}
|
778
|
|
|
779
|
0
|
public static double[] convertToDoubleArray(Object a){
|
780
|
0
|
double[] ans = null;
|
781
|
|
|
782
|
|
|
783
|
0
|
if (a.getClass().getName().equals("[D")) {
|
784
|
0
|
ans = (double[])a;
|
785
|
|
}
|
786
|
|
else {
|
787
|
0
|
Object[] ia = (Object[])a;
|
788
|
0
|
ans = new double[ia.length];
|
789
|
0
|
for (int i = 0; i < ia.length; i++) {
|
790
|
0
|
ans[i] = ((Number)ia[i]).doubleValue();
|
791
|
|
}
|
792
|
|
}
|
793
|
0
|
return ans;
|
794
|
|
}
|
795
|
|
|
796
|
0
|
public static Object convertToPrimitiveArray(Object a, Class type) {
|
797
|
0
|
if (type == Byte.TYPE)
|
798
|
0
|
return convertToByteArray(a);
|
799
|
0
|
if (type == Boolean.TYPE)
|
800
|
0
|
return convertToBooleanArray(a);
|
801
|
0
|
if (type == Short.TYPE)
|
802
|
0
|
return convertToShortArray(a);
|
803
|
0
|
if (type == Character.TYPE)
|
804
|
0
|
return convertToCharArray(a);
|
805
|
0
|
if (type == Integer.TYPE)
|
806
|
0
|
return convertToIntArray(a);
|
807
|
0
|
if (type == Long.TYPE)
|
808
|
0
|
return convertToLongArray(a);
|
809
|
0
|
if (type == Float.TYPE)
|
810
|
0
|
return convertToFloatArray(a);
|
811
|
0
|
if (type == Double.TYPE)
|
812
|
0
|
return convertToDoubleArray(a);
|
813
|
|
else
|
814
|
0
|
return a;
|
815
|
|
}
|
816
|
|
|
817
|
|
|
818
|
|
|
819
|
|
|
820
|
|
|
821
|
|
|
822
|
0
|
public static Integer integerValue(int v) {
|
823
|
0
|
int index = v + INT_CACHE_OFFSET;
|
824
|
0
|
if (index >= 0 && index < INT_CACHE_LEN ) {
|
825
|
0
|
return SMALL_INTEGERS[index];
|
826
|
|
}
|
827
|
|
else {
|
828
|
0
|
return new Integer(v);
|
829
|
|
}
|
830
|
|
}
|
831
|
|
|
832
|
|
private static Integer[] SMALL_INTEGERS;
|
833
|
|
private static int INT_CACHE_OFFSET = 128, INT_CACHE_LEN = 256;
|
834
|
|
static {
|
835
|
0
|
SMALL_INTEGERS = new Integer[INT_CACHE_LEN];
|
836
|
0
|
for (int i = 0; i < SMALL_INTEGERS.length; i++) {
|
837
|
0
|
SMALL_INTEGERS[i] = new Integer(i - INT_CACHE_OFFSET);
|
838
|
|
}
|
839
|
|
}
|
840
|
|
}
|
841
|
|
|