1 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 63 public class ScriptBytecodeAdapter { 64 public static final Object [] EMPTY_ARGS = { 65 }; 66 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 invokeConstructor(String type, Object arguments) throws Throwable { 122 try { 123 return InvokerHelper.invokeConstructor(type, arguments); 124 } catch (GroovyRuntimeException gre) { 125 return unwrap(gre); 126 } 127 } 128 129 public static Object invokeConstructorOf(Class type, Object arguments) throws Throwable { 130 try { 131 return InvokerHelper.invokeConstructorOf(type, arguments); 132 } catch (GroovyRuntimeException gre) { 133 return unwrap(gre); 134 } 135 } 136 137 public static Object invokeNoArgumentsConstructorOf(Class type) throws Throwable { 138 return invokeConstructorOf(type, EMPTY_ARGS); 139 } 140 141 public static Object invokeClosure(Object closure, Object arguments) throws Throwable { 142 return invokeMethod(closure, "doCall", arguments); 143 } 144 145 public static Object invokeSuperMethod(Object object, String methodName, Object arguments) throws Throwable { 146 try { 147 return InvokerHelper.invokeSuperMethod(object, methodName, arguments); 148 } catch (GroovyRuntimeException gre) { 149 return unwrap(gre); 150 } 151 } 152 153 public static Object invokeNoArgumentsMethod(Object object, String methodName) throws Throwable { 154 return invokeMethod(object, methodName, EMPTY_ARGS); 155 } 156 157 public static Object invokeNoArgumentsMethodSafe(Object object, String methodName) throws Throwable { 158 if (object != null) return invokeNoArgumentsMethod(object, methodName); 159 return null; 160 } 161 162 public static Object invokeNoArgumentsMethodSpreadSafe(Object object, String methodName) throws Throwable { 163 if (object != null) { 164 if (object instanceof List ) { 165 List list = (List ) object; 166 List answer = new ArrayList (); 167 Iterator it = list.iterator(); 168 for (; it.hasNext();) { 169 answer.add(invokeNoArgumentsMethod(it.next(), methodName)); 170 } 171 return answer; 172 } 173 else 174 return invokeNoArgumentsMethod(object, methodName); 175 } 176 return null; 177 } 178 179 public static Object invokeStaticNoArgumentsMethod(String type, String methodName) throws Throwable { 180 return invokeStaticMethod(type, methodName, EMPTY_ARGS); 181 } 182 183 public static int asInt(Object value) throws Throwable { 184 try { 185 return InvokerHelper.asInt(value); 186 } catch (GroovyRuntimeException gre) { 187 unwrap(gre); 188 return -1; 190 } 191 } 192 193 201 public static Object asType(Object object, Class type) throws Throwable { 202 try { 203 return InvokerHelper.asType(object, type); 204 } catch (GroovyRuntimeException gre) { 205 return unwrap(gre); 206 } 207 } 208 209 210 211 public static Object getAttribute(Object object, String attribute) throws Throwable { 214 try { 215 return InvokerHelper.getAttribute(object, attribute); 216 } catch (GroovyRuntimeException gre) { 217 return unwrap(gre); 218 } 219 } 220 221 public static Object getAttributeSafe(Object object, String attribute) throws Throwable { 222 if (object != null) return getAttribute(object, attribute); 223 return null; 224 } 225 226 public static Object getAttributeSpreadSafe(Object object, String attribute) throws Throwable { 227 if (object != null) { 228 if (object instanceof List ) { 229 List list = (List ) object; 230 List answer = new ArrayList (); 231 Iterator it = list.iterator(); 232 for (; it.hasNext(); ) { 233 answer.add(getAttributeSafe(it.next(), attribute)); 234 } 235 return answer; 236 } 237 else 238 return getAttributeSafe(object, attribute); 239 } 240 return null; 241 } 242 243 public static void setAttribute(Object object, String attribute, Object newValue) throws Throwable { 244 try { 245 InvokerHelper.setAttribute(object, attribute, newValue); 246 } catch (GroovyRuntimeException gre) { 247 unwrap(gre); 248 } 249 } 250 255 public static void setAttribute2(Object newValue, Object object, String property) throws Throwable { 256 setAttribute(object, property, newValue); 257 } 258 259 264 public static void setAttributeSafe2(Object newValue, Object object, String property) throws Throwable { 265 setAttribute2(newValue, object, property); 266 } 267 268 269 270 public static Object getProperty(Object object, String property) throws Throwable { 273 try { 274 return InvokerHelper.getProperty(object, property); 275 } catch (GroovyRuntimeException gre) { 276 return unwrap(gre); 277 } 278 } 279 280 public static Object getPropertySafe(Object object, String property) throws Throwable { 281 if (object != null) return getProperty(object, property); 282 return null; 283 } 284 285 public static Object getPropertySpreadSafe(Object object, String property) throws Throwable { 286 if (object != null) { 287 if (object instanceof List ) { 288 List list = (List ) object; 289 List answer = new ArrayList (); 290 Iterator it = list.iterator(); 291 for (; it.hasNext(); ) { 292 answer.add(getPropertySafe(it.next(), property)); 293 } 294 return answer; 295 } 296 else 297 return getPropertySafe(object, property); 298 } 299 return null; 300 } 301 302 public static void setProperty(Object object, String property, Object newValue) throws Throwable { 303 try { 304 InvokerHelper.setProperty(object, property, newValue); 305 } catch (GroovyRuntimeException gre) { 306 unwrap(gre); 307 } 308 } 309 310 315 public static void setProperty2(Object newValue, Object object, String property) throws Throwable { 316 setProperty(object, property, newValue); 317 } 318 319 324 public static void setPropertySafe2(Object newValue, Object object, String property) throws Throwable { 325 setProperty2(newValue, object, property); 326 } 327 328 329 334 public static void setGroovyObjectProperty(Object newValue, GroovyObject object, String property) throws Throwable { 335 try { 336 object.setProperty(property, newValue); 337 } catch (GroovyRuntimeException gre) { 338 unwrap(gre); 339 } 340 } 341 342 public static Object getGroovyObjectProperty(GroovyObject object, String property) throws Throwable { 343 try { 344 return object.getProperty(property); 345 } catch (GroovyRuntimeException gre) { 346 return unwrap(gre); 347 } 348 } 349 350 351 354 public static Closure getMethodPointer(Object object, String methodName) { 355 return InvokerHelper.getMethodPointer(object, methodName); 356 } 357 358 public static Iterator asIterator(Object collection) throws Throwable { 361 try { 362 return InvokerHelper.asIterator(collection); 363 } catch (GroovyRuntimeException gre) { 364 return (Iterator ) unwrap(gre); 365 } 366 } 367 368 public static boolean asBool(Object object) throws Throwable { 369 try { 370 return InvokerHelper.asBool(object); 371 } catch (GroovyRuntimeException gre) { 372 unwrap(gre); 373 return false; 375 } 376 } 377 378 public static boolean notBoolean(boolean bool) { 379 return !bool; 380 } 381 382 public static boolean notObject(Object object) throws Throwable { 383 return !asBool(object); 384 } 385 386 public static Pattern regexPattern(Object regex) throws Throwable { 387 try { 388 return InvokerHelper.regexPattern(regex); 389 } catch (GroovyRuntimeException gre) { 390 return (Pattern ) unwrap(gre); 391 } 392 } 393 394 public static Object spreadList(Object value) throws Throwable { 395 try { 396 return InvokerHelper.spreadList(value); 397 } catch (GroovyRuntimeException gre) { 398 return unwrap(gre); 399 } 400 } 401 402 public static Object spreadMap(Object value) throws Throwable { 403 try { 404 return InvokerHelper.spreadMap(value); 405 } catch (GroovyRuntimeException gre) { 406 return unwrap(gre); 407 } 408 } 409 410 public static Object negate(Object value) throws Throwable { 411 try { 412 return InvokerHelper.negate(value); 413 } catch (GroovyRuntimeException gre) { 414 return unwrap(gre); 415 } 416 } 417 418 public static Object bitNegate(Object value) throws Throwable { 419 try { 420 return InvokerHelper.bitNegate(value); 421 } catch (GroovyRuntimeException gre) { 422 return unwrap(gre); 423 } 424 } 425 426 432 public static Object [] convertPrimitiveArray(Object a, Class type) throws Throwable { 433 try { 434 return InvokerHelper.convertPrimitiveArray(a,type); 435 } catch (GroovyRuntimeException gre) { 436 return (Object [])unwrap(gre); 437 } 438 } 439 440 public static Object convertToPrimitiveArray(Object a, Class type) throws Throwable { 441 try { 442 return InvokerHelper.convertToPrimitiveArray(a,type); 443 } catch (GroovyRuntimeException gre) { 444 return unwrap(gre); 445 } 446 } 447 448 public static boolean compareIdentical(Object left, Object right) { 449 return left == right; 450 } 451 452 public static boolean compareEqual(Object left, Object right) throws Throwable { 453 try { 454 return InvokerHelper.compareEqual(left, right); 455 } catch (GroovyRuntimeException gre) { 456 unwrap(gre); 457 return false; 459 } 460 } 461 462 public static boolean compareNotEqual(Object left, Object right) throws Throwable { 463 return !compareEqual(left, right); 464 } 465 466 public static Integer compareTo(Object left, Object right) throws Throwable { 467 try { 468 return InvokerHelper.compareTo(left, right); 469 } catch (GroovyRuntimeException gre) { 470 return (Integer ) unwrap(gre); 471 } 472 } 473 474 public static Matcher findRegex(Object left, Object right) throws Throwable { 475 try { 476 return InvokerHelper.findRegex(left, right); 477 } catch (GroovyRuntimeException gre) { 478 return (Matcher ) unwrap(gre); 479 } 480 } 481 482 public static boolean matchRegex(Object left, Object right) throws Throwable { 483 try { 484 return InvokerHelper.matchRegex(left, right); 485 } catch (GroovyRuntimeException gre) { 486 unwrap(gre); 487 return false; 489 } 490 } 491 492 public static boolean compareLessThan(Object left, Object right) throws Throwable { 493 return compareTo(left, right).intValue() < 0; 494 } 495 496 public static boolean compareLessThanEqual(Object left, Object right) throws Throwable { 497 return compareTo(left, right).intValue() <= 0; 498 } 499 500 public static boolean compareGreaterThan(Object left, Object right) throws Throwable { 501 return compareTo(left, right).intValue() > 0; 502 } 503 504 public static boolean compareGreaterThanEqual(Object left, Object right) throws Throwable { 505 return compareTo(left, right).intValue() >= 0; 506 } 507 508 public static boolean isCase(Object switchValue, Object caseExpression) throws Throwable { 509 return asBool(invokeMethod(caseExpression, "isCase", new Object []{switchValue})); 510 } 511 512 public static Tuple createTuple(Object [] array) throws Throwable { 513 return new Tuple(array); 514 } 515 516 public static List createList(Object [] values) throws Throwable { 517 return InvokerHelper.createList(values); 518 } 519 520 public static Map createMap(Object [] values) throws Throwable { 521 return InvokerHelper.createMap(values); 522 } 523 524 public static List createRange(Object from, Object to, boolean inclusive) throws Throwable { 525 try { 526 return InvokerHelper.createRange(from,to,inclusive); 527 } catch (GroovyRuntimeException gre) { 528 return (List ) unwrap(gre); 529 } 530 } 531 532 public static void assertFailed(Object expression, Object message) { 533 InvokerHelper.assertFailed(expression,message); 534 } 535 536 public static Object box(boolean value) { 537 return value ? Boolean.TRUE : Boolean.FALSE; 538 } 539 540 public static Object box(byte value) { 541 return new Byte (value); 542 } 543 544 public static Object box(char value) { 545 return new Character (value); 546 } 547 548 public static Object box(short value) { 549 return new Short (value); 550 } 551 552 public static Object box(int value) { 553 return integerValue(value); 554 } 555 556 public static Object box(long value) { 557 return new Long (value); 558 } 559 560 public static Object box(float value) { 561 return new Float (value); 562 } 563 564 public static Object box(double value) { 565 return new Double (value); 566 } 567 568 574 public static Integer integerValue(int v) { 575 return InvokerHelper.integerValue(v); 576 } 577 578 public static byte byteUnbox(Object value) throws Throwable { 579 Number n = (Number ) asType(value, Byte .class); 580 return n.byteValue(); 581 } 582 583 public static char charUnbox(Object value) throws Throwable { 584 Character n = (Character ) asType(value, Character .class); 585 return n.charValue(); 586 } 587 588 public static short shortUnbox(Object value) throws Throwable { 589 Number n = (Number ) asType(value, Short .class); 590 return n.shortValue(); 591 } 592 593 public static int intUnbox(Object value) throws Throwable { 594 Number n = (Number ) asType(value, Integer .class); 595 return n.intValue(); 596 } 597 598 public static boolean booleanUnbox(Object value) throws Throwable { 599 Boolean n = (Boolean ) asType(value, Boolean .class); 600 return n.booleanValue(); 601 } 602 603 public static long longUnbox(Object value) throws Throwable { 604 Number n = (Number ) asType(value, Long .class); 605 return n.longValue(); 606 } 607 608 public static float floatUnbox(Object value) throws Throwable { 609 Number n = (Number ) asType(value, Float .class); 610 return n.floatValue(); 611 } 612 613 public static double doubleUnbox(Object value) throws Throwable { 614 Number n = (Number ) asType(value, Double .class); 615 return n.doubleValue(); 616 } 617 618 public static MetaClass getMetaClass(Object object) { 619 return InvokerHelper.getMetaClass(object); 620 } 621 622 685 686 692 707 708 713 721 722 725 879 880 891 } 892 | Popular Tags |