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
50 import java.util.Iterator;
51 import java.util.List;
52 import java.util.Map;
53 import java.util.regex.Matcher;
54 import java.util.regex.Pattern;
55
56 /***
57 * A static helper class to make bytecode generation easier and act as a facade over the Invoker.
58 *
59 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
60 * @version $Revision: 1.5 $
61 */
62 public class ScriptBytecodeAdapter {
63 public static final Object[] EMPTY_ARGS = {
64 };
65
66
67
68
69
70
71
72
73
74
75 private static Object unwrap(GroovyRuntimeException gre) throws Throwable{
76 Throwable th = gre;
77 if (th.getCause()!=null && th.getCause()!=gre) th=th.getCause();
78 if (th!=gre && (th instanceof GroovyRuntimeException)) unwrap((GroovyRuntimeException) th);
79 throw th;
80 }
81
82 public static Object invokeMethod(Object object, String methodName, Object arguments) throws Throwable{
83 try {
84 return InvokerHelper.invokeMethod(object, methodName, arguments);
85 } catch (GroovyRuntimeException gre) {
86 return unwrap(gre);
87 }
88 }
89
90 public static Object invokeMethodSafe(Object object, String methodName, Object arguments) throws Throwable{
91 if (object != null) return invokeMethod(object, methodName, arguments);
92 return null;
93 }
94
95 public static Object invokeStaticMethod(String type, String methodName, Object arguments) throws Throwable{
96 try {
97 return InvokerHelper.invokeStaticMethod(type, methodName, arguments);
98 } catch (GroovyRuntimeException gre) {
99 return unwrap(gre);
100 }
101 }
102
103 public static Object invokeConstructor(String type, Object arguments) throws Throwable{
104 try {
105 return InvokerHelper.invokeConstructor(type, arguments);
106 } catch (GroovyRuntimeException gre) {
107 return unwrap(gre);
108 }
109 }
110
111 public static Object invokeConstructorOf(Class type, Object arguments) throws Throwable{
112 try {
113 return InvokerHelper.invokeConstructorOf(type, arguments);
114 } catch (GroovyRuntimeException gre) {
115 return unwrap(gre);
116 }
117 }
118
119 public static Object invokeNoArgumentsConstructorOf(Class type) throws Throwable{
120 return invokeConstructorOf(type, EMPTY_ARGS);
121 }
122
123 public static Object invokeClosure(Object closure, Object arguments) throws Throwable {
124 return invokeMethod(closure, "doCall", arguments);
125 }
126
127 public static Object invokeSuperMethod(Object object, String methodName, Object arguments) throws Throwable{
128 try {
129 return InvokerHelper.invokeSuperMethod(object, methodName, arguments);
130 } catch (GroovyRuntimeException gre) {
131 return unwrap(gre);
132 }
133 }
134
135 public static Object invokeNoArgumentsMethod(Object object, String methodName) throws Throwable {
136 return invokeMethod(object, methodName, EMPTY_ARGS);
137 }
138
139 public static Object invokeStaticNoArgumentsMethod(String type, String methodName) throws Throwable {
140 return invokeStaticMethod(type, methodName, EMPTY_ARGS);
141 }
142
143 public static int asInt(Object value) throws Throwable {
144 try {
145 return InvokerHelper.asInt(value);
146 } catch (GroovyRuntimeException gre) {
147 unwrap(gre);
148
149 return -1;
150 }
151 }
152
153 /***
154 * Provides a hook for type coercion of the given object to the required type
155 *
156 * @param type of object to convert the given object to
157 * @param object the object to be converted
158 * @return the original object or a new converted value
159 * @throws Throwable
160 */
161 public static Object asType(Object object, Class type) throws Throwable {
162 try {
163 return InvokerHelper.asType(object, type);
164 } catch (GroovyRuntimeException gre) {
165 return unwrap(gre);
166 }
167 }
168
169
170
171
172
173 public static Object getAttribute(Object object, String attribute) throws Throwable {
174 try {
175 return InvokerHelper.getAttribute(object, attribute);
176 } catch (GroovyRuntimeException gre) {
177 return unwrap(gre);
178 }
179 }
180
181 public static Object getAttributeSafe(Object object, String attribute) throws Throwable {
182 if (object != null) return getAttribute(object, attribute);
183 return null;
184 }
185
186 public static void setAttribute(Object object, String attribute, Object newValue) throws Throwable {
187 try {
188 InvokerHelper.setAttribute(object, attribute, newValue);
189 } catch (GroovyRuntimeException gre) {
190 unwrap(gre);
191 }
192 }
193 /***
194 * This is so we don't have to reorder the stack when we call this method.
195 * At some point a better name might be in order.
196 * @throws Throwable
197 */
198 public static void setAttribute2(Object newValue, Object object, String property) throws Throwable {
199 setAttribute(object, property, newValue);
200 }
201
202 /***
203 * This is so we don't have to reorder the stack when we call this method.
204 * At some point a better name might be in order.
205 * @throws Throwable
206 */
207 public static void setAttributeSafe2(Object newValue, Object object, String property) throws Throwable {
208 setAttribute2(newValue, object, property);
209 }
210
211
212
213
214
215 public static Object getProperty(Object object, String property) throws Throwable {
216 try {
217 return InvokerHelper.getProperty(object, property);
218 } catch (GroovyRuntimeException gre) {
219 return unwrap(gre);
220 }
221 }
222
223 public static Object getPropertySafe(Object object, String property) throws Throwable {
224 if (object != null) return getProperty(object, property);
225 return null;
226 }
227
228 public static void setProperty(Object object, String property, Object newValue) throws Throwable {
229 try {
230 InvokerHelper.setProperty(object, property, newValue);
231 } catch (GroovyRuntimeException gre) {
232 unwrap(gre);
233 }
234 }
235
236 /***
237 * This is so we don't have to reorder the stack when we call this method.
238 * At some point a better name might be in order.
239 * @throws Throwable
240 */
241 public static void setProperty2(Object newValue, Object object, String property) throws Throwable {
242 setProperty(object, property, newValue);
243 }
244
245 /***
246 * This is so we don't have to reorder the stack when we call this method.
247 * At some point a better name might be in order.
248 * @throws Throwable
249 */
250 public static void setPropertySafe2(Object newValue, Object object, String property) throws Throwable {
251 setProperty2(newValue, object, property);
252 }
253
254
255 /***
256 * This is so we don't have to reorder the stack when we call this method.
257 * At some point a better name might be in order.
258 * @throws Throwable
259 */
260 public static void setGroovyObjectProperty(Object newValue, GroovyObject object, String property) throws Throwable {
261 try {
262 object.setProperty(property, newValue);
263 } catch (GroovyRuntimeException gre) {
264 unwrap(gre);
265 }
266 }
267
268 public static Object getGroovyObjectProperty(GroovyObject object, String property) throws Throwable {
269 try {
270 return object.getProperty(property);
271 } catch (GroovyRuntimeException gre) {
272 return unwrap(gre);
273 }
274 }
275
276
277 /***
278 * Returns the method pointer for the given object name
279 */
280 public static Closure getMethodPointer(Object object, String methodName) {
281 return InvokerHelper.getMethodPointer(object, methodName);
282 }
283
284
285
286 public static Iterator asIterator(Object collection) throws Throwable {
287 try {
288 return InvokerHelper.asIterator(collection);
289 } catch (GroovyRuntimeException gre) {
290 return (Iterator) unwrap(gre);
291 }
292 }
293
294 public static boolean asBool(Object object) throws Throwable {
295 try {
296 return InvokerHelper.asBool(object);
297 } catch (GroovyRuntimeException gre) {
298 unwrap(gre);
299
300 return false;
301 }
302 }
303
304 public static boolean notBoolean(boolean bool) {
305 return !bool;
306 }
307
308 public static boolean notObject(Object object) throws Throwable {
309 return !asBool(object);
310 }
311
312 public static Pattern regexPattern(Object regex) throws Throwable {
313 try {
314 return InvokerHelper.regexPattern(regex);
315 } catch (GroovyRuntimeException gre) {
316 return (Pattern) unwrap(gre);
317 }
318 }
319
320 public static Object spreadList(Object value) throws Throwable {
321 try {
322 return InvokerHelper.spreadList(value);
323 } catch (GroovyRuntimeException gre) {
324 return unwrap(gre);
325 }
326 }
327
328 public static Object negate(Object value) throws Throwable {
329 try {
330 return InvokerHelper.negate(value);
331 } catch (GroovyRuntimeException gre) {
332 return unwrap(gre);
333 }
334 }
335
336 public static Object bitNegate(Object value) throws Throwable {
337 try {
338 return InvokerHelper.bitNegate(value);
339 } catch (GroovyRuntimeException gre) {
340 return unwrap(gre);
341 }
342 }
343
344 /***
345 * @param a array of primitives
346 * @param type component type of the array
347 * @return
348 * @throws Throwable
349 */
350 public static Object[] convertPrimitiveArray(Object a, Class type) throws Throwable {
351 try {
352 return InvokerHelper.convertPrimitiveArray(a,type);
353 } catch (GroovyRuntimeException gre) {
354 return (Object[])unwrap(gre);
355 }
356 }
357
358 public static Object convertToPrimitiveArray(Object a, Class type) throws Throwable {
359 try {
360 return InvokerHelper.convertToPrimitiveArray(a,type);
361 } catch (GroovyRuntimeException gre) {
362 return unwrap(gre);
363 }
364 }
365
366 public static boolean compareIdentical(Object left, Object right) {
367 return left == right;
368 }
369
370 public static boolean compareEqual(Object left, Object right) throws Throwable{
371 try {
372 return InvokerHelper.compareEqual(left, right);
373 } catch (GroovyRuntimeException gre) {
374 unwrap(gre);
375
376 return false;
377 }
378 }
379
380 public static boolean compareNotEqual(Object left, Object right) throws Throwable{
381 return !compareEqual(left, right);
382 }
383
384 public static Integer compareTo(Object left, Object right) throws Throwable{
385 try {
386 return InvokerHelper.compareTo(left, right);
387 } catch (GroovyRuntimeException gre) {
388 return (Integer) unwrap(gre);
389 }
390 }
391
392 public static Matcher findRegex(Object left, Object right) throws Throwable{
393 try {
394 return InvokerHelper.findRegex(left, right);
395 } catch (GroovyRuntimeException gre) {
396 return (Matcher) unwrap(gre);
397 }
398 }
399
400 public static boolean matchRegex(Object left, Object right) throws Throwable{
401 try {
402 return InvokerHelper.matchRegex(left, right);
403 } catch (GroovyRuntimeException gre) {
404 unwrap(gre);
405
406 return false;
407 }
408 }
409
410 public static boolean compareLessThan(Object left, Object right) throws Throwable{
411 return compareTo(left, right).intValue() < 0;
412 }
413
414 public static boolean compareLessThanEqual(Object left, Object right) throws Throwable{
415 return compareTo(left, right).intValue() <= 0;
416 }
417
418 public static boolean compareGreaterThan(Object left, Object right) throws Throwable{
419 return compareTo(left, right).intValue() > 0;
420 }
421
422 public static boolean compareGreaterThanEqual(Object left, Object right) throws Throwable{
423 return compareTo(left, right).intValue() >= 0;
424 }
425
426 public static boolean isCase(Object switchValue, Object caseExpression) throws Throwable{
427 return asBool(invokeMethod(caseExpression, "isCase", new Object[]{switchValue}));
428 }
429
430 public static Tuple createTuple(Object[] array) throws Throwable{
431 return new Tuple(array);
432 }
433
434 public static List createList(Object[] values) throws Throwable{
435 return InvokerHelper.createList(values);
436 }
437
438 public static Map createMap(Object[] values) throws Throwable{
439 return InvokerHelper.createMap(values);
440 }
441
442 public static List createRange(Object from, Object to, boolean inclusive) throws Throwable{
443 try {
444 return InvokerHelper.createRange(from,to,inclusive);
445 } catch (GroovyRuntimeException gre) {
446 return (List) unwrap(gre);
447 }
448 }
449
450 public static void assertFailed(Object expression, Object message) {
451 InvokerHelper.assertFailed(expression,message);
452 }
453
454 public static Object box(boolean value) {
455 return value ? Boolean.TRUE : Boolean.FALSE;
456 }
457
458 public static Object box(byte value) {
459 return new Byte(value);
460 }
461
462 public static Object box(char value) {
463 return new Character(value);
464 }
465
466 public static Object box(short value) {
467 return new Short(value);
468 }
469
470 public static Object box(int value) {
471 return integerValue(value);
472 }
473
474 public static Object box(long value) {
475 return new Long(value);
476 }
477
478 public static Object box(float value) {
479 return new Float(value);
480 }
481
482 public static Object box(double value) {
483 return new Double(value);
484 }
485
486 /***
487 * get the Integer object from an int. Cached version is used for small ints.
488 *
489 * @param v
490 * @return
491 */
492 public static Integer integerValue(int v) {
493 return InvokerHelper.integerValue(v);
494 }
495
496 public static byte byteUnbox(Object value) throws Throwable {
497 Number n = (Number) asType(value, Byte.class);
498 return n.byteValue();
499 }
500
501 public static char charUnbox(Object value) throws Throwable {
502 Character n = (Character) asType(value, Character.class);
503 return n.charValue();
504 }
505
506 public static short shortUnbox(Object value) throws Throwable {
507 Number n = (Number) asType(value, Short.class);
508 return n.shortValue();
509 }
510
511 public static int intUnbox(Object value) throws Throwable {
512 Number n = (Number) asType(value, Integer.class);
513 return n.intValue();
514 }
515
516 public static boolean booleanUnbox(Object value) throws Throwable {
517 Boolean n = (Boolean) asType(value, Boolean.class);
518 return n.booleanValue();
519 }
520
521 public static long longUnbox(Object value) throws Throwable {
522 Number n = (Number) asType(value, Long.class);
523 return n.longValue();
524 }
525
526 public static float floatUnbox(Object value) throws Throwable {
527 Number n = (Number) asType(value, Float.class);
528 return n.floatValue();
529 }
530
531 public static double doubleUnbox(Object value) throws Throwable {
532 Number n = (Number) asType(value, Double.class);
533 return n.doubleValue();
534 }
535
536 public static MetaClass getMetaClass(Object object) {
537 return InvokerHelper.getMetaClass(object);
538 }
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604 /***
605 * Sets the properties on the given object
606 *
607 * @param object
608 * @param map
609 */
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626 /***
627 * Allows conversion of arrays into a mutable List
628 *
629 * @return the array as a List
630 */
631
632
633
634
635
636
637
638
639
640 /***
641 * Writes the given object to the given stream
642 */
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809 }