View Javadoc

    1   /*
    2    $Id: ScriptBytecodeAdapter.java,v 1.12 2006/05/30 13:15:26 dierk 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.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      private static final Object[] EMPTY_MAIN_ARGS = new Object[]{new String[0]};
    68
    69      private static final Invoker singleton = new Invoker();
    70
    71      private static final Integer ZERO = new Integer(0);
    72      private static final Integer MINUS_ONE = new Integer(-1);
    73      private static final Integer ONE = new Integer(1);*/
    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     /// public static Object invokeConstructorAt(Class at, String type, Object arguments) throws Throwable{
    130     ///     try {
    131     ///         return InvokerHelper.invokeConstructorAt(at, type, arguments);
    132     ///     } catch (GroovyRuntimeException gre) {
    133     ///         return unwrap(gre);
    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     /// public static Object invokeConstructorOf(String type, Object arguments) throws Throwable{
    151     ///     try {
    152     ///         return InvokerHelper.invokeConstructorOf(type, arguments);
    153     ///     } catch (GroovyRuntimeException gre) {
    154     ///         return unwrap(gre);
    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            // return never reached
    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     // Attributes
    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     // Properties
    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     // Coercions
    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             //return never reached
    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             // return never reached
    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             // return never reached
    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     public static void removeClass(Class clazz) {
    648         getInstance().removeMetaClass(clazz);
    649         Introspector.flushFromCaches(clazz);
    650     }
    651
    652     public static Invoker getInstance() {
    653         return singleton;
    654     }
    655
    656     public static Collection asCollection(Object collection) {
    657         return getInstance().asCollection(collection);
    658     }
    659
    660     public static List asList(Object args) {
    661         return getInstance().asList(args);
    662     }
    663
    664     public static String toString(Object arguments) {
    665         return getInstance().toString(arguments);
    666     }
    667
    668     public static String toTypeString(Object[] arguments) {
    669         return getInstance().toTypeString(arguments);
    670     }
    671
    672     public static String inspect(Object self) {
    673         return getInstance().inspect(self);
    674     }
    675
    676
    677
    678     public static Object runScript(Class scriptClass, String[] args) {
    679         Binding context = new Binding(args);
    680         Script script = createScript(scriptClass, context);
    681         return invokeMethod(script, "run", EMPTY_ARGS);
    682     }
    683
    684     public static Script createScript(Class scriptClass, Binding context) {
    685         try {
    686             final GroovyObject object = (GroovyObject) scriptClass.newInstance();
    687             Script script = null;
    688             if (object instanceof Script) {
    689                 script = (Script) object;
    690             } else {
    691                 // it could just be a class, so lets wrap it in a Script wrapper
    692                 // though the bindings will be ignored
    693                 script = new Script() {
    694                     public Object run() {
    695                         object.invokeMethod("main", EMPTY_MAIN_ARGS);
    696                         return null;
    697                     }
    698                 };
    699                 setProperties(object, context.getVariables());
    700             }
    701             script.setBinding(context);
    702             return script;
    703         } catch (Exception e) {
    704             throw new GroovyRuntimeException("Failed to create Script instance for class: " + scriptClass + ". Reason: " + e,
    705                     e);
    706         }
    707     }
    708 */
    709
    710     /***
    711      * Sets the properties on the given object
    712      *
    713      * @param object
    714      * @param map
    715      */
    716 /*    public static void setProperties(Object object, Map map) {
    717         getMetaClass(object).setProperties(object, map);
    718     }
    719
    720     public static String getVersion() {
    721         String version = null;
    722         Package p = Package.getPackage("groovy.lang");
    723         if (p != null) {
    724             version = p.getImplementationVersion();
    725         }
    726         if (version == null) {
    727             version = "";
    728         }
    729         return version;
    730     }*/
    731
    732     /***
    733      * Allows conversion of arrays into a mutable List
    734      *
    735      * @return the array as a List
    736      */
    737     /*protected static List primitiveArrayToList(Object array) {
    738         int size = Array.getLength(array);
    739         List list = new ArrayList(size);
    740         for (int i = 0; i < size; i++) {
    741             list.add(Array.get(array, i));
    742         }
    743         return list;
    744     }*/
    745
    746     /***
    747      * Writes the given object to the given stream
    748      */
    749 /*    public static void write(Writer out, Object object) throws IOException {
    750         if (object instanceof String) {
    751             out.write((String) object);
    752         } else if (object instanceof Writable) {
    753             Writable writable = (Writable) object;
    754             writable.writeTo(out);
    755         } else if (object instanceof InputStream || object instanceof Reader) {
    756             // Copy stream to stream
    757             Reader reader;
    758             if (object instanceof InputStream) {
    759                 reader = new InputStreamReader((InputStream) object);
    760             } else {
    761                 reader = (Reader) object;
    762             }
    763             char[] chars = new char[8192];
    764             int i;
    765             while ((i = reader.read(chars)) != -1) {
    766                 out.write(chars, 0, i);
    767             }
    768             reader.close();
    769         } else {
    770             out.write(toString(object));
    771         }
    772     }
    773
    774     public static int[] convertToIntArray(Object a) {
    775         int[] ans = null;
    776
    777         // conservative coding
    778         if (a.getClass().getName().equals("[I")) {
    779             ans = (int[]) a;
    780         } else {
    781             Object[] ia = (Object[]) a;
    782             ans = new int[ia.length];
    783             for (int i = 0; i < ia.length; i++) {
    784                 ans[i] = ((Number) ia[i]).intValue();
    785             }
    786         }
    787         return ans;
    788     }
    789
    790     public static boolean[] convertToBooleanArray(Object a) {
    791         boolean[] ans = null;
    792
    793         // conservative coding
    794         if (a.getClass().getName().equals("[Z")) {
    795             ans = (boolean[]) a;
    796         } else {
    797             Object[] ia = (Object[]) a;
    798             ans = new boolean[ia.length];
    799             for (int i = 0; i < ia.length; i++) {
    800                 ans[i] = ((Boolean) ia[i]).booleanValue();
    801             }
    802         }
    803         return ans;
    804     }
    805
    806     public static byte[] convertToByteArray(Object a) {
    807         byte[] ans = null;
    808
    809         // conservative coding
    810         if (a.getClass().getName().equals("[B")) {
    811             ans = (byte[]) a;
    812         } else {
    813             Object[] ia = (Object[]) a;
    814             ans = new byte[ia.length];
    815             for (int i = 0; i < ia.length; i++) {
    816                 if (ia[i] != null)
    817                     ans[i] = ((Number) ia[i]).byteValue();
    818             }
    819         }
    820         return ans;
    821     }
    822
    823     public static short[] convertToShortArray(Object a) {
    824         short[] ans = null;
    825
    826         // conservative coding
    827         if (a.getClass().getName().equals("[S")) {
    828             ans = (short[]) a;
    829         } else {
    830             Object[] ia = (Object[]) a;
    831             ans = new short[ia.length];
    832             for (int i = 0; i < ia.length; i++) {
    833                 ans[i] = ((Number) ia[i]).shortValue();
    834             }
    835         }
    836         return ans;
    837     }
    838
    839     public static char[] convertToCharArray(Object a) {
    840         char[] ans = null;
    841
    842         // conservative coding
    843         if (a.getClass().getName().equals("[C")) {
    844             ans = (char[]) a;
    845         } else {
    846             Object[] ia = (Object[]) a;
    847             ans = new char[ia.length];
    848             for (int i = 0; i < ia.length; i++) {
    849                 ans[i] = ((Character) ia[i]).charValue();
    850             }
    851         }
    852         return ans;
    853     }
    854
    855     public static long[] convertToLongArray(Object a) {
    856         long[] ans = null;
    857
    858         // conservative coding
    859         if (a.getClass().getName().equals("[J")) {
    860             ans = (long[]) a;
    861         } else {
    862             Object[] ia = (Object[]) a;
    863             ans = new long[ia.length];
    864             for (int i = 0; i < ia.length; i++) {
    865                 ans[i] = ((Number) ia[i]).longValue();
    866             }
    867         }
    868         return ans;
    869     }
    870
    871     public static float[] convertToFloatArray(Object a) {
    872         float[] ans = null;
    873
    874         // conservative coding
    875         if (a.getClass().getName().equals("[F")) {
    876             ans = (float[]) a;
    877         } else {
    878             Object[] ia = (Object[]) a;
    879             ans = new float[ia.length];
    880             for (int i = 0; i < ia.length; i++) {
    881                 ans[i] = ((Number) ia[i]).floatValue();
    882             }
    883         }
    884         return ans;
    885     }
    886
    887     public static double[] convertToDoubleArray(Object a) {
    888         double[] ans = null;
    889
    890         // conservative coding
    891         if (a.getClass().getName().equals("[D")) {
    892             ans = (double[]) a;
    893         } else {
    894             Object[] ia = (Object[]) a;
    895             ans = new double[ia.length];
    896             for (int i = 0; i < ia.length; i++) {
    897                 ans[i] = ((Number) ia[i]).doubleValue();
    898             }
    899         }
    900         return ans;
    901     }
    902 */
    903
    904     /*
    905
    906     private static Integer[] SMALL_INTEGERS;
    907     private static int INT_CACHE_OFFSET = 128, INT_CACHE_LEN = 256;
    908
    909     static {
    910         SMALL_INTEGERS = new Integer[INT_CACHE_LEN];
    911         for (int i = 0; i < SMALL_INTEGERS.length; i++) {
    912             SMALL_INTEGERS[i] = new Integer(i - INT_CACHE_OFFSET);
    913         }
    914     }*/
    915 }