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.ArrayList;
53 import java.util.Map;
54 import java.util.regex.Matcher;
55 import java.util.regex.Pattern;
56
57 /***
58 * A static helper class to make bytecode generation easier and act as a facade over the Invoker.
59 *
60 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
61 * @version $Revision: 1.12 $
62 */
63 public class ScriptBytecodeAdapter {
64 public static final Object[] EMPTY_ARGS = {
65 };
66
67
68
69
70
71
72
73
74
75
76 private static Object unwrap(GroovyRuntimeException gre) throws Throwable{
77 Throwable th = gre;
78 if (th.getCause()!=null && th.getCause()!=gre) th=th.getCause();
79 if (th!=gre && (th instanceof GroovyRuntimeException)) unwrap((GroovyRuntimeException) th);
80 throw th;
81 }
82
83 public static Object invokeMethod(Object object, String methodName, Object arguments) throws Throwable{
84 try {
85 return InvokerHelper.invokeMethod(object, methodName, arguments);
86 } catch (GroovyRuntimeException gre) {
87 return unwrap(gre);
88 }
89 }
90
91 public static Object invokeMethodSafe(Object object, String methodName, Object arguments) throws Throwable{
92 if (object != null) return invokeMethod(object, methodName, arguments);
93 return null;
94 }
95
96 public static Object invokeMethodSpreadSafe(Object object, String methodName, Object arguments) throws Throwable{
97 if (object != null) {
98 if (object instanceof List) {
99 List list = (List) object;
100 List answer = new ArrayList();
101 Iterator it = list.iterator();
102 for (; it.hasNext();) {
103 answer.add(invokeMethodSafe(it.next(), methodName, arguments));
104 }
105 return answer;
106 }
107 else
108 return invokeMethodSafe(object, methodName, arguments);
109 }
110 return null;
111 }
112
113 public static Object invokeStaticMethod(String type, String methodName, Object arguments) throws Throwable{
114 try {
115 return InvokerHelper.invokeStaticMethod(type, methodName, arguments);
116 } catch (GroovyRuntimeException gre) {
117 return unwrap(gre);
118 }
119 }
120
121 public static Object invokeConstructorAt(Class at, Class type, Object arguments) throws Throwable{
122 try {
123 return InvokerHelper.invokeConstructorAt(at, type, arguments);
124 } catch (GroovyRuntimeException gre) {
125 return unwrap(gre);
126 }
127 }
128
129
130
131
132
133
134
135
136
137 public static Object invokeNoArgumentsConstructorAt(Class at, Class type) throws Throwable {
138 return invokeConstructorAt(at, type, EMPTY_ARGS);
139 }
140
141
142 public static Object invokeConstructorOf(Class type, Object arguments) throws Throwable{
143 try {
144 return InvokerHelper.invokeConstructorOf(type, arguments);
145 } catch (GroovyRuntimeException gre) {
146 return unwrap(gre);
147 }
148 }
149
150
151
152
153
154
155
156
157
158 public static Object invokeNoArgumentsConstructorOf(Class type) throws Throwable {
159 return invokeConstructorOf(type, EMPTY_ARGS);
160 }
161
162 public static Object invokeClosure(Object closure, Object arguments) throws Throwable {
163 return invokeMethod(closure, "doCall", arguments);
164 }
165
166 public static Object invokeSuperMethod(Object object, String methodName, Object arguments) throws Throwable{
167 try {
168 return InvokerHelper.invokeSuperMethod(object, methodName, arguments);
169 } catch (GroovyRuntimeException gre) {
170 return unwrap(gre);
171 }
172 }
173
174 public static Object invokeNoArgumentsMethod(Object object, String methodName) throws Throwable {
175 return invokeMethod(object, methodName, EMPTY_ARGS);
176 }
177
178 public static Object invokeNoArgumentsMethodSafe(Object object, String methodName) throws Throwable {
179 if (object != null) return invokeNoArgumentsMethod(object, methodName);
180 return null;
181 }
182
183 public static Object invokeNoArgumentsMethodSpreadSafe(Object object, String methodName) throws Throwable {
184 if (object != null) {
185 if (object instanceof List) {
186 List list = (List) object;
187 List answer = new ArrayList();
188 Iterator it = list.iterator();
189 for (; it.hasNext();) {
190 answer.add(invokeNoArgumentsMethod(it.next(), methodName));
191 }
192 return answer;
193 }
194 else
195 return invokeNoArgumentsMethod(object, methodName);
196 }
197 return null;
198 }
199
200 public static Object invokeStaticNoArgumentsMethod(String type, String methodName) throws Throwable {
201 return invokeStaticMethod(type, methodName, EMPTY_ARGS);
202 }
203
204 public static int asInt(Object value) throws Throwable {
205 try {
206 return InvokerHelper.asInt(value);
207 } catch (GroovyRuntimeException gre) {
208 unwrap(gre);
209
210 return -1;
211 }
212 }
213
214 /***
215 * Provides a hook for type coercion of the given object to the required type
216 *
217 * @param type of object to convert the given object to
218 * @param object the object to be converted
219 * @return the original object or a new converted value
220 * @throws Throwable
221 */
222 public static Object asType(Object object, Class type) throws Throwable {
223 try {
224 return InvokerHelper.asType(object, type);
225 } catch (GroovyRuntimeException gre) {
226 return unwrap(gre);
227 }
228 }
229
230
231
232
233
234 public static Object getAttribute(Object object, String attribute) throws Throwable {
235 try {
236 return InvokerHelper.getAttribute(object, attribute);
237 } catch (GroovyRuntimeException gre) {
238 return unwrap(gre);
239 }
240 }
241
242 public static Object getAttributeSafe(Object object, String attribute) throws Throwable {
243 if (object != null) return getAttribute(object, attribute);
244 return null;
245 }
246
247 public static Object getAttributeSpreadSafe(Object object, String attribute) throws Throwable {
248 if (object != null) {
249 if (object instanceof List) {
250 List list = (List) object;
251 List answer = new ArrayList();
252 Iterator it = list.iterator();
253 for (; it.hasNext(); ) {
254 answer.add(getAttributeSafe(it.next(), attribute));
255 }
256 return answer;
257 }
258 else
259 return getAttributeSafe(object, attribute);
260 }
261 return null;
262 }
263
264 public static void setAttribute(Object object, String attribute, Object newValue) throws Throwable {
265 try {
266 InvokerHelper.setAttribute(object, attribute, newValue);
267 } catch (GroovyRuntimeException gre) {
268 unwrap(gre);
269 }
270 }
271 /***
272 * This is so we don't have to reorder the stack when we call this method.
273 * At some point a better name might be in order.
274 * @throws Throwable
275 */
276 public static void setAttribute2(Object newValue, Object object, String property) throws Throwable {
277 setAttribute(object, property, newValue);
278 }
279
280 /***
281 * This is so we don't have to reorder the stack when we call this method.
282 * At some point a better name might be in order.
283 * @throws Throwable
284 */
285 public static void setAttributeSafe2(Object newValue, Object object, String property) throws Throwable {
286 setAttribute2(newValue, object, property);
287 }
288
289
290
291
292
293 public static Object getProperty(Object object, String property) throws Throwable {
294 try {
295 return InvokerHelper.getProperty(object, property);
296 } catch (GroovyRuntimeException gre) {
297 return unwrap(gre);
298 }
299 }
300
301 public static Object getPropertySafe(Object object, String property) throws Throwable {
302 if (object != null) return getProperty(object, property);
303 return null;
304 }
305
306 public static Object getPropertySpreadSafe(Object object, String property) throws Throwable {
307 if (object != null) {
308 if (object instanceof List) {
309 List list = (List) object;
310 List answer = new ArrayList();
311 Iterator it = list.iterator();
312 for (; it.hasNext(); ) {
313 answer.add(getPropertySafe(it.next(), property));
314 }
315 return answer;
316 }
317 else
318 return getPropertySafe(object, property);
319 }
320 return null;
321 }
322
323 public static void setProperty(Object object, String property, Object newValue) throws Throwable {
324 try {
325 InvokerHelper.setProperty(object, property, newValue);
326 } catch (GroovyRuntimeException gre) {
327 unwrap(gre);
328 }
329 }
330
331 /***
332 * This is so we don't have to reorder the stack when we call this method.
333 * At some point a better name might be in order.
334 * @throws Throwable
335 */
336 public static void setProperty2(Object newValue, Object object, String property) throws Throwable {
337 setProperty(object, property, newValue);
338 }
339
340 /***
341 * This is so we don't have to reorder the stack when we call this method.
342 * At some point a better name might be in order.
343 * @throws Throwable
344 */
345 public static void setPropertySafe2(Object newValue, Object object, String property) throws Throwable {
346 setProperty2(newValue, object, property);
347 }
348
349
350 /***
351 * This is so we don't have to reorder the stack when we call this method.
352 * At some point a better name might be in order.
353 * @throws Throwable
354 */
355 public static void setGroovyObjectProperty(Object newValue, GroovyObject object, String property) throws Throwable {
356 try {
357 object.setProperty(property, newValue);
358 } catch (GroovyRuntimeException gre) {
359 unwrap(gre);
360 }
361 }
362
363 public static Object getGroovyObjectProperty(GroovyObject object, String property) throws Throwable {
364 try {
365 return object.getProperty(property);
366 } catch (GroovyRuntimeException gre) {
367 return unwrap(gre);
368 }
369 }
370
371
372 /***
373 * Returns the method pointer for the given object name
374 */
375 public static Closure getMethodPointer(Object object, String methodName) {
376 return InvokerHelper.getMethodPointer(object, methodName);
377 }
378
379
380
381 public static Iterator asIterator(Object collection) throws Throwable {
382 try {
383 return InvokerHelper.asIterator(collection);
384 } catch (GroovyRuntimeException gre) {
385 return (Iterator) unwrap(gre);
386 }
387 }
388
389 public static boolean asBool(Object object) throws Throwable {
390 try {
391 return InvokerHelper.asBool(object);
392 } catch (GroovyRuntimeException gre) {
393 unwrap(gre);
394
395 return false;
396 }
397 }
398
399 public static boolean notBoolean(boolean bool) {
400 return !bool;
401 }
402
403 public static boolean notObject(Object object) throws Throwable {
404 return !asBool(object);
405 }
406
407 public static Pattern regexPattern(Object regex) throws Throwable {
408 try {
409 return InvokerHelper.regexPattern(regex);
410 } catch (GroovyRuntimeException gre) {
411 return (Pattern) unwrap(gre);
412 }
413 }
414
415 public static Object spreadList(Object value) throws Throwable {
416 try {
417 return InvokerHelper.spreadList(value);
418 } catch (GroovyRuntimeException gre) {
419 return unwrap(gre);
420 }
421 }
422
423 public static Object spreadMap(Object value) throws Throwable {
424 try {
425 return InvokerHelper.spreadMap(value);
426 } catch (GroovyRuntimeException gre) {
427 return unwrap(gre);
428 }
429 }
430
431 public static Object negate(Object value) throws Throwable {
432 try {
433 return InvokerHelper.negate(value);
434 } catch (GroovyRuntimeException gre) {
435 return unwrap(gre);
436 }
437 }
438
439 public static Object bitNegate(Object value) throws Throwable {
440 try {
441 return InvokerHelper.bitNegate(value);
442 } catch (GroovyRuntimeException gre) {
443 return unwrap(gre);
444 }
445 }
446
447 /***
448 * @param a array of primitives
449 * @param type component type of the array
450 * @return
451 * @throws Throwable
452 */
453 public static Object[] convertPrimitiveArray(Object a, Class type) throws Throwable {
454 try {
455 return InvokerHelper.convertPrimitiveArray(a,type);
456 } catch (GroovyRuntimeException gre) {
457 return (Object[])unwrap(gre);
458 }
459 }
460
461 public static Object convertToPrimitiveArray(Object a, Class type) throws Throwable {
462 try {
463 return InvokerHelper.convertToPrimitiveArray(a,type);
464 } catch (GroovyRuntimeException gre) {
465 return unwrap(gre);
466 }
467 }
468
469 public static boolean compareIdentical(Object left, Object right) {
470 return left == right;
471 }
472
473 public static boolean compareEqual(Object left, Object right) throws Throwable{
474 try {
475 return InvokerHelper.compareEqual(left, right);
476 } catch (GroovyRuntimeException gre) {
477 unwrap(gre);
478
479 return false;
480 }
481 }
482
483 public static boolean compareNotEqual(Object left, Object right) throws Throwable{
484 return !compareEqual(left, right);
485 }
486
487 public static Integer compareTo(Object left, Object right) throws Throwable{
488 try {
489 return InvokerHelper.compareTo(left, right);
490 } catch (GroovyRuntimeException gre) {
491 return (Integer) unwrap(gre);
492 }
493 }
494
495 public static Matcher findRegex(Object left, Object right) throws Throwable{
496 try {
497 return InvokerHelper.findRegex(left, right);
498 } catch (GroovyRuntimeException gre) {
499 return (Matcher) unwrap(gre);
500 }
501 }
502
503 public static boolean matchRegex(Object left, Object right) throws Throwable{
504 try {
505 return InvokerHelper.matchRegex(left, right);
506 } catch (GroovyRuntimeException gre) {
507 unwrap(gre);
508
509 return false;
510 }
511 }
512
513 public static boolean compareLessThan(Object left, Object right) throws Throwable{
514 return compareTo(left, right).intValue() < 0;
515 }
516
517 public static boolean compareLessThanEqual(Object left, Object right) throws Throwable{
518 return compareTo(left, right).intValue() <= 0;
519 }
520
521 public static boolean compareGreaterThan(Object left, Object right) throws Throwable{
522 return compareTo(left, right).intValue() > 0;
523 }
524
525 public static boolean compareGreaterThanEqual(Object left, Object right) throws Throwable{
526 return compareTo(left, right).intValue() >= 0;
527 }
528
529 public static boolean isCase(Object switchValue, Object caseExpression) throws Throwable{
530 if (caseExpression == null) {
531 return switchValue == null;
532 }
533 return asBool(invokeMethod(caseExpression, "isCase", new Object[]{switchValue}));
534 }
535
536 public static Tuple createTuple(Object[] array) throws Throwable{
537 return new Tuple(array);
538 }
539
540 public static List createList(Object[] values) throws Throwable{
541 return InvokerHelper.createList(values);
542 }
543
544 public static Map createMap(Object[] values) throws Throwable{
545 return InvokerHelper.createMap(values);
546 }
547
548 public static List createRange(Object from, Object to, boolean inclusive) throws Throwable{
549 try {
550 return InvokerHelper.createRange(from,to,inclusive);
551 } catch (GroovyRuntimeException gre) {
552 return (List) unwrap(gre);
553 }
554 }
555
556 public static void assertFailed(Object expression, Object message) {
557 InvokerHelper.assertFailed(expression,message);
558 }
559
560 public static Object box(boolean value) {
561 return value ? Boolean.TRUE : Boolean.FALSE;
562 }
563
564 public static Object box(byte value) {
565 return new Byte(value);
566 }
567
568 public static Object box(char value) {
569 return new Character(value);
570 }
571
572 public static Object box(short value) {
573 return new Short(value);
574 }
575
576 public static Object box(int value) {
577 return integerValue(value);
578 }
579
580 public static Object box(long value) {
581 return new Long(value);
582 }
583
584 public static Object box(float value) {
585 return new Float(value);
586 }
587
588 public static Object box(double value) {
589 return new Double(value);
590 }
591
592 /***
593 * get the Integer object from an int. Cached version is used for small ints.
594 *
595 * @param v
596 * @return
597 */
598 public static Integer integerValue(int v) {
599 return InvokerHelper.integerValue(v);
600 }
601
602 public static byte byteUnbox(Object value) throws Throwable {
603 Number n = (Number) asType(value, Byte.class);
604 return n.byteValue();
605 }
606
607 public static char charUnbox(Object value) throws Throwable {
608 Character n = (Character) asType(value, Character.class);
609 return n.charValue();
610 }
611
612 public static short shortUnbox(Object value) throws Throwable {
613 Number n = (Number) asType(value, Short.class);
614 return n.shortValue();
615 }
616
617 public static int intUnbox(Object value) throws Throwable {
618 Number n = (Number) asType(value, Integer.class);
619 return n.intValue();
620 }
621
622 public static boolean booleanUnbox(Object value) throws Throwable {
623 Boolean n = (Boolean) asType(value, Boolean.class);
624 return n.booleanValue();
625 }
626
627 public static long longUnbox(Object value) throws Throwable {
628 Number n = (Number) asType(value, Long.class);
629 return n.longValue();
630 }
631
632 public static float floatUnbox(Object value) throws Throwable {
633 Number n = (Number) asType(value, Float.class);
634 return n.floatValue();
635 }
636
637 public static double doubleUnbox(Object value) throws Throwable {
638 Number n = (Number) asType(value, Double.class);
639 return n.doubleValue();
640 }
641
642 public static MetaClass getMetaClass(Object object) {
643 return InvokerHelper.getMetaClass(object);
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 * Sets the properties on the given object
712 *
713 * @param object
714 * @param map
715 */
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732 /***
733 * Allows conversion of arrays into a mutable List
734 *
735 * @return the array as a List
736 */
737
738
739
740
741
742
743
744
745
746 /***
747 * Writes the given object to the given stream
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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915 }