rt/emul/compact/src/main/java/sun/invoke/util/Wrapper.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 09 Aug 2014 11:11:13 +0200
branchjdk8-b132
changeset 1646 c880a8a8803b
permissions -rw-r--r--
Batch of classes necessary to implement invoke dynamic interfaces. Taken from JDK8 build 132
     1 /*
     2  * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 
    26 package sun.invoke.util;
    27 
    28 public enum Wrapper {
    29     BOOLEAN(Boolean.class, boolean.class, 'Z', (Boolean)false, new boolean[0], Format.unsigned(1)),
    30     // These must be in the order defined for widening primitive conversions in JLS 5.1.2
    31     BYTE(Byte.class, byte.class, 'B', (Byte)(byte)0, new byte[0], Format.signed(8)),
    32     SHORT(Short.class, short.class, 'S', (Short)(short)0, new short[0], Format.signed(16)),
    33     CHAR(Character.class, char.class, 'C', (Character)(char)0, new char[0], Format.unsigned(16)),
    34     INT(Integer.class, int.class, 'I', (Integer)/*(int)*/0, new int[0], Format.signed(32)),
    35     LONG(Long.class, long.class, 'J', (Long)(long)0, new long[0], Format.signed(64)),
    36     FLOAT(Float.class, float.class, 'F', (Float)(float)0, new float[0], Format.floating(32)),
    37     DOUBLE(Double.class, double.class, 'D', (Double)(double)0, new double[0], Format.floating(64)),
    38     //NULL(Null.class, null.class, 'N', null, null, Format.other(1)),
    39     OBJECT(Object.class, Object.class, 'L', null, new Object[0], Format.other(1)),
    40     // VOID must be the last type, since it is "assignable" from any other type:
    41     VOID(Void.class, void.class, 'V', null, null, Format.other(0)),
    42     ;
    43 
    44     private final Class<?> wrapperType;
    45     private final Class<?> primitiveType;
    46     private final char     basicTypeChar;
    47     private final Object   zero;
    48     private final Object   emptyArray;
    49     private final int      format;
    50     private final String   wrapperSimpleName;
    51     private final String   primitiveSimpleName;
    52 
    53     private Wrapper(Class<?> wtype, Class<?> ptype, char tchar, Object zero, Object emptyArray, int format) {
    54         this.wrapperType = wtype;
    55         this.primitiveType = ptype;
    56         this.basicTypeChar = tchar;
    57         this.zero = zero;
    58         this.emptyArray = emptyArray;
    59         this.format = format;
    60         this.wrapperSimpleName = wtype.getSimpleName();
    61         this.primitiveSimpleName = ptype.getSimpleName();
    62     }
    63 
    64     /** For debugging, give the details of this wrapper. */
    65     public String detailString() {
    66         return wrapperSimpleName+
    67                 java.util.Arrays.asList(wrapperType, primitiveType,
    68                 basicTypeChar, zero,
    69                 "0x"+Integer.toHexString(format));
    70     }
    71 
    72     private static abstract class Format {
    73         static final int SLOT_SHIFT = 0, SIZE_SHIFT = 2, KIND_SHIFT = 12;
    74         static final int
    75                 SIGNED   = (-1) << KIND_SHIFT,
    76                 UNSIGNED = 0    << KIND_SHIFT,
    77                 FLOATING = 1    << KIND_SHIFT;
    78         static final int
    79                 SLOT_MASK = ((1<<(SIZE_SHIFT-SLOT_SHIFT))-1),
    80                 SIZE_MASK = ((1<<(KIND_SHIFT-SIZE_SHIFT))-1);
    81         static int format(int kind, int size, int slots) {
    82             assert(((kind >> KIND_SHIFT) << KIND_SHIFT) == kind);
    83             assert((size & (size-1)) == 0); // power of two
    84             assert((kind == SIGNED)   ? (size > 0) :
    85                    (kind == UNSIGNED) ? (size > 0) :
    86                    (kind == FLOATING) ? (size == 32 || size == 64)  :
    87                    false);
    88             assert((slots == 2) ? (size == 64) :
    89                    (slots == 1) ? (size <= 32) :
    90                    false);
    91             return kind | (size << SIZE_SHIFT) | (slots << SLOT_SHIFT);
    92         }
    93         static final int
    94                 INT      = SIGNED   | (32 << SIZE_SHIFT) | (1 << SLOT_SHIFT),
    95                 SHORT    = SIGNED   | (16 << SIZE_SHIFT) | (1 << SLOT_SHIFT),
    96                 BOOLEAN  = UNSIGNED | (1  << SIZE_SHIFT) | (1 << SLOT_SHIFT),
    97                 CHAR     = UNSIGNED | (16 << SIZE_SHIFT) | (1 << SLOT_SHIFT),
    98                 FLOAT    = FLOATING | (32 << SIZE_SHIFT) | (1 << SLOT_SHIFT),
    99                 VOID     = UNSIGNED | (0  << SIZE_SHIFT) | (0 << SLOT_SHIFT),
   100                 NUM_MASK = (-1) << SIZE_SHIFT;
   101         static int signed(int size)   { return format(SIGNED,   size, (size > 32 ? 2 : 1)); }
   102         static int unsigned(int size) { return format(UNSIGNED, size, (size > 32 ? 2 : 1)); }
   103         static int floating(int size) { return format(FLOATING, size, (size > 32 ? 2 : 1)); }
   104         static int other(int slots)   { return slots << SLOT_SHIFT; }
   105     }
   106 
   107     /// format queries:
   108 
   109     /** How many bits are in the wrapped value?  Returns 0 for OBJECT or VOID. */
   110     public int     bitWidth()      { return (format >> Format.SIZE_SHIFT) & Format.SIZE_MASK; }
   111     /** How many JVM stack slots occupied by the wrapped value?  Returns 0 for VOID. */
   112     public int     stackSlots()    { return (format >> Format.SLOT_SHIFT) & Format.SLOT_MASK; }
   113     /** Does the wrapped value occupy a single JVM stack slot? */
   114     public boolean isSingleWord()  { return (format & (1 << Format.SLOT_SHIFT)) != 0; }
   115     /** Does the wrapped value occupy two JVM stack slots? */
   116     public boolean isDoubleWord()  { return (format & (2 << Format.SLOT_SHIFT)) != 0; }
   117     /** Is the wrapped type numeric (not void or object)? */
   118     public boolean isNumeric()     { return (format & Format.NUM_MASK) != 0; }
   119     /** Is the wrapped type a primitive other than float, double, or void? */
   120     public boolean isIntegral()    { return isNumeric() && format < Format.FLOAT; }
   121     /** Is the wrapped type one of int, boolean, byte, char, or short? */
   122     public boolean isSubwordOrInt() { return isIntegral() && isSingleWord(); }
   123     /* Is the wrapped value a signed integral type (one of byte, short, int, or long)? */
   124     public boolean isSigned()      { return format < Format.VOID; }
   125     /* Is the wrapped value an unsigned integral type (one of boolean or char)? */
   126     public boolean isUnsigned()    { return format >= Format.BOOLEAN && format < Format.FLOAT; }
   127     /** Is the wrapped type either float or double? */
   128     public boolean isFloating()    { return format >= Format.FLOAT; }
   129     /** Is the wrapped type either void or a reference? */
   130     public boolean isOther()       { return (format & ~Format.SLOT_MASK) == 0; }
   131 
   132     /** Does the JLS 5.1.2 allow a variable of this wrapper's
   133      *  primitive type to be assigned from a value of the given wrapper's primitive type?
   134      *  Cases:
   135      *  <ul>
   136      *  <li>unboxing followed by widening primitive conversion
   137      *  <li>any type converted to {@code void} (i.e., dropping a method call's value)
   138      *  <li>boxing conversion followed by widening reference conversion to {@code Object}
   139      *  </ul>
   140      *  These are the cases allowed by MethodHandle.asType.
   141      */
   142     public boolean isConvertibleFrom(Wrapper source) {
   143         if (this == source)  return true;
   144         if (this.compareTo(source) < 0) {
   145             // At best, this is a narrowing conversion.
   146             return false;
   147         }
   148         // All conversions are allowed in the enum order between floats and signed ints.
   149         // First detect non-signed non-float types (boolean, char, Object, void).
   150         boolean floatOrSigned = (((this.format & source.format) & Format.SIGNED) != 0);
   151         if (!floatOrSigned) {
   152             if (this.isOther())  return true;
   153             // can convert char to int or wider, but nothing else
   154             if (source.format == Format.CHAR)  return true;
   155             // no other conversions are classified as widening
   156             return false;
   157         }
   158         // All signed and float conversions in the enum order are widening.
   159         assert(this.isFloating() || this.isSigned());
   160         assert(source.isFloating() || source.isSigned());
   161         return true;
   162     }
   163 
   164     static { assert(checkConvertibleFrom()); }
   165     private static boolean checkConvertibleFrom() {
   166         // Check the matrix for correct classification of widening conversions.
   167         for (Wrapper w : values()) {
   168             assert(w.isConvertibleFrom(w));
   169             assert(VOID.isConvertibleFrom(w));
   170             if (w != VOID) {
   171                 assert(OBJECT.isConvertibleFrom(w));
   172                 assert(!w.isConvertibleFrom(VOID));
   173             }
   174             // check relations with unsigned integral types:
   175             if (w != CHAR) {
   176                 assert(!CHAR.isConvertibleFrom(w));
   177                 if (!w.isConvertibleFrom(INT))
   178                     assert(!w.isConvertibleFrom(CHAR));
   179             }
   180             if (w != BOOLEAN) {
   181                 assert(!BOOLEAN.isConvertibleFrom(w));
   182                 if (w != VOID && w != OBJECT)
   183                     assert(!w.isConvertibleFrom(BOOLEAN));
   184             }
   185             // check relations with signed integral types:
   186             if (w.isSigned()) {
   187                 for (Wrapper x : values()) {
   188                     if (w == x)  continue;
   189                     if (x.isFloating())
   190                         assert(!w.isConvertibleFrom(x));
   191                     else if (x.isSigned()) {
   192                         if (w.compareTo(x) < 0)
   193                             assert(!w.isConvertibleFrom(x));
   194                         else
   195                             assert(w.isConvertibleFrom(x));
   196                     }
   197                 }
   198             }
   199             // check relations with floating types:
   200             if (w.isFloating()) {
   201                 for (Wrapper x : values()) {
   202                     if (w == x)  continue;
   203                     if (x.isSigned())
   204                         assert(w.isConvertibleFrom(x));
   205                     else if (x.isFloating()) {
   206                         if (w.compareTo(x) < 0)
   207                             assert(!w.isConvertibleFrom(x));
   208                         else
   209                             assert(w.isConvertibleFrom(x));
   210                     }
   211                 }
   212             }
   213         }
   214         return true;  // i.e., assert(true)
   215     }
   216 
   217     /** Produce a zero value for the given wrapper type.
   218      *  This will be a numeric zero for a number or character,
   219      *  false for a boolean, and null for a reference or void.
   220      *  The common thread is that this is what is contained
   221      *  in a default-initialized variable of the given primitive
   222      *  type.  (For void, it is what a reflective method returns
   223      *  instead of no value at all.)
   224      */
   225     public Object zero() { return zero; }
   226 
   227     /** Produce a zero value for the given wrapper type T.
   228      *  The optional argument must a type compatible with this wrapper.
   229      *  Equivalent to {@code this.cast(this.zero(), type)}.
   230      */
   231     public <T> T zero(Class<T> type) { return convert(zero, type); }
   232 
   233 //    /** Produce a wrapper for the given wrapper or primitive type. */
   234 //    public static Wrapper valueOf(Class<?> type) {
   235 //        if (isPrimitiveType(type))
   236 //            return forPrimitiveType(type);
   237 //        else
   238 //            return forWrapperType(type);
   239 //    }
   240 
   241     /** Return the wrapper that wraps values of the given type.
   242      *  The type may be {@code Object}, meaning the {@code OBJECT} wrapper.
   243      *  Otherwise, the type must be a primitive.
   244      *  @throws IllegalArgumentException for unexpected types
   245      */
   246     public static Wrapper forPrimitiveType(Class<?> type) {
   247         Wrapper w = findPrimitiveType(type);
   248         if (w != null)  return w;
   249         if (type.isPrimitive())
   250             throw new InternalError(); // redo hash function
   251         throw newIllegalArgumentException("not primitive: "+type);
   252     }
   253 
   254     static Wrapper findPrimitiveType(Class<?> type) {
   255         Wrapper w = FROM_PRIM[hashPrim(type)];
   256         if (w != null && w.primitiveType == type) {
   257             return w;
   258         }
   259         return null;
   260     }
   261 
   262     /** Return the wrapper that wraps values into the given wrapper type.
   263      *  If it is {@code Object}, return {@code OBJECT}.
   264      *  Otherwise, it must be a wrapper type.
   265      *  The type must not be a primitive type.
   266      *  @throws IllegalArgumentException for unexpected types
   267      */
   268     public static Wrapper forWrapperType(Class<?> type) {
   269         Wrapper w = findWrapperType(type);
   270         if (w != null)  return w;
   271         for (Wrapper x : values())
   272             if (x.wrapperType == type)
   273                 throw new InternalError(); // redo hash function
   274         throw newIllegalArgumentException("not wrapper: "+type);
   275     }
   276 
   277     static Wrapper findWrapperType(Class<?> type) {
   278         Wrapper w = FROM_WRAP[hashWrap(type)];
   279         if (w != null && w.wrapperType == type) {
   280             return w;
   281         }
   282         return null;
   283     }
   284 
   285     /** Return the wrapper that corresponds to the given bytecode
   286      *  signature character.  Return {@code OBJECT} for the character 'L'.
   287      *  @throws IllegalArgumentException for any non-signature character or {@code '['}.
   288      */
   289     public static Wrapper forBasicType(char type) {
   290         Wrapper w = FROM_CHAR[hashChar(type)];
   291         if (w != null && w.basicTypeChar == type) {
   292             return w;
   293         }
   294         for (Wrapper x : values())
   295             if (w.basicTypeChar == type)
   296                 throw new InternalError(); // redo hash function
   297         throw newIllegalArgumentException("not basic type char: "+type);
   298     }
   299 
   300     /** Return the wrapper for the given type, if it is
   301      *  a primitive type, else return {@code OBJECT}.
   302      */
   303     public static Wrapper forBasicType(Class<?> type) {
   304         if (type.isPrimitive())
   305             return forPrimitiveType(type);
   306         return OBJECT;  // any reference, including wrappers or arrays
   307     }
   308 
   309     // Note on perfect hashes:
   310     //   for signature chars c, do (c + (c >> 1)) % 16
   311     //   for primitive type names n, do (n[0] + n[2]) % 16
   312     // The type name hash works for both primitive and wrapper names.
   313     // You can add "java/lang/Object" to the primitive names.
   314     // But you add the wrapper name Object, use (n[2] + (3*n[1])) % 16.
   315     private static final Wrapper[] FROM_PRIM = new Wrapper[16];
   316     private static final Wrapper[] FROM_WRAP = new Wrapper[16];
   317     private static final Wrapper[] FROM_CHAR = new Wrapper[16];
   318     private static int hashPrim(Class<?> x) {
   319         String xn = x.getName();
   320         if (xn.length() < 3)  return 0;
   321         return (xn.charAt(0) + xn.charAt(2)) % 16;
   322     }
   323     private static int hashWrap(Class<?> x) {
   324         String xn = x.getName();
   325         final int offset = 10; assert(offset == "java.lang.".length());
   326         if (xn.length() < offset+3)  return 0;
   327         return (3*xn.charAt(offset+1) + xn.charAt(offset+2)) % 16;
   328     }
   329     private static int hashChar(char x) {
   330         return (x + (x >> 1)) % 16;
   331     }
   332     static {
   333         for (Wrapper w : values()) {
   334             int pi = hashPrim(w.primitiveType);
   335             int wi = hashWrap(w.wrapperType);
   336             int ci = hashChar(w.basicTypeChar);
   337             assert(FROM_PRIM[pi] == null);
   338             assert(FROM_WRAP[wi] == null);
   339             assert(FROM_CHAR[ci] == null);
   340             FROM_PRIM[pi] = w;
   341             FROM_WRAP[wi] = w;
   342             FROM_CHAR[ci] = w;
   343         }
   344         //assert(jdk.sun.invoke.util.WrapperTest.test(false));
   345     }
   346 
   347     /** What is the primitive type wrapped by this wrapper? */
   348     public Class<?> primitiveType() { return primitiveType; }
   349 
   350     /** What is the wrapper type for this wrapper? */
   351     public Class<?> wrapperType() { return wrapperType; }
   352 
   353     /** What is the wrapper type for this wrapper?
   354      * Otherwise, the example type must be the wrapper type,
   355      * or the corresponding primitive type.
   356      * (For {@code OBJECT}, the example type can be any non-primitive,
   357      * and is normalized to {@code Object.class}.)
   358      * The resulting class type has the same type parameter.
   359      */
   360     public <T> Class<T> wrapperType(Class<T> exampleType) {
   361         if (exampleType == wrapperType) {
   362             return exampleType;
   363         } else if (exampleType == primitiveType ||
   364                    wrapperType == Object.class ||
   365                    exampleType.isInterface()) {
   366             return forceType(wrapperType, exampleType);
   367         }
   368         throw newClassCastException(exampleType, primitiveType);
   369     }
   370 
   371     private static ClassCastException newClassCastException(Class<?> actual, Class<?> expected) {
   372         return new ClassCastException(actual + " is not compatible with " + expected);
   373     }
   374 
   375     /** If {@code type} is a primitive type, return the corresponding
   376      *  wrapper type, else return {@code type} unchanged.
   377      */
   378     public static <T> Class<T> asWrapperType(Class<T> type) {
   379         if (type.isPrimitive()) {
   380             return forPrimitiveType(type).wrapperType(type);
   381         }
   382         return type;
   383     }
   384 
   385     /** If {@code type} is a wrapper type, return the corresponding
   386      *  primitive type, else return {@code type} unchanged.
   387      */
   388     public static <T> Class<T> asPrimitiveType(Class<T> type) {
   389         Wrapper w = findWrapperType(type);
   390         if (w != null) {
   391             return forceType(w.primitiveType(), type);
   392         }
   393         return type;
   394     }
   395 
   396     /** Query:  Is the given type a wrapper, such as {@code Integer} or {@code Void}? */
   397     public static boolean isWrapperType(Class<?> type) {
   398         return findWrapperType(type) != null;
   399     }
   400 
   401     /** Query:  Is the given type a primitive, such as {@code int} or {@code void}? */
   402     public static boolean isPrimitiveType(Class<?> type) {
   403         return type.isPrimitive();
   404     }
   405 
   406     /** What is the bytecode signature character for this type?
   407      *  All non-primitives, including array types, report as 'L', the signature character for references.
   408      */
   409     public static char basicTypeChar(Class<?> type) {
   410         if (!type.isPrimitive())
   411             return 'L';
   412         else
   413             return forPrimitiveType(type).basicTypeChar();
   414     }
   415 
   416     /** What is the bytecode signature character for this wrapper's
   417      *  primitive type?
   418      */
   419     public char basicTypeChar() { return basicTypeChar; }
   420 
   421     /** What is the simple name of the wrapper type?
   422      */
   423     public String wrapperSimpleName() { return wrapperSimpleName; }
   424 
   425     /** What is the simple name of the primitive type?
   426      */
   427     public String primitiveSimpleName() { return primitiveSimpleName; }
   428 
   429 //    /** Wrap a value in the given type, which may be either a primitive or wrapper type.
   430 //     *  Performs standard primitive conversions, including truncation and float conversions.
   431 //     */
   432 //    public static <T> T wrap(Object x, Class<T> type) {
   433 //        return Wrapper.valueOf(type).cast(x, type);
   434 //    }
   435 
   436     /** Cast a wrapped value to the given type, which may be either a primitive or wrapper type.
   437      *  The given target type must be this wrapper's primitive or wrapper type.
   438      *  If this wrapper is OBJECT, the target type may also be an interface, perform no runtime check.
   439      *  Performs standard primitive conversions, including truncation and float conversions.
   440      *  The given type must be compatible with this wrapper.  That is, it must either
   441      *  be the wrapper type (or a subtype, in the case of {@code OBJECT}) or else
   442      *  it must be the wrapper's primitive type.
   443      *  Primitive conversions are only performed if the given type is itself a primitive.
   444      *  @throws ClassCastException if the given type is not compatible with this wrapper
   445      */
   446     public <T> T cast(Object x, Class<T> type) {
   447         return convert(x, type, true);
   448     }
   449 
   450     /** Convert a wrapped value to the given type.
   451      *  The given target type must be this wrapper's primitive or wrapper type.
   452      *  This is equivalent to {@link #cast}, except that it refuses to perform
   453      *  narrowing primitive conversions.
   454      */
   455     public <T> T convert(Object x, Class<T> type) {
   456         return convert(x, type, false);
   457     }
   458 
   459     private <T> T convert(Object x, Class<T> type, boolean isCast) {
   460         if (this == OBJECT) {
   461             // If the target wrapper is OBJECT, just do a reference cast.
   462             // If the target type is an interface, perform no runtime check.
   463             // (This loophole is safe, and is allowed by the JVM verifier.)
   464             // If the target type is a primitive, change it to a wrapper.
   465             assert(!type.isPrimitive());
   466             if (!type.isInterface())
   467                 type.cast(x);
   468             @SuppressWarnings("unchecked")
   469             T result = (T) x;  // unchecked warning is expected here
   470             return result;
   471         }
   472         Class<T> wtype = wrapperType(type);
   473         if (wtype.isInstance(x)) {
   474             return wtype.cast(x);
   475         }
   476         if (!isCast) {
   477             Class<?> sourceType = x.getClass();  // throw NPE if x is null
   478             Wrapper source = findWrapperType(sourceType);
   479             if (source == null || !this.isConvertibleFrom(source)) {
   480                 throw newClassCastException(wtype, sourceType);
   481             }
   482         } else if (x == null) {
   483             @SuppressWarnings("unchecked")
   484             T z = (T) zero;
   485             return z;
   486         }
   487         @SuppressWarnings("unchecked")
   488         T result = (T) wrap(x);  // unchecked warning is expected here
   489         assert (result == null ? Void.class : result.getClass()) == wtype;
   490         return result;
   491     }
   492 
   493     /** Cast a reference type to another reference type.
   494      * If the target type is an interface, perform no runtime check.
   495      * (This loophole is safe, and is allowed by the JVM verifier.)
   496      * If the target type is a primitive, change it to a wrapper.
   497      */
   498     static <T> Class<T> forceType(Class<?> type, Class<T> exampleType) {
   499         boolean z = (type == exampleType ||
   500                type.isPrimitive() && forPrimitiveType(type) == findWrapperType(exampleType) ||
   501                exampleType.isPrimitive() && forPrimitiveType(exampleType) == findWrapperType(type) ||
   502                type == Object.class && !exampleType.isPrimitive());
   503         if (!z)
   504             System.out.println(type+" <= "+exampleType);
   505         assert(type == exampleType ||
   506                type.isPrimitive() && forPrimitiveType(type) == findWrapperType(exampleType) ||
   507                exampleType.isPrimitive() && forPrimitiveType(exampleType) == findWrapperType(type) ||
   508                type == Object.class && !exampleType.isPrimitive());
   509         @SuppressWarnings("unchecked")
   510         Class<T> result = (Class<T>) type;  // unchecked warning is expected here
   511         return result;
   512     }
   513 
   514     /** Wrap a value in this wrapper's type.
   515      * Performs standard primitive conversions, including truncation and float conversions.
   516      * Performs returns the unchanged reference for {@code OBJECT}.
   517      * Returns null for {@code VOID}.
   518      * Returns a zero value for a null input.
   519      * @throws ClassCastException if this wrapper is numeric and the operand
   520      *                            is not a number, character, boolean, or null
   521      */
   522     public Object wrap(Object x) {
   523         // do non-numeric wrappers first
   524         switch (basicTypeChar) {
   525             case 'L': return x;
   526             case 'V': return null;
   527         }
   528         Number xn = numberValue(x);
   529         switch (basicTypeChar) {
   530             case 'I': return Integer.valueOf(xn.intValue());
   531             case 'J': return Long.valueOf(xn.longValue());
   532             case 'F': return Float.valueOf(xn.floatValue());
   533             case 'D': return Double.valueOf(xn.doubleValue());
   534             case 'S': return Short.valueOf((short) xn.intValue());
   535             case 'B': return Byte.valueOf((byte) xn.intValue());
   536             case 'C': return Character.valueOf((char) xn.intValue());
   537             case 'Z': return Boolean.valueOf(boolValue(xn.byteValue()));
   538         }
   539         throw new InternalError("bad wrapper");
   540     }
   541 
   542     /** Wrap a value (an int or smaller value) in this wrapper's type.
   543      * Performs standard primitive conversions, including truncation and float conversions.
   544      * Produces an {@code Integer} for {@code OBJECT}, although the exact type
   545      * of the operand is not known.
   546      * Returns null for {@code VOID}.
   547      */
   548     public Object wrap(int x) {
   549         if (basicTypeChar == 'L')  return (Integer)x;
   550         switch (basicTypeChar) {
   551             case 'L': throw newIllegalArgumentException("cannot wrap to object type");
   552             case 'V': return null;
   553             case 'I': return Integer.valueOf(x);
   554             case 'J': return Long.valueOf(x);
   555             case 'F': return Float.valueOf(x);
   556             case 'D': return Double.valueOf(x);
   557             case 'S': return Short.valueOf((short) x);
   558             case 'B': return Byte.valueOf((byte) x);
   559             case 'C': return Character.valueOf((char) x);
   560             case 'Z': return Boolean.valueOf(boolValue((byte) x));
   561         }
   562         throw new InternalError("bad wrapper");
   563     }
   564 
   565     private static Number numberValue(Object x) {
   566         if (x instanceof Number)     return (Number)x;
   567         if (x instanceof Character)  return (int)(Character)x;
   568         if (x instanceof Boolean)    return (Boolean)x ? 1 : 0;
   569         // Remaining allowed case of void:  Must be a null reference.
   570         return (Number)x;
   571     }
   572 
   573     // Parameter type of boolValue must be byte, because
   574     // MethodHandles.explicitCastArguments defines boolean
   575     // conversion as first converting to byte.
   576     private static boolean boolValue(byte bits) {
   577         bits &= 1;  // simple 31-bit zero extension
   578         return (bits != 0);
   579     }
   580 
   581     private static RuntimeException newIllegalArgumentException(String message, Object x) {
   582         return newIllegalArgumentException(message + x);
   583     }
   584     private static RuntimeException newIllegalArgumentException(String message) {
   585         return new IllegalArgumentException(message);
   586     }
   587 
   588     // primitive array support
   589     public Object makeArray(int len) {
   590         return java.lang.reflect.Array.newInstance(primitiveType, len);
   591     }
   592     public Class<?> arrayType() {
   593         return emptyArray.getClass();
   594     }
   595     public void copyArrayUnboxing(Object[] values, int vpos, Object a, int apos, int length) {
   596         if (a.getClass() != arrayType())
   597             arrayType().cast(a);  // throw NPE or CCE if bad type
   598         for (int i = 0; i < length; i++) {
   599             Object value = values[i+vpos];
   600             value = convert(value, primitiveType);
   601             java.lang.reflect.Array.set(a, i+apos, value);
   602         }
   603     }
   604     public void copyArrayBoxing(Object a, int apos, Object[] values, int vpos, int length) {
   605         if (a.getClass() != arrayType())
   606             arrayType().cast(a);  // throw NPE or CCE if bad type
   607         for (int i = 0; i < length; i++) {
   608             Object value = java.lang.reflect.Array.get(a, i+apos);
   609             //Already done: value = convert(value, primitiveType);
   610             assert(value.getClass() == wrapperType);
   611             values[i+vpos] = value;
   612         }
   613     }
   614 }