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.GroovyObject;
|
49
|
|
import groovy.lang.GroovyRuntimeException;
|
50
|
|
import groovy.lang.MetaClass;
|
51
|
|
import groovy.lang.MetaClassRegistry;
|
52
|
|
import groovy.lang.Range;
|
53
|
|
import groovy.lang.Tuple;
|
54
|
|
|
55
|
|
import java.io.File;
|
56
|
|
import java.io.IOException;
|
57
|
|
import java.io.StringWriter;
|
58
|
|
import java.lang.reflect.Method;
|
59
|
|
import java.security.AccessController;
|
60
|
|
import java.security.PrivilegedAction;
|
61
|
|
import java.util.ArrayList;
|
62
|
|
import java.util.Arrays;
|
63
|
|
import java.util.Collection;
|
64
|
|
import java.util.Collections;
|
65
|
|
import java.util.Enumeration;
|
66
|
|
import java.util.Iterator;
|
67
|
|
import java.util.List;
|
68
|
|
import java.util.Map;
|
69
|
|
import java.util.NoSuchElementException;
|
70
|
|
import java.util.regex.Matcher;
|
71
|
|
import java.util.regex.Pattern;
|
72
|
|
|
73
|
|
import org.apache.xml.serialize.OutputFormat;
|
74
|
|
import org.apache.xml.serialize.XMLSerializer;
|
75
|
|
import org.w3c.dom.Element;
|
76
|
|
import org.w3c.dom.Node;
|
77
|
|
import org.w3c.dom.NodeList;
|
78
|
|
|
79
|
|
|
80
|
|
|
81
|
|
|
82
|
|
|
83
|
|
|
84
|
|
|
85
|
|
public class Invoker {
|
86
|
|
|
87
|
|
protected static final Object[] EMPTY_ARGUMENTS = {
|
88
|
|
};
|
89
|
|
protected static final Class[] EMPTY_TYPES = {
|
90
|
|
};
|
91
|
|
|
92
|
0
|
public MetaClassRegistry getMetaRegistry() {
|
93
|
0
|
return metaRegistry;
|
94
|
|
}
|
95
|
|
|
96
|
|
private MetaClassRegistry metaRegistry = new MetaClassRegistry();
|
97
|
|
|
98
|
0
|
public MetaClass getMetaClass(Object object) {
|
99
|
0
|
return metaRegistry.getMetaClass(object.getClass());
|
100
|
|
}
|
101
|
|
|
102
|
|
|
103
|
|
|
104
|
|
|
105
|
|
|
106
|
|
|
107
|
|
|
108
|
|
|
109
|
|
|
110
|
0
|
public Object invokeMethod(Object object, String methodName, Object arguments) {
|
111
|
|
|
112
|
|
|
113
|
|
|
114
|
|
|
115
|
|
|
116
|
|
|
117
|
|
|
118
|
|
|
119
|
|
|
120
|
|
|
121
|
|
|
122
|
|
|
123
|
|
|
124
|
0
|
if (object == null) {
|
125
|
0
|
throw new NullPointerException("Cannot invoke method: " + methodName + " on null object");
|
126
|
|
}
|
127
|
|
|
128
|
0
|
if (object instanceof GroovyObject) {
|
129
|
0
|
GroovyObject groovy = (GroovyObject) object;
|
130
|
0
|
return groovy.invokeMethod(methodName, arguments);
|
131
|
|
}
|
132
|
|
else {
|
133
|
0
|
if (object instanceof Class) {
|
134
|
0
|
Class theClass = (Class) object;
|
135
|
|
|
136
|
0
|
MetaClass metaClass = metaRegistry.getMetaClass(theClass);
|
137
|
0
|
return metaClass.invokeStaticMethod(object, methodName, asArray(arguments));
|
138
|
|
}
|
139
|
|
else {
|
140
|
0
|
Class theClass = object.getClass();
|
141
|
|
|
142
|
0
|
MetaClass metaClass = metaRegistry.getMetaClass(theClass);
|
143
|
0
|
return metaClass.invokeMethod(object, methodName, asArray(arguments));
|
144
|
|
}
|
145
|
|
}
|
146
|
|
}
|
147
|
|
|
148
|
0
|
public Object invokeSuperMethod(Object object, String methodName, Object arguments) {
|
149
|
0
|
if (object == null) {
|
150
|
0
|
throw new NullPointerException("Cannot invoke method: " + methodName + " on null object");
|
151
|
|
}
|
152
|
|
|
153
|
0
|
Class theClass = object.getClass();
|
154
|
|
|
155
|
0
|
MetaClass metaClass = metaRegistry.getMetaClass(theClass.getSuperclass());
|
156
|
0
|
return metaClass.invokeMethod(object, methodName, asArray(arguments));
|
157
|
|
}
|
158
|
|
|
159
|
0
|
public Object invokeStaticMethod(String type, String method, Object arguments) {
|
160
|
0
|
MetaClass metaClass = metaRegistry.getMetaClass(loadClass(type));
|
161
|
0
|
List argumentList = asList(arguments);
|
162
|
0
|
return metaClass.invokeStaticMethod(null, method, asArray(arguments));
|
163
|
|
}
|
164
|
|
|
165
|
0
|
public Object invokeConstructor(String type, Object arguments) {
|
166
|
|
|
167
|
0
|
return invokeConstructorOf(loadClass(type), arguments);
|
168
|
|
}
|
169
|
|
|
170
|
0
|
public Object invokeConstructorOf(Class type, Object arguments) {
|
171
|
0
|
MetaClass metaClass = metaRegistry.getMetaClass(type);
|
172
|
0
|
return metaClass.invokeConstructor(asArray(arguments));
|
173
|
|
}
|
174
|
|
|
175
|
|
|
176
|
|
|
177
|
|
|
178
|
|
|
179
|
0
|
public Object[] asArray(Object arguments) {
|
180
|
0
|
if (arguments == null) {
|
181
|
0
|
return EMPTY_ARGUMENTS;
|
182
|
|
}
|
183
|
0
|
if (arguments instanceof Tuple) {
|
184
|
0
|
Tuple tuple = (Tuple) arguments;
|
185
|
0
|
return tuple.toArray();
|
186
|
|
}
|
187
|
0
|
if (arguments instanceof Object[]) {
|
188
|
0
|
return (Object[]) arguments;
|
189
|
|
}
|
190
|
|
else {
|
191
|
0
|
return new Object[] { arguments };
|
192
|
|
}
|
193
|
|
}
|
194
|
|
|
195
|
0
|
public List asList(Object value) {
|
196
|
0
|
if (value == null) {
|
197
|
0
|
return Collections.EMPTY_LIST;
|
198
|
|
}
|
199
|
0
|
else if (value instanceof List) {
|
200
|
0
|
return (List) value;
|
201
|
|
}
|
202
|
0
|
else if (value.getClass().isArray()) {
|
203
|
0
|
return Arrays.asList((Object[]) value);
|
204
|
|
}
|
205
|
0
|
else if (value instanceof Enumeration) {
|
206
|
0
|
List answer = new ArrayList();
|
207
|
0
|
for (Enumeration e = (Enumeration) value; e.hasMoreElements();) {
|
208
|
0
|
answer.add(e.nextElement());
|
209
|
|
}
|
210
|
0
|
return answer;
|
211
|
|
}
|
212
|
|
else {
|
213
|
|
|
214
|
0
|
return Collections.singletonList(value);
|
215
|
|
}
|
216
|
|
}
|
217
|
|
|
218
|
|
|
219
|
|
|
220
|
|
|
221
|
|
|
222
|
0
|
public Collection asCollection(Object value) {
|
223
|
0
|
if (value == null) {
|
224
|
0
|
return Collections.EMPTY_LIST;
|
225
|
|
}
|
226
|
0
|
else if (value instanceof Collection) {
|
227
|
0
|
return (Collection) value;
|
228
|
|
}
|
229
|
0
|
else if (value instanceof Map) {
|
230
|
0
|
Map map = (Map) value;
|
231
|
0
|
return map.entrySet();
|
232
|
|
}
|
233
|
0
|
else if (value.getClass().isArray()) {
|
234
|
0
|
if (value.getClass().getComponentType().isPrimitive()) {
|
235
|
0
|
return InvokerHelper.primitiveArrayToList(value);
|
236
|
|
}
|
237
|
0
|
return Arrays.asList((Object[]) value);
|
238
|
|
}
|
239
|
0
|
else if (value instanceof MethodClosure) {
|
240
|
0
|
MethodClosure method = (MethodClosure) value;
|
241
|
0
|
IteratorClosureAdapter adapter = new IteratorClosureAdapter(method.getDelegate());
|
242
|
0
|
method.call(adapter);
|
243
|
0
|
return adapter.asList();
|
244
|
|
}
|
245
|
0
|
else if (value instanceof String) {
|
246
|
0
|
return DefaultGroovyMethods.toList((String) value);
|
247
|
|
}
|
248
|
0
|
else if (value instanceof File) {
|
249
|
0
|
try {
|
250
|
0
|
return DefaultGroovyMethods.readLines((File) value);
|
251
|
|
}
|
252
|
|
catch (IOException e) {
|
253
|
0
|
throw new GroovyRuntimeException("Error reading file: " + value, e);
|
254
|
|
}
|
255
|
|
}
|
256
|
|
else {
|
257
|
|
|
258
|
0
|
return Collections.singletonList(value);
|
259
|
|
}
|
260
|
|
}
|
261
|
|
|
262
|
0
|
public Iterator asIterator(Object value) {
|
263
|
0
|
if (value == null) {
|
264
|
0
|
Collections.EMPTY_LIST.iterator();
|
265
|
|
}
|
266
|
0
|
if (value instanceof Iterator) {
|
267
|
0
|
return (Iterator) value;
|
268
|
|
}
|
269
|
0
|
if (value instanceof NodeList) {
|
270
|
0
|
final NodeList nodeList = (NodeList) value;
|
271
|
0
|
return new Iterator() {
|
272
|
|
private int current = 0;
|
273
|
0
|
public boolean hasNext() {
|
274
|
0
|
return current < nodeList.getLength();
|
275
|
|
}
|
276
|
0
|
public Object next() {
|
277
|
0
|
Node node = nodeList.item(current++);
|
278
|
0
|
return node;
|
279
|
|
}
|
280
|
0
|
public void remove() {
|
281
|
0
|
throw new UnsupportedOperationException("Cannot remove() from an Enumeration");
|
282
|
|
}
|
283
|
|
};
|
284
|
|
}
|
285
|
0
|
else if (value instanceof Enumeration) {
|
286
|
0
|
final Enumeration enumeration = (Enumeration) value;
|
287
|
0
|
return new Iterator() {
|
288
|
|
private Object last;
|
289
|
|
|
290
|
0
|
public boolean hasNext() {
|
291
|
0
|
return enumeration.hasMoreElements();
|
292
|
|
}
|
293
|
|
|
294
|
0
|
public Object next() {
|
295
|
0
|
last = enumeration.nextElement();
|
296
|
0
|
return last;
|
297
|
|
}
|
298
|
|
|
299
|
0
|
public void remove() {
|
300
|
0
|
throw new UnsupportedOperationException("Cannot remove() from an Enumeration");
|
301
|
|
}
|
302
|
|
};
|
303
|
|
}
|
304
|
0
|
else if (value instanceof Matcher) {
|
305
|
0
|
final Matcher matcher = (Matcher) value;
|
306
|
0
|
return new Iterator() {
|
307
|
|
private boolean found = false;
|
308
|
|
private boolean done = false;
|
309
|
0
|
public boolean hasNext() {
|
310
|
0
|
if (done)
|
311
|
0
|
return false;
|
312
|
0
|
if (!found) {
|
313
|
0
|
found = matcher.find();
|
314
|
0
|
if (!found)
|
315
|
0
|
done = true;
|
316
|
|
}
|
317
|
0
|
return found;
|
318
|
|
}
|
319
|
0
|
public Object next() {
|
320
|
0
|
if (!found) {
|
321
|
0
|
if (!hasNext()) {
|
322
|
0
|
throw new NoSuchElementException();
|
323
|
|
}
|
324
|
|
}
|
325
|
0
|
found = false;
|
326
|
0
|
return matcher.group();
|
327
|
|
}
|
328
|
0
|
public void remove() {
|
329
|
0
|
throw new UnsupportedOperationException();
|
330
|
|
}
|
331
|
|
};
|
332
|
|
}
|
333
|
|
else {
|
334
|
0
|
try {
|
335
|
|
|
336
|
0
|
final Method method = value.getClass().getMethod("iterator", EMPTY_TYPES);
|
337
|
|
|
338
|
0
|
if (method != null) {
|
339
|
0
|
AccessController.doPrivileged(new PrivilegedAction() {
|
340
|
0
|
public Object run() {
|
341
|
0
|
method.setAccessible(true);
|
342
|
0
|
return null;
|
343
|
|
}
|
344
|
|
});
|
345
|
|
|
346
|
0
|
return (Iterator) method.invoke(value, EMPTY_ARGUMENTS);
|
347
|
|
}
|
348
|
|
}
|
349
|
|
catch (Exception e) {
|
350
|
|
|
351
|
|
}
|
352
|
|
}
|
353
|
0
|
return asCollection(value).iterator();
|
354
|
|
}
|
355
|
|
|
356
|
|
|
357
|
|
|
358
|
|
|
359
|
0
|
public boolean objectsEqual(Object left, Object right) {
|
360
|
0
|
if (left == right) {
|
361
|
0
|
return true;
|
362
|
|
}
|
363
|
0
|
if (left != null) {
|
364
|
0
|
if (right == null) {
|
365
|
0
|
return false;
|
366
|
|
}
|
367
|
0
|
if (left instanceof Comparable) {
|
368
|
0
|
return compareTo(left, right) == 0;
|
369
|
|
}
|
370
|
|
else {
|
371
|
0
|
return left.equals(right);
|
372
|
|
}
|
373
|
|
}
|
374
|
0
|
return false;
|
375
|
|
}
|
376
|
|
|
377
|
0
|
public String inspect(Object self) {
|
378
|
0
|
return format(self, true);
|
379
|
|
}
|
380
|
|
|
381
|
|
|
382
|
|
|
383
|
|
|
384
|
0
|
public int compareTo(Object left, Object right) {
|
385
|
|
|
386
|
0
|
if (left == right) {
|
387
|
0
|
return 0;
|
388
|
|
}
|
389
|
0
|
if (left == null) {
|
390
|
0
|
return -1;
|
391
|
|
}
|
392
|
0
|
else if (right == null) {
|
393
|
0
|
return 1;
|
394
|
|
}
|
395
|
0
|
if (left instanceof Comparable) {
|
396
|
0
|
if (left instanceof Number) {
|
397
|
0
|
if (isValidCharacterString(right)) {
|
398
|
0
|
return asCharacter((Number) left).compareTo(asCharacter((String) right));
|
399
|
|
}
|
400
|
0
|
return DefaultGroovyMethods.compareTo((Number) left, asNumber(right));
|
401
|
|
}
|
402
|
0
|
else if (left instanceof Character) {
|
403
|
0
|
if (isValidCharacterString(right)) {
|
404
|
0
|
return ((Character) left).compareTo(asCharacter((String) right));
|
405
|
|
}
|
406
|
0
|
else if (right instanceof Number) {
|
407
|
0
|
return ((Character) left).compareTo(asCharacter((Number) right));
|
408
|
|
}
|
409
|
|
}
|
410
|
0
|
else if (right instanceof Number) {
|
411
|
0
|
if (isValidCharacterString(left)) {
|
412
|
0
|
return asCharacter((String) left).compareTo(asCharacter((Number) right));
|
413
|
|
}
|
414
|
0
|
return DefaultGroovyMethods.compareTo(asNumber(left), (Number) right);
|
415
|
|
}
|
416
|
0
|
else if (left instanceof String && right instanceof Character) {
|
417
|
0
|
return ((String)left).compareTo(right.toString());
|
418
|
|
}
|
419
|
0
|
Comparable comparable = (Comparable) left;
|
420
|
0
|
return comparable.compareTo(right);
|
421
|
|
}
|
422
|
0
|
if (left.getClass().isArray()) {
|
423
|
0
|
Collection leftList = asCollection(left);
|
424
|
0
|
if (right.getClass().isArray()) {
|
425
|
0
|
right = asCollection(right);
|
426
|
|
}
|
427
|
0
|
return ((Comparable) leftList).compareTo(right);
|
428
|
|
}
|
429
|
|
|
430
|
0
|
throw new GroovyRuntimeException("Cannot compare values: " + left + " and " + right);
|
431
|
|
}
|
432
|
|
|
433
|
|
|
434
|
|
|
435
|
|
|
436
|
|
|
437
|
0
|
public String toString(Object arguments) {
|
438
|
0
|
return format(arguments, false);
|
439
|
|
}
|
440
|
|
|
441
|
|
|
442
|
|
|
443
|
|
|
444
|
0
|
public String toTypeString(Object[] arguments) {
|
445
|
0
|
if (arguments == null) {
|
446
|
0
|
return "null";
|
447
|
|
}
|
448
|
0
|
StringBuffer argBuf = new StringBuffer();
|
449
|
0
|
for (int i = 0; i < arguments.length; i++) {
|
450
|
0
|
if (i>0)
|
451
|
0
|
argBuf.append(", ");
|
452
|
0
|
argBuf.append(arguments[i] != null ? arguments[i].getClass().getName() : "null");
|
453
|
|
}
|
454
|
0
|
return argBuf.toString();
|
455
|
|
}
|
456
|
|
|
457
|
0
|
protected String format(Object arguments, boolean verbose) {
|
458
|
0
|
if (arguments == null) {
|
459
|
0
|
return "null";
|
460
|
|
}
|
461
|
0
|
else if (arguments.getClass().isArray()) {
|
462
|
0
|
return format(asCollection(arguments), verbose);
|
463
|
|
}
|
464
|
0
|
else if (arguments instanceof Range) {
|
465
|
0
|
Range range = (Range) arguments;
|
466
|
0
|
if (verbose) {
|
467
|
0
|
return range.inspect();
|
468
|
|
}
|
469
|
|
else {
|
470
|
0
|
return range.toString();
|
471
|
|
}
|
472
|
|
}
|
473
|
0
|
else if (arguments instanceof List) {
|
474
|
0
|
List list = (List) arguments;
|
475
|
0
|
StringBuffer buffer = new StringBuffer("[");
|
476
|
0
|
boolean first = true;
|
477
|
0
|
for (Iterator iter = list.iterator(); iter.hasNext();) {
|
478
|
0
|
if (first) {
|
479
|
0
|
first = false;
|
480
|
|
}
|
481
|
|
else {
|
482
|
0
|
buffer.append(", ");
|
483
|
|
}
|
484
|
0
|
buffer.append(format(iter.next(), verbose));
|
485
|
|
}
|
486
|
0
|
buffer.append("]");
|
487
|
0
|
return buffer.toString();
|
488
|
|
}
|
489
|
0
|
else if (arguments instanceof Map) {
|
490
|
0
|
Map map = (Map) arguments;
|
491
|
0
|
if (map.isEmpty()) {
|
492
|
0
|
return "[:]";
|
493
|
|
}
|
494
|
0
|
StringBuffer buffer = new StringBuffer("[");
|
495
|
0
|
boolean first = true;
|
496
|
0
|
for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
|
497
|
0
|
if (first) {
|
498
|
0
|
first = false;
|
499
|
|
}
|
500
|
|
else {
|
501
|
0
|
buffer.append(", ");
|
502
|
|
}
|
503
|
0
|
Map.Entry entry = (Map.Entry) iter.next();
|
504
|
0
|
buffer.append(format(entry.getKey(), verbose));
|
505
|
0
|
buffer.append(":");
|
506
|
0
|
buffer.append(format(entry.getValue(), verbose));
|
507
|
|
}
|
508
|
0
|
buffer.append("]");
|
509
|
0
|
return buffer.toString();
|
510
|
|
}
|
511
|
0
|
else if (arguments instanceof Element) {
|
512
|
0
|
Element node = (Element) arguments;
|
513
|
0
|
OutputFormat format = new OutputFormat(node.getOwnerDocument());
|
514
|
0
|
format.setOmitXMLDeclaration(true);
|
515
|
0
|
format.setIndenting(true);
|
516
|
0
|
format.setLineWidth(0);
|
517
|
0
|
format.setPreserveSpace(true);
|
518
|
0
|
StringWriter sw = new StringWriter();
|
519
|
0
|
XMLSerializer serializer = new XMLSerializer (sw, format);
|
520
|
0
|
try {
|
521
|
0
|
serializer.asDOMSerializer();
|
522
|
0
|
serializer.serialize(node);
|
523
|
|
} catch (IOException e) {
|
524
|
|
}
|
525
|
0
|
return sw.toString();
|
526
|
|
}
|
527
|
0
|
else if (arguments instanceof String) {
|
528
|
0
|
if (verbose) {
|
529
|
0
|
return "\"" + arguments + "\"";
|
530
|
|
}
|
531
|
|
else {
|
532
|
0
|
return (String) arguments;
|
533
|
|
}
|
534
|
|
}
|
535
|
|
else {
|
536
|
0
|
return arguments.toString();
|
537
|
|
}
|
538
|
|
}
|
539
|
|
|
540
|
|
|
541
|
|
|
542
|
|
|
543
|
|
|
544
|
|
|
545
|
|
|
546
|
|
|
547
|
|
|
548
|
0
|
public void setProperty(Object object, String property, Object newValue) {
|
549
|
0
|
if (object == null) {
|
550
|
0
|
throw new GroovyRuntimeException("Cannot set property on null object");
|
551
|
|
}
|
552
|
0
|
else if (object instanceof GroovyObject) {
|
553
|
0
|
GroovyObject pogo = (GroovyObject) object;
|
554
|
0
|
pogo.setProperty(property, newValue);
|
555
|
|
}
|
556
|
0
|
else if (object instanceof Map) {
|
557
|
0
|
Map map = (Map) object;
|
558
|
0
|
map.put(property, newValue);
|
559
|
|
}
|
560
|
|
else {
|
561
|
0
|
metaRegistry.getMetaClass(object.getClass()).setProperty(object, property, newValue);
|
562
|
|
}
|
563
|
|
}
|
564
|
|
|
565
|
|
|
566
|
|
|
567
|
|
|
568
|
|
|
569
|
|
|
570
|
|
|
571
|
|
|
572
|
0
|
public Object getProperty(Object object, String property) {
|
573
|
0
|
if (object == null) {
|
574
|
0
|
throw new NullPointerException("Cannot get property: " + property + " on null object");
|
575
|
|
}
|
576
|
0
|
else if (object instanceof GroovyObject) {
|
577
|
0
|
GroovyObject pogo = (GroovyObject) object;
|
578
|
0
|
return pogo.getProperty(property);
|
579
|
|
}
|
580
|
0
|
else if (object instanceof Map) {
|
581
|
0
|
Map map = (Map) object;
|
582
|
0
|
return map.get(property);
|
583
|
|
}
|
584
|
|
else {
|
585
|
0
|
return metaRegistry.getMetaClass(object.getClass()).getProperty(object, property);
|
586
|
|
}
|
587
|
|
}
|
588
|
|
|
589
|
0
|
public int asInt(Object value) {
|
590
|
0
|
if (value instanceof Number) {
|
591
|
0
|
Number n = (Number) value;
|
592
|
0
|
return n.intValue();
|
593
|
|
}
|
594
|
0
|
throw new GroovyRuntimeException("Could not convert object: " + value + " into an int");
|
595
|
|
}
|
596
|
|
|
597
|
0
|
public Number asNumber(Object value) {
|
598
|
0
|
if (value instanceof Number) {
|
599
|
0
|
return (Number) value;
|
600
|
|
}
|
601
|
0
|
else if (value instanceof String) {
|
602
|
0
|
String s = (String) value;
|
603
|
|
|
604
|
0
|
if (s.length() == 1)
|
605
|
0
|
return new Integer(s.charAt(0));
|
606
|
|
else
|
607
|
0
|
return Double.valueOf(s);
|
608
|
|
}
|
609
|
0
|
else if (value instanceof Character) {
|
610
|
0
|
return new Integer(((Character) value).charValue());
|
611
|
|
}
|
612
|
|
else {
|
613
|
0
|
throw new GroovyRuntimeException("Could not convert object: " + value + " into a Number");
|
614
|
|
}
|
615
|
|
}
|
616
|
|
|
617
|
|
|
618
|
|
|
619
|
|
|
620
|
|
|
621
|
0
|
protected Class loadClass(String type) {
|
622
|
0
|
try {
|
623
|
0
|
return getClass().getClassLoader().loadClass(type);
|
624
|
|
}
|
625
|
|
catch (ClassNotFoundException e) {
|
626
|
0
|
try {
|
627
|
0
|
return Thread.currentThread().getContextClassLoader().loadClass(type);
|
628
|
|
}
|
629
|
|
catch (ClassNotFoundException e2) {
|
630
|
0
|
try {
|
631
|
0
|
return Class.forName(type);
|
632
|
|
}
|
633
|
|
catch (ClassNotFoundException e3) {
|
634
|
|
}
|
635
|
|
}
|
636
|
0
|
throw new GroovyRuntimeException("Could not load type: " + type, e);
|
637
|
|
}
|
638
|
|
}
|
639
|
|
|
640
|
|
|
641
|
|
|
642
|
|
|
643
|
|
|
644
|
|
|
645
|
|
|
646
|
|
|
647
|
0
|
public Matcher objectFindRegex(Object left, Object right) {
|
648
|
0
|
String stringToCompare;
|
649
|
0
|
if (left instanceof String) {
|
650
|
0
|
stringToCompare = (String) left;
|
651
|
|
}
|
652
|
|
else {
|
653
|
0
|
stringToCompare = toString(left);
|
654
|
|
}
|
655
|
0
|
String regexToCompareTo;
|
656
|
0
|
if (right instanceof String) {
|
657
|
0
|
regexToCompareTo = (String) right;
|
658
|
|
}
|
659
|
0
|
else if (right instanceof Pattern) {
|
660
|
0
|
Pattern pattern = (Pattern) right;
|
661
|
0
|
return pattern.matcher(stringToCompare);
|
662
|
|
}
|
663
|
|
else {
|
664
|
0
|
regexToCompareTo = toString(right);
|
665
|
|
}
|
666
|
0
|
Matcher matcher = Pattern.compile(regexToCompareTo).matcher(stringToCompare);
|
667
|
0
|
return matcher;
|
668
|
|
}
|
669
|
|
|
670
|
|
|
671
|
|
|
672
|
|
|
673
|
|
|
674
|
|
|
675
|
|
|
676
|
|
|
677
|
0
|
public boolean objectMatchRegex(Object left, Object right) {
|
678
|
0
|
Pattern pattern;
|
679
|
0
|
if (right instanceof Pattern) {
|
680
|
0
|
pattern = (Pattern) right;
|
681
|
|
}
|
682
|
|
else {
|
683
|
0
|
pattern = Pattern.compile(toString(right));
|
684
|
|
}
|
685
|
0
|
String stringToCompare = toString(left);
|
686
|
0
|
Matcher matcher = pattern.matcher(stringToCompare);
|
687
|
0
|
RegexSupport.setLastMatcher(matcher);
|
688
|
0
|
return matcher.matches();
|
689
|
|
}
|
690
|
|
|
691
|
|
|
692
|
|
|
693
|
|
|
694
|
|
|
695
|
|
|
696
|
|
|
697
|
0
|
public Pattern regexPattern(Object regex) {
|
698
|
0
|
return Pattern.compile(regex.toString());
|
699
|
|
}
|
700
|
|
|
701
|
0
|
public Object asType(Object object, Class type) {
|
702
|
0
|
if (object == null) {
|
703
|
0
|
return null;
|
704
|
|
}
|
705
|
0
|
if (type.isInstance(object)) {
|
706
|
0
|
return object;
|
707
|
|
}
|
708
|
0
|
if (type.equals(String.class)) {
|
709
|
0
|
return object.toString();
|
710
|
|
}
|
711
|
0
|
if (type.equals(Character.class)) {
|
712
|
0
|
if (object instanceof Number) {
|
713
|
0
|
return asCharacter((Number) object);
|
714
|
|
}
|
715
|
|
else {
|
716
|
0
|
String text = object.toString();
|
717
|
0
|
if (text.length() == 1) {
|
718
|
0
|
return new Character(text.charAt(0));
|
719
|
|
}
|
720
|
|
else {
|
721
|
0
|
throw new ClassCastException("Cannot cast: " + text + " to a Character");
|
722
|
|
}
|
723
|
|
}
|
724
|
|
}
|
725
|
0
|
if (Number.class.isAssignableFrom(type)) {
|
726
|
0
|
if (object instanceof Character) {
|
727
|
0
|
return new Integer(((Character) object).charValue());
|
728
|
|
}
|
729
|
0
|
else if (object instanceof String) {
|
730
|
0
|
String c = (String) object;
|
731
|
0
|
if (c.length() == 1) {
|
732
|
0
|
return new Integer(c.charAt(0));
|
733
|
|
}
|
734
|
|
else {
|
735
|
0
|
throw new ClassCastException("Cannot cast: '" + c + "' to an Integer");
|
736
|
|
}
|
737
|
|
}
|
738
|
|
}
|
739
|
0
|
if (object instanceof Number) {
|
740
|
0
|
Number n = (Number) object;
|
741
|
0
|
if (type.isPrimitive()) {
|
742
|
0
|
if (type == byte.class) {
|
743
|
0
|
return new Byte(n.byteValue());
|
744
|
|
}
|
745
|
0
|
if (type == char.class) {
|
746
|
0
|
return new Character((char) n.intValue());
|
747
|
|
}
|
748
|
0
|
if (type == short.class) {
|
749
|
0
|
return new Short(n.shortValue());
|
750
|
|
}
|
751
|
0
|
if (type == int.class) {
|
752
|
0
|
return new Integer(n.intValue());
|
753
|
|
}
|
754
|
0
|
if (type == long.class) {
|
755
|
0
|
return new Long(n.longValue());
|
756
|
|
}
|
757
|
0
|
if (type == float.class) {
|
758
|
0
|
return new Float(n.floatValue());
|
759
|
|
}
|
760
|
0
|
if (type == double.class) {
|
761
|
0
|
Double answer = new Double(n.doubleValue());
|
762
|
|
|
763
|
0
|
if (!(n instanceof Double) && (answer.doubleValue() == Double.NEGATIVE_INFINITY
|
764
|
|
|| answer.doubleValue() == Double.POSITIVE_INFINITY)) {
|
765
|
0
|
throw new GroovyRuntimeException("Automatic coercion of "+n.getClass().getName()
|
766
|
|
+" value "+n+" to double failed. Value is out of range.");
|
767
|
|
}
|
768
|
0
|
return answer;
|
769
|
|
}
|
770
|
|
}
|
771
|
|
else {
|
772
|
0
|
if (Number.class.isAssignableFrom(type)) {
|
773
|
0
|
if (type == Byte.class) {
|
774
|
0
|
return new Byte(n.byteValue());
|
775
|
|
}
|
776
|
0
|
if (type == Character.class) {
|
777
|
0
|
return new Character((char) n.intValue());
|
778
|
|
}
|
779
|
0
|
if (type == Short.class) {
|
780
|
0
|
return new Short(n.shortValue());
|
781
|
|
}
|
782
|
0
|
if (type == Integer.class) {
|
783
|
0
|
return new Integer(n.intValue());
|
784
|
|
}
|
785
|
0
|
if (type == Long.class) {
|
786
|
0
|
return new Long(n.longValue());
|
787
|
|
}
|
788
|
0
|
if (type == Float.class) {
|
789
|
0
|
return new Float(n.floatValue());
|
790
|
|
}
|
791
|
0
|
if (type == Double.class) {
|
792
|
0
|
Double answer = new Double(n.doubleValue());
|
793
|
|
|
794
|
0
|
if (!(n instanceof Double) && (answer.doubleValue() == Double.NEGATIVE_INFINITY
|
795
|
|
|| answer.doubleValue() == Double.POSITIVE_INFINITY)) {
|
796
|
0
|
throw new GroovyRuntimeException("Automatic coercion of "+n.getClass().getName()
|
797
|
|
+" value "+n+" to double failed. Value is out of range.");
|
798
|
|
}
|
799
|
0
|
return answer;
|
800
|
|
}
|
801
|
|
|
802
|
|
}
|
803
|
|
}
|
804
|
|
}
|
805
|
0
|
if (type == Boolean.class) {
|
806
|
0
|
return asBool(object) ? Boolean.TRUE : Boolean.FALSE;
|
807
|
|
}
|
808
|
0
|
return object;
|
809
|
|
}
|
810
|
|
|
811
|
0
|
public boolean asBool(Object object) {
|
812
|
0
|
if (object instanceof Boolean) {
|
813
|
0
|
Boolean booleanValue = (Boolean) object;
|
814
|
0
|
return booleanValue.booleanValue();
|
815
|
|
}
|
816
|
0
|
else if (object instanceof Matcher) {
|
817
|
0
|
Matcher matcher = (Matcher) object;
|
818
|
0
|
RegexSupport.setLastMatcher(matcher);
|
819
|
0
|
return matcher.find();
|
820
|
|
}
|
821
|
0
|
else if (object instanceof Collection) {
|
822
|
0
|
Collection collection = (Collection) object;
|
823
|
0
|
return !collection.isEmpty();
|
824
|
|
}
|
825
|
0
|
else if (object instanceof Number) {
|
826
|
0
|
Number n = (Number) object;
|
827
|
0
|
return n.intValue() != 0;
|
828
|
|
}
|
829
|
|
else {
|
830
|
0
|
return object != null;
|
831
|
|
}
|
832
|
|
}
|
833
|
|
|
834
|
0
|
protected Character asCharacter(Number value) {
|
835
|
0
|
return new Character((char) value.intValue());
|
836
|
|
}
|
837
|
|
|
838
|
0
|
protected Character asCharacter(String text) {
|
839
|
0
|
return new Character(text.charAt(0));
|
840
|
|
}
|
841
|
|
|
842
|
|
|
843
|
|
|
844
|
|
|
845
|
0
|
protected boolean isValidCharacterString(Object value) {
|
846
|
0
|
if (value instanceof String) {
|
847
|
0
|
String s = (String) value;
|
848
|
0
|
if (s.length() == 1) {
|
849
|
0
|
return true;
|
850
|
|
}
|
851
|
|
}
|
852
|
0
|
return false;
|
853
|
|
}
|
854
|
|
|
855
|
|
}
|
856
|
|
|