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