Clover coverage report - groovy - 1.0-beta-8
Coverage timestamp: Fri Dec 17 2004 14:55:55 GMT
file stats: LOC: 793   Methods: 83
NCLOC: 584   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
InvokerHelper.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
  $Id: InvokerHelper.java,v 1.57 2004/12/08 02:53:27 spullara Exp $
 3   
 
 4   
  Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
 5   
 
 6   
  Redistribution and use of this software and associated documentation
 7   
  ("Software"), with or without modification, are permitted provided
 8   
  that the following conditions are met:
 9   
 
 10   
  1. Redistributions of source code must retain copyright
 11   
     statements and notices.  Redistributions must also contain a
 12   
     copy of this document.
 13   
 
 14   
  2. Redistributions in binary form must reproduce the
 15   
     above copyright notice, this list of conditions and the
 16   
     following disclaimer in the documentation and/or other
 17   
     materials provided with the distribution.
 18   
 
 19   
  3. The name "groovy" must not be used to endorse or promote
 20   
     products derived from this Software without prior written
 21   
     permission of The Codehaus.  For written permission,
 22   
     please contact info@codehaus.org.
 23   
 
 24   
  4. Products derived from this Software may not be called "groovy"
 25   
     nor may "groovy" appear in their names without prior written
 26   
     permission of The Codehaus. "groovy" is a registered
 27   
     trademark of The Codehaus.
 28   
 
 29   
  5. Due credit should be given to The Codehaus -
 30   
     http://groovy.codehaus.org/
 31   
 
 32   
  THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
 33   
  ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 34   
  NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 35   
  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
 36   
  THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 37   
  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 38   
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 39   
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 40   
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 41   
  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 42   
  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 43   
  OF THE POSSIBILITY OF SUCH DAMAGE.
 44   
 
 45   
  */
 46   
 package org.codehaus.groovy.runtime;
 47   
 
 48   
 import groovy.lang.*;
 49   
 
 50   
 import java.beans.Introspector;
 51   
 import java.io.*;
 52   
 import java.lang.reflect.Array;
 53   
 import java.math.BigDecimal;
 54   
 import java.math.BigInteger;
 55   
 import java.util.*;
 56   
 import java.util.regex.Matcher;
 57   
 import java.util.regex.Pattern;
 58   
 
 59   
 /**
 60   
  * A static helper class to make bytecode generation easier and act as a facade over the Invoker
 61   
  *
 62   
  * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
 63   
  * @version $Revision: 1.57 $
 64   
  */
 65   
 public class InvokerHelper {
 66   
     public static final Object[] EMPTY_ARGS = {
 67   
     };
 68   
 
 69   
     private static final Object[] EMPTY_MAIN_ARGS = new Object[]{new String[0]};
 70   
 
 71   
     private static final Invoker singleton = new Invoker();
 72   
 
 73   
     private static final Integer ZERO = new Integer(0);
 74   
     private static final Integer MINUS_ONE = new Integer(-1);
 75   
     private static final Integer ONE = new Integer(1);
 76   
 
 77  0
     public static MetaClass getMetaClass(Object object) {
 78  0
         return getInstance().getMetaClass(object);
 79   
     }
 80   
 
 81  0
     public static void removeClass(Class clazz) {
 82  0
         getInstance().removeMetaClass(clazz);
 83  0
         Introspector.flushFromCaches(clazz);
 84   
     }
 85   
 
 86  0
     public static Invoker getInstance() {
 87  0
         return singleton;
 88   
     }
 89   
 
 90  0
     public static Object invokeNoArgumentsMethod(Object object, String methodName) {
 91  0
         return getInstance().invokeMethod(object, methodName, EMPTY_ARGS);
 92   
     }
 93   
 
 94  0
     public static Object invokeMethod(Object object, String methodName, Object arguments) {
 95  0
         return getInstance().invokeMethod(object, methodName, arguments);
 96   
     }
 97   
 
 98  0
     public static Object invokeSuperMethod(Object object, String methodName, Object arguments) {
 99  0
         return getInstance().invokeSuperMethod(object, methodName, arguments);
 100   
     }
 101   
 
 102  0
     public static Object invokeMethodSafe(Object object, String methodName, Object arguments) {
 103  0
         if (object != null) {
 104  0
             return getInstance().invokeMethod(object, methodName, arguments);
 105   
         }
 106  0
         return null;
 107   
     }
 108   
 
 109  0
     public static Object invokeStaticMethod(String type, String methodName, Object arguments) {
 110  0
         return getInstance().invokeStaticMethod(type, methodName, arguments);
 111   
     }
 112   
 
 113  0
     public static Object invokeStaticNoArgumentsMethod(String type, String methodName) {
 114  0
         return getInstance().invokeStaticMethod(type, methodName, EMPTY_ARGS);
 115   
     }
 116   
 
 117  0
     public static Object invokeConstructor(String type, Object arguments) {
 118  0
         return getInstance().invokeConstructor(type, arguments);
 119   
     }
 120   
 
 121  0
     public static Object invokeConstructorOf(Class type, Object arguments) {
 122  0
         return getInstance().invokeConstructorOf(type, arguments);
 123   
     }
 124   
 
 125  0
     public static Object invokeNoArgumentsConstructorOf(Class type) {
 126  0
         return getInstance().invokeConstructorOf(type, EMPTY_ARGS);
 127   
     }
 128   
 
 129  0
     public static Object invokeClosure(Object closure, Object arguments) {
 130  0
         return getInstance().invokeMethod(closure, "doCall", arguments);
 131   
     }
 132   
 
 133  0
     public static Iterator asIterator(Object collection) {
 134  0
         return getInstance().asIterator(collection);
 135   
     }
 136   
 
 137  0
     public static Collection asCollection(Object collection) {
 138  0
         return getInstance().asCollection(collection);
 139   
     }
 140   
 
 141  0
     public static List asList(Object args) {
 142  0
         return getInstance().asList(args);
 143   
     }
 144   
 
 145  0
     public static String toString(Object arguments) {
 146  0
         return getInstance().toString(arguments);
 147   
     }
 148   
 
 149  0
     public static String toTypeString(Object[] arguments) {
 150  0
         return getInstance().toTypeString(arguments);
 151   
     }
 152   
 
 153  0
     public static String inspect(Object self) {
 154  0
         return getInstance().inspect(self);
 155   
     }
 156   
 
 157  0
     public static Object getProperty(Object object, String property) {
 158  0
         return getInstance().getProperty(object, property);
 159   
     }
 160   
 
 161  0
     public static Object getPropertySafe(Object object, String property) {
 162  0
         if (object != null) {
 163  0
             return getInstance().getProperty(object, property);
 164   
         }
 165  0
         return null;
 166   
     }
 167   
 
 168  0
     public static void setProperty(Object object, String property, Object newValue) {
 169  0
         getInstance().setProperty(object, property, newValue);
 170   
     }
 171   
 
 172   
     /**
 173   
      * This is so we don't have to reorder the stack when we call this method.
 174   
      * At some point a better name might be in order.
 175   
      */
 176  0
     public static void setProperty2(Object newValue, Object object, String property) {
 177  0
         getInstance().setProperty(object, property, newValue);
 178   
     }
 179   
 
 180   
 
 181   
     /**
 182   
      * This is so we don't have to reorder the stack when we call this method.
 183   
      * At some point a better name might be in order.
 184   
      */
 185  0
     public static void setGroovyObjectProperty(Object newValue, GroovyObject object, String property) {
 186  0
         object.setProperty(property, newValue);
 187   
     }
 188   
 
 189  0
     public static Object getGroovyObjectProperty(GroovyObject object, String property) {
 190  0
         return object.getProperty(property);
 191   
     }
 192   
 
 193   
 
 194   
     /**
 195   
      * This is so we don't have to reorder the stack when we call this method.
 196   
      * At some point a better name might be in order.
 197   
      */
 198  0
     public static void setPropertySafe2(Object newValue, Object object, String property) {
 199  0
         if (object != null) {
 200  0
             setProperty2(newValue, object, property);
 201   
         }
 202   
     }
 203   
 
 204   
     /**
 205   
      * Provides a hook for type coercion of the given object to the required type
 206   
      *
 207   
      * @param type   of object to convert the given object to
 208   
      * @param object the object to be converted
 209   
      * @return the original object or a new converted value
 210   
      */
 211  0
     public static Object asType(Object object, Class type) {
 212  0
         return getInstance().asType(object, type);
 213   
     }
 214   
 
 215  0
     public static boolean asBool(Object object) {
 216  0
         return getInstance().asBool(object);
 217   
     }
 218   
 
 219  0
     public static boolean notObject(Object object) {
 220  0
         return !asBool(object);
 221   
     }
 222   
 
 223  0
     public static boolean notBoolean(boolean bool) {
 224  0
         return !bool;
 225   
     }
 226   
 
 227  0
     public static Object negate(Object value) {
 228  0
         if (value instanceof Integer) {
 229  0
             Integer number = (Integer) value;
 230  0
             return integerValue(-number.intValue());
 231  0
         } else if (value instanceof Long) {
 232  0
             Long number = (Long) value;
 233  0
             return new Long(-number.longValue());
 234  0
         } else if (value instanceof BigInteger) {
 235  0
             return ((BigInteger) value).negate();
 236  0
         } else if (value instanceof BigDecimal) {
 237  0
             return ((BigDecimal) value).negate();
 238  0
         } else if (value instanceof Double) {
 239  0
             Double number = (Double) value;
 240  0
             return new Double(-number.doubleValue());
 241  0
         } else if (value instanceof Float) {
 242  0
             Float number = (Float) value;
 243  0
             return new Float(-number.floatValue());
 244   
         } else {
 245  0
             throw new GroovyRuntimeException("Cannot negate type " + value.getClass().getName() + ", value " + value);
 246   
         }
 247   
     }
 248   
 
 249  0
     public static boolean isCase(Object switchValue, Object caseExpression) {
 250  0
         return asBool(invokeMethod(caseExpression, "isCase", new Object[]{switchValue}));
 251   
     }
 252   
 
 253  0
     public static boolean compareIdentical(Object left, Object right) {
 254  0
         return left == right;
 255   
     }
 256   
 
 257  0
     public static boolean compareEqual(Object left, Object right) {
 258  0
         return getInstance().objectsEqual(left, right);
 259   
     }
 260   
 
 261  0
     public static Matcher findRegex(Object left, Object right) {
 262  0
         return getInstance().objectFindRegex(left, right);
 263   
     }
 264   
 
 265  0
     public static boolean matchRegex(Object left, Object right) {
 266  0
         return getInstance().objectMatchRegex(left, right);
 267   
     }
 268   
 
 269  0
     public static Pattern regexPattern(Object regex) {
 270  0
         return getInstance().regexPattern(regex);
 271   
     }
 272   
 
 273  0
     public static boolean compareNotEqual(Object left, Object right) {
 274  0
         return !getInstance().objectsEqual(left, right);
 275   
     }
 276   
 
 277  0
     public static boolean compareLessThan(Object left, Object right) {
 278  0
         return getInstance().compareTo(left, right) < 0;
 279   
     }
 280   
 
 281  0
     public static boolean compareLessThanEqual(Object left, Object right) {
 282  0
         return getInstance().compareTo(left, right) <= 0;
 283   
     }
 284   
 
 285  0
     public static boolean compareGreaterThan(Object left, Object right) {
 286  0
         return getInstance().compareTo(left, right) > 0;
 287   
     }
 288   
 
 289  0
     public static boolean compareGreaterThanEqual(Object left, Object right) {
 290  0
         return getInstance().compareTo(left, right) >= 0;
 291   
     }
 292   
 
 293  0
     public static Integer compareTo(Object left, Object right) {
 294  0
         int answer = getInstance().compareTo(left, right);
 295  0
         if (answer == 0) {
 296  0
             return ZERO;
 297   
         } else {
 298  0
             return answer > 0 ? ONE : MINUS_ONE;
 299   
         }
 300   
     }
 301   
 
 302  0
     public static Tuple createTuple(Object[] array) {
 303  0
         return new Tuple(array);
 304   
     }
 305   
 
 306  0
     public static List createList(Object[] values) {
 307  0
         ArrayList answer = new ArrayList(values.length);
 308  0
         for (int i = 0; i < values.length; i++) {
 309  0
             answer.add(values[i]);
 310   
         }
 311  0
         return answer;
 312   
     }
 313   
 
 314  0
     public static Map createMap(Object[] values) {
 315  0
         Map answer = new HashMap(values.length / 2);
 316  0
         int i = 0;
 317  0
         while (i < values.length) {
 318  0
             answer.put(values[i++], values[i++]);
 319   
         }
 320  0
         return answer;
 321   
     }
 322   
 
 323  0
     public static List createRange(Object from, Object to, boolean inclusive) {
 324  0
         if (!inclusive) {
 325  0
             if (compareGreaterThan(from, to)) {
 326  0
                 to = invokeMethod(to, "next", EMPTY_ARGS);
 327   
             } else {
 328  0
                 to = invokeMethod(to, "previous", EMPTY_ARGS);
 329   
             }
 330   
         }
 331  0
         if (from instanceof Integer && to instanceof Integer) {
 332  0
             return new IntRange(asInt(from), asInt(to));
 333   
         } else {
 334  0
             return new ObjectRange((Comparable) from, (Comparable) to);
 335   
         }
 336   
     }
 337   
 
 338  0
     public static int asInt(Object value) {
 339  0
         return getInstance().asInt(value);
 340   
     }
 341   
 
 342  0
     public static void assertFailed(Object expression, Object message) {
 343  0
         if (message == null || "".equals(message)) {
 344  0
             throw new AssertionError("Expression: " + expression);
 345   
         } else {
 346  0
             throw new AssertionError("" + message + ". Expression: " + expression);
 347   
         }
 348   
     }
 349   
 
 350  0
     public static Object runScript(Class scriptClass, String[] args) {
 351  0
         Binding context = new Binding(args);
 352  0
         Script script = createScript(scriptClass, context);
 353  0
         return invokeMethod(script, "run", EMPTY_ARGS);
 354   
     }
 355   
 
 356  0
     public static Script createScript(Class scriptClass, Binding context) {
 357  0
         try {
 358  0
             final GroovyObject object = (GroovyObject) scriptClass.newInstance();
 359  0
             Script script = null;
 360  0
             if (object instanceof Script) {
 361  0
                 script = (Script) object;
 362   
             } else {
 363   
                 // it could just be a class, so lets wrap it in a Script wrapper
 364   
                 // though the bindings will be ignored
 365  0
                 script = new Script() {
 366  0
                     public Object run() {
 367  0
                         object.invokeMethod("main", EMPTY_MAIN_ARGS);
 368  0
                         return null;
 369   
                     }
 370   
                 };
 371  0
                 setProperties(object, context.getVariables());
 372   
             }
 373  0
             script.setBinding(context);
 374  0
             return script;
 375   
         } catch (Exception e) {
 376  0
             throw new GroovyRuntimeException("Failed to create Script instance for class: " + scriptClass + ". Reason: " + e,
 377   
                     e);
 378   
         }
 379   
     }
 380   
 
 381   
     /**
 382   
      * Sets the properties on the given object
 383   
      *
 384   
      * @param object
 385   
      * @param map
 386   
      */
 387  0
     public static void setProperties(Object object, Map map) {
 388  0
         getMetaClass(object).setProperties(object, map);
 389   
     }
 390   
 
 391  0
     public static String getVersion() {
 392  0
         String version = null;
 393  0
         Package p = Package.getPackage("groovy.lang");
 394  0
         if (p != null) {
 395  0
             version = p.getImplementationVersion();
 396   
         }
 397  0
         if (version == null) {
 398  0
             version = "";
 399   
         }
 400  0
         return version;
 401   
     }
 402   
 
 403   
     /**
 404   
      * Allows conversion of arrays into a mutable List
 405   
      *
 406   
      * @return the array as a List
 407   
      */
 408  0
     protected static List primitiveArrayToList(Object array) {
 409  0
         int size = Array.getLength(array);
 410  0
         List list = new ArrayList(size);
 411  0
         for (int i = 0; i < size; i++) {
 412  0
             list.add(Array.get(array, i));
 413   
         }
 414  0
         return list;
 415   
     }
 416   
 
 417   
     /**
 418   
      * Writes the given object to the given stream
 419   
      */
 420  0
     public static void write(Writer out, Object object) throws IOException {
 421  0
         if (object instanceof String) {
 422  0
             out.write((String) object);
 423  0
         } else if (object instanceof Writable) {
 424  0
             Writable writable = (Writable) object;
 425  0
             writable.writeTo(out);
 426  0
         } else if (object instanceof InputStream || object instanceof Reader) {
 427   
             // Copy stream to stream
 428  0
             Reader reader;
 429  0
             if (object instanceof InputStream) {
 430  0
                 reader = new InputStreamReader((InputStream) object);
 431   
             } else {
 432  0
                 reader = (Reader) object;
 433   
             }
 434  0
             char[] chars = new char[8192];
 435  0
             int i;
 436  0
             while ((i = reader.read(chars)) != -1) {
 437  0
                 out.write(chars, 0, i);
 438   
             }
 439  0
             reader.close();
 440   
         } else {
 441  0
             out.write(toString(object));
 442   
         }
 443   
     }
 444   
 
 445  0
     public static Object box(boolean value) {
 446  0
         return value ? Boolean.TRUE : Boolean.FALSE;
 447   
     }
 448   
 
 449  0
     public static Object box(byte value) {
 450  0
         return new Byte(value);
 451   
     }
 452   
 
 453  0
     public static Object box(char value) {
 454  0
         return new Character(value);
 455   
     }
 456   
 
 457  0
     public static Object box(short value) {
 458  0
         return new Short(value);
 459   
     }
 460   
 
 461  0
     public static Object box(int value) {
 462  0
         return integerValue(value);
 463   
     }
 464   
 
 465  0
     public static Object box(long value) {
 466  0
         return new Long(value);
 467   
     }
 468   
 
 469  0
     public static Object box(float value) {
 470  0
         return new Float(value);
 471   
     }
 472   
 
 473  0
     public static Object box(double value) {
 474  0
         return new Double(value);
 475   
     }
 476   
 
 477  0
     public static byte byteUnbox(Object value) {
 478  0
         Number n = (Number) asType(value, Byte.class);
 479  0
         return n.byteValue();
 480   
     }
 481   
 
 482  0
     public static char charUnbox(Object value) {
 483  0
         Character n = (Character) asType(value, Character.class);
 484  0
         return n.charValue();
 485   
     }
 486   
 
 487  0
     public static short shortUnbox(Object value) {
 488  0
         Number n = (Number) asType(value, Short.class);
 489  0
         return n.shortValue();
 490   
     }
 491   
 
 492  0
     public static int intUnbox(Object value) {
 493  0
         Number n = (Number) asType(value, Integer.class);
 494  0
         return n.intValue();
 495   
     }
 496   
 
 497  0
     public static boolean booleanUnbox(Object value) {
 498  0
         Boolean n = (Boolean) asType(value, Boolean.class);
 499  0
         return n.booleanValue();
 500   
     }
 501   
 
 502  0
     public static long longUnbox(Object value) {
 503  0
         Number n = (Number) asType(value, Long.class);
 504  0
         return n.longValue();
 505   
     }
 506   
 
 507  0
     public static float floatUnbox(Object value) {
 508  0
         Number n = (Number) asType(value, Float.class);
 509  0
         return n.floatValue();
 510   
     }
 511   
 
 512  0
     public static double doubleUnbox(Object value) {
 513  0
         Number n = (Number) asType(value, Double.class);
 514  0
         return n.doubleValue();
 515   
     }
 516   
 
 517   
     /**
 518   
      * @param a    array of primitives
 519   
      * @param type component type of the array
 520   
      * @return
 521   
      */
 522  0
     public static Object[] convertPrimitiveArray(Object a, Class type) {
 523   
 //        System.out.println("a.getClass() = " + a.getClass());
 524  0
         Object[] ans = null;
 525  0
         String elemType = type.getName();
 526  0
         if (elemType.equals("int")) {
 527   
             // conservative coding
 528  0
             if (a.getClass().getName().equals("[Ljava.lang.Integer;")) {
 529  0
                 ans = (Integer[]) a;
 530   
             } else {
 531  0
                 int[] ia = (int[]) a;
 532  0
                 ans = new Integer[ia.length];
 533  0
                 for (int i = 0; i < ia.length; i++) {
 534  0
                     int e = ia[i];
 535  0
                     ans[i] = integerValue(e);
 536   
                 }
 537   
             }
 538  0
         } else if (elemType.equals("char")) {
 539  0
             if (a.getClass().getName().equals("[Ljava.lang.Character;")) {
 540  0
                 ans = (Character[]) a;
 541   
             } else {
 542  0
                 char[] ia = (char[]) a;
 543  0
                 ans = new Character[ia.length];
 544  0
                 for (int i = 0; i < ia.length; i++) {
 545  0
                     char e = ia[i];
 546  0
                     ans[i] = new Character(e);
 547   
                 }
 548   
             }
 549  0
         } else if (elemType.equals("boolean")) {
 550  0
             if (a.getClass().getName().equals("[Ljava.lang.Boolean;")) {
 551  0
                 ans = (Boolean[]) a;
 552   
             } else {
 553  0
                 boolean[] ia = (boolean[]) a;
 554  0
                 ans = new Boolean[ia.length];
 555  0
                 for (int i = 0; i < ia.length; i++) {
 556  0
                     boolean e = ia[i];
 557  0
                     ans[i] = new Boolean(e);
 558   
                 }
 559   
             }
 560  0
         } else if (elemType.equals("byte")) {
 561  0
             if (a.getClass().getName().equals("[Ljava.lang.Byte;")) {
 562  0
                 ans = (Byte[]) a;
 563   
             } else {
 564  0
                 byte[] ia = (byte[]) a;
 565  0
                 ans = new Byte[ia.length];
 566  0
                 for (int i = 0; i < ia.length; i++) {
 567  0
                     byte e = ia[i];
 568  0
                     ans[i] = new Byte(e);
 569   
                 }
 570   
             }
 571  0
         } else if (elemType.equals("short")) {
 572  0
             if (a.getClass().getName().equals("[Ljava.lang.Short;")) {
 573  0
                 ans = (Short[]) a;
 574   
             } else {
 575  0
                 short[] ia = (short[]) a;
 576  0
                 ans = new Short[ia.length];
 577  0
                 for (int i = 0; i < ia.length; i++) {
 578  0
                     short e = ia[i];
 579  0
                     ans[i] = new Short(e);
 580   
                 }
 581   
             }
 582  0
         } else if (elemType.equals("float")) {
 583  0
             if (a.getClass().getName().equals("[Ljava.lang.Float;")) {
 584  0
                 ans = (Float[]) a;
 585   
             } else {
 586  0
                 float[] ia = (float[]) a;
 587  0
                 ans = new Float[ia.length];
 588  0
                 for (int i = 0; i < ia.length; i++) {
 589  0
                     float e = ia[i];
 590  0
                     ans[i] = new Float(e);
 591   
                 }
 592   
             }
 593  0
         } else if (elemType.equals("long")) {
 594  0
             if (a.getClass().getName().equals("[Ljava.lang.Long;")) {
 595  0
                 ans = (Long[]) a;
 596   
             } else {
 597  0
                 long[] ia = (long[]) a;
 598  0
                 ans = new Long[ia.length];
 599  0
                 for (int i = 0; i < ia.length; i++) {
 600  0
                     long e = ia[i];
 601  0
                     ans[i] = new Long(e);
 602   
                 }
 603   
             }
 604  0
         } else if (elemType.equals("double")) {
 605  0
             if (a.getClass().getName().equals("[Ljava.lang.Double;")) {
 606  0
                 ans = (Double[]) a;
 607   
             } else {
 608  0
                 double[] ia = (double[]) a;
 609  0
                 ans = new Double[ia.length];
 610  0
                 for (int i = 0; i < ia.length; i++) {
 611  0
                     double e = ia[i];
 612  0
                     ans[i] = new Double(e);
 613   
                 }
 614   
             }
 615   
         }
 616  0
         return ans;
 617   
     }
 618   
 
 619  0
     public static int[] convertToIntArray(Object a) {
 620  0
         int[] ans = null;
 621   
 
 622   
         // conservative coding
 623  0
         if (a.getClass().getName().equals("[I")) {
 624  0
             ans = (int[]) a;
 625   
         } else {
 626  0
             Object[] ia = (Object[]) a;
 627  0
             ans = new int[ia.length];
 628  0
             for (int i = 0; i < ia.length; i++) {
 629  0
                 ans[i] = ((Number) ia[i]).intValue();
 630   
             }
 631   
         }
 632  0
         return ans;
 633   
     }
 634   
 
 635  0
     public static boolean[] convertToBooleanArray(Object a) {
 636  0
         boolean[] ans = null;
 637   
 
 638   
         // conservative coding
 639  0
         if (a.getClass().getName().equals("[Z")) {
 640  0
             ans = (boolean[]) a;
 641   
         } else {
 642  0
             Object[] ia = (Object[]) a;
 643  0
             ans = new boolean[ia.length];
 644  0
             for (int i = 0; i < ia.length; i++) {
 645  0
                 ans[i] = ((Boolean) ia[i]).booleanValue();
 646   
             }
 647   
         }
 648  0
         return ans;
 649   
     }
 650   
 
 651  0
     public static byte[] convertToByteArray(Object a) {
 652  0
         byte[] ans = null;
 653   
 
 654   
         // conservative coding
 655  0
         if (a.getClass().getName().equals("[B")) {
 656  0
             ans = (byte[]) a;
 657   
         } else {
 658  0
             Object[] ia = (Object[]) a;
 659  0
             ans = new byte[ia.length];
 660  0
             for (int i = 0; i < ia.length; i++) {
 661  0
                 ans[i] = ((Number) ia[i]).byteValue();
 662   
             }
 663   
         }
 664  0
         return ans;
 665   
     }
 666   
 
 667  0
     public static short[] convertToShortArray(Object a) {
 668  0
         short[] ans = null;
 669   
 
 670   
         // conservative coding
 671  0
         if (a.getClass().getName().equals("[S")) {
 672  0
             ans = (short[]) a;
 673   
         } else {
 674  0
             Object[] ia = (Object[]) a;
 675  0
             ans = new short[ia.length];
 676  0
             for (int i = 0; i < ia.length; i++) {
 677  0
                 ans[i] = ((Number) ia[i]).shortValue();
 678   
             }
 679   
         }
 680  0
         return ans;
 681   
     }
 682   
 
 683  0
     public static char[] convertToCharArray(Object a) {
 684  0
         char[] ans = null;
 685   
 
 686   
         // conservative coding
 687  0
         if (a.getClass().getName().equals("[C")) {
 688  0
             ans = (char[]) a;
 689   
         } else {
 690  0
             Object[] ia = (Object[]) a;
 691  0
             ans = new char[ia.length];
 692  0
             for (int i = 0; i < ia.length; i++) {
 693  0
                 ans[i] = ((Character) ia[i]).charValue();
 694   
             }
 695   
         }
 696  0
         return ans;
 697   
     }
 698   
 
 699  0
     public static long[] convertToLongArray(Object a) {
 700  0
         long[] ans = null;
 701   
 
 702   
         // conservative coding
 703  0
         if (a.getClass().getName().equals("[J")) {
 704  0
             ans = (long[]) a;
 705   
         } else {
 706  0
             Object[] ia = (Object[]) a;
 707  0
             ans = new long[ia.length];
 708  0
             for (int i = 0; i < ia.length; i++) {
 709  0
                 ans[i] = ((Number) ia[i]).longValue();
 710   
             }
 711   
         }
 712  0
         return ans;
 713   
     }
 714   
 
 715  0
     public static float[] convertToFloatArray(Object a) {
 716  0
         float[] ans = null;
 717   
 
 718   
         // conservative coding
 719  0
         if (a.getClass().getName().equals("[F")) {
 720  0
             ans = (float[]) a;
 721   
         } else {
 722  0
             Object[] ia = (Object[]) a;
 723  0
             ans = new float[ia.length];
 724  0
             for (int i = 0; i < ia.length; i++) {
 725  0
                 ans[i] = ((Number) ia[i]).floatValue();
 726   
             }
 727   
         }
 728  0
         return ans;
 729   
     }
 730   
 
 731  0
     public static double[] convertToDoubleArray(Object a) {
 732  0
         double[] ans = null;
 733   
 
 734   
         // conservative coding
 735  0
         if (a.getClass().getName().equals("[D")) {
 736  0
             ans = (double[]) a;
 737   
         } else {
 738  0
             Object[] ia = (Object[]) a;
 739  0
             ans = new double[ia.length];
 740  0
             for (int i = 0; i < ia.length; i++) {
 741  0
                 ans[i] = ((Number) ia[i]).doubleValue();
 742   
             }
 743   
         }
 744  0
         return ans;
 745   
     }
 746   
 
 747  0
     public static Object convertToPrimitiveArray(Object a, Class type) {
 748  0
         if (type == Byte.TYPE)
 749  0
             return convertToByteArray(a);
 750  0
         if (type == Boolean.TYPE)
 751  0
             return convertToBooleanArray(a);
 752  0
         if (type == Short.TYPE)
 753  0
             return convertToShortArray(a);
 754  0
         if (type == Character.TYPE)
 755  0
             return convertToCharArray(a);
 756  0
         if (type == Integer.TYPE)
 757  0
             return convertToIntArray(a);
 758  0
         if (type == Long.TYPE)
 759  0
             return convertToLongArray(a);
 760  0
         if (type == Float.TYPE)
 761  0
             return convertToFloatArray(a);
 762  0
         if (type == Double.TYPE)
 763  0
             return convertToDoubleArray(a);
 764   
         else
 765  0
             return a;
 766   
     }
 767   
 
 768   
     /**
 769   
      * get the Integer object from an int. Cached version is used for small ints.
 770   
      *
 771   
      * @param v
 772   
      * @return
 773   
      */
 774  0
     public static Integer integerValue(int v) {
 775  0
         int index = v + INT_CACHE_OFFSET;
 776  0
         if (index >= 0 && index < INT_CACHE_LEN) {
 777  0
             return SMALL_INTEGERS[index];
 778   
         } else {
 779  0
             return new Integer(v);
 780   
         }
 781   
     }
 782   
 
 783   
     private static Integer[] SMALL_INTEGERS;
 784   
     private static int INT_CACHE_OFFSET = 128, INT_CACHE_LEN = 256;
 785   
 
 786   
     static {
 787  0
         SMALL_INTEGERS = new Integer[INT_CACHE_LEN];
 788  0
         for (int i = 0; i < SMALL_INTEGERS.length; i++) {
 789  0
             SMALL_INTEGERS[i] = new Integer(i - INT_CACHE_OFFSET);
 790   
         }
 791   
     }
 792   
 }
 793