1193N/A/*
3793N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
1193N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1193N/A *
1193N/A * This code is free software; you can redistribute it and/or modify it
1193N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1193N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1193N/A *
1193N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1193N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1193N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1193N/A * version 2 for more details (a copy is included in the LICENSE file that
1193N/A * accompanied this code).
1193N/A *
1193N/A * You should have received a copy of the GNU General Public License version
1193N/A * 2 along with this work; if not, write to the Free Software Foundation,
1193N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1193N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1193N/A */
1193N/A
3793N/Apackage sun.invoke.util;
1193N/A
1193N/Apublic enum Wrapper {
4183N/A BOOLEAN(Boolean.class, boolean.class, 'Z', (Boolean)false, new boolean[0], Format.unsigned(1)),
3240N/A // These must be in the order defined for widening primitive conversions in JLS 5.1.2
4183N/A BYTE(Byte.class, byte.class, 'B', (Byte)(byte)0, new byte[0], Format.signed(8)),
4183N/A SHORT(Short.class, short.class, 'S', (Short)(short)0, new short[0], Format.signed(16)),
4183N/A CHAR(Character.class, char.class, 'C', (Character)(char)0, new char[0], Format.unsigned(16)),
5455N/A INT(Integer.class, int.class, 'I', (Integer)/*(int)*/0, new int[0], Format.signed(32)),
4183N/A LONG(Long.class, long.class, 'J', (Long)(long)0, new long[0], Format.signed(64)),
4183N/A FLOAT(Float.class, float.class, 'F', (Float)(float)0, new float[0], Format.floating(32)),
4183N/A DOUBLE(Double.class, double.class, 'D', (Double)(double)0, new double[0], Format.floating(64)),
4183N/A //NULL(Null.class, null.class, 'N', null, null, Format.other(1)),
4183N/A OBJECT(Object.class, Object.class, 'L', null, new Object[0], Format.other(1)),
3240N/A // VOID must be the last type, since it is "assignable" from any other type:
4183N/A VOID(Void.class, void.class, 'V', null, null, Format.other(0)),
1193N/A ;
1193N/A
1193N/A private final Class<?> wrapperType;
1193N/A private final Class<?> primitiveType;
1193N/A private final char basicTypeChar;
1193N/A private final Object zero;
4183N/A private final Object emptyArray;
1193N/A private final int format;
5459N/A private final String wrapperSimpleName;
5459N/A private final String primitiveSimpleName;
1193N/A
4183N/A private Wrapper(Class<?> wtype, Class<?> ptype, char tchar, Object zero, Object emptyArray, int format) {
1193N/A this.wrapperType = wtype;
1193N/A this.primitiveType = ptype;
1193N/A this.basicTypeChar = tchar;
1193N/A this.zero = zero;
4183N/A this.emptyArray = emptyArray;
1193N/A this.format = format;
5459N/A this.wrapperSimpleName = wtype.getSimpleName();
5459N/A this.primitiveSimpleName = ptype.getSimpleName();
1193N/A }
1193N/A
4183N/A /** For debugging, give the details of this wrapper. */
4183N/A public String detailString() {
5459N/A return wrapperSimpleName+
4183N/A java.util.Arrays.asList(wrapperType, primitiveType,
4183N/A basicTypeChar, zero,
4183N/A "0x"+Integer.toHexString(format));
4183N/A }
4183N/A
1193N/A private static abstract class Format {
1193N/A static final int SLOT_SHIFT = 0, SIZE_SHIFT = 2, KIND_SHIFT = 12;
1193N/A static final int
1193N/A SIGNED = (-1) << KIND_SHIFT,
1193N/A UNSIGNED = 0 << KIND_SHIFT,
1193N/A FLOATING = 1 << KIND_SHIFT;
1193N/A static final int
1193N/A SLOT_MASK = ((1<<(SIZE_SHIFT-SLOT_SHIFT))-1),
1193N/A SIZE_MASK = ((1<<(KIND_SHIFT-SIZE_SHIFT))-1);
1193N/A static int format(int kind, int size, int slots) {
1193N/A assert(((kind >> KIND_SHIFT) << KIND_SHIFT) == kind);
1193N/A assert((size & (size-1)) == 0); // power of two
1193N/A assert((kind == SIGNED) ? (size > 0) :
1193N/A (kind == UNSIGNED) ? (size > 0) :
1193N/A (kind == FLOATING) ? (size == 32 || size == 64) :
1193N/A false);
1193N/A assert((slots == 2) ? (size == 64) :
1193N/A (slots == 1) ? (size <= 32) :
1193N/A false);
1193N/A return kind | (size << SIZE_SHIFT) | (slots << SLOT_SHIFT);
1193N/A }
3240N/A static final int
1193N/A INT = SIGNED | (32 << SIZE_SHIFT) | (1 << SLOT_SHIFT),
3240N/A SHORT = SIGNED | (16 << SIZE_SHIFT) | (1 << SLOT_SHIFT),
1193N/A BOOLEAN = UNSIGNED | (1 << SIZE_SHIFT) | (1 << SLOT_SHIFT),
3240N/A CHAR = UNSIGNED | (16 << SIZE_SHIFT) | (1 << SLOT_SHIFT),
1193N/A FLOAT = FLOATING | (32 << SIZE_SHIFT) | (1 << SLOT_SHIFT),
1193N/A VOID = UNSIGNED | (0 << SIZE_SHIFT) | (0 << SLOT_SHIFT),
1193N/A NUM_MASK = (-1) << SIZE_SHIFT;
1193N/A static int signed(int size) { return format(SIGNED, size, (size > 32 ? 2 : 1)); }
1193N/A static int unsigned(int size) { return format(UNSIGNED, size, (size > 32 ? 2 : 1)); }
1193N/A static int floating(int size) { return format(FLOATING, size, (size > 32 ? 2 : 1)); }
1193N/A static int other(int slots) { return slots << SLOT_SHIFT; }
1193N/A }
1193N/A
1193N/A /// format queries:
1193N/A
1193N/A /** How many bits are in the wrapped value? Returns 0 for OBJECT or VOID. */
1193N/A public int bitWidth() { return (format >> Format.SIZE_SHIFT) & Format.SIZE_MASK; }
1193N/A /** How many JVM stack slots occupied by the wrapped value? Returns 0 for VOID. */
1193N/A public int stackSlots() { return (format >> Format.SLOT_SHIFT) & Format.SLOT_MASK; }
1193N/A /** Does the wrapped value occupy a single JVM stack slot? */
1193N/A public boolean isSingleWord() { return (format & (1 << Format.SLOT_SHIFT)) != 0; }
1193N/A /** Does the wrapped value occupy two JVM stack slots? */
1193N/A public boolean isDoubleWord() { return (format & (2 << Format.SLOT_SHIFT)) != 0; }
1193N/A /** Is the wrapped type numeric (not void or object)? */
1193N/A public boolean isNumeric() { return (format & Format.NUM_MASK) != 0; }
1193N/A /** Is the wrapped type a primitive other than float, double, or void? */
1193N/A public boolean isIntegral() { return isNumeric() && format < Format.FLOAT; }
1193N/A /** Is the wrapped type one of int, boolean, byte, char, or short? */
1193N/A public boolean isSubwordOrInt() { return isIntegral() && isSingleWord(); }
1193N/A /* Is the wrapped value a signed integral type (one of byte, short, int, or long)? */
1193N/A public boolean isSigned() { return format < Format.VOID; }
1193N/A /* Is the wrapped value an unsigned integral type (one of boolean or char)? */
1193N/A public boolean isUnsigned() { return format >= Format.BOOLEAN && format < Format.FLOAT; }
1193N/A /** Is the wrapped type either float or double? */
1193N/A public boolean isFloating() { return format >= Format.FLOAT; }
4183N/A /** Is the wrapped type either void or a reference? */
4183N/A public boolean isOther() { return (format & ~Format.SLOT_MASK) == 0; }
1193N/A
4183N/A /** Does the JLS 5.1.2 allow a variable of this wrapper's
3240N/A * primitive type to be assigned from a value of the given wrapper's primitive type?
3240N/A * Cases:
3240N/A * <ul>
3240N/A * <li>unboxing followed by widening primitive conversion
4183N/A * <li>any type converted to {@code void} (i.e., dropping a method call's value)
3240N/A * <li>boxing conversion followed by widening reference conversion to {@code Object}
3240N/A * </ul>
4250N/A * These are the cases allowed by MethodHandle.asType.
3240N/A */
3240N/A public boolean isConvertibleFrom(Wrapper source) {
3240N/A if (this == source) return true;
3240N/A if (this.compareTo(source) < 0) {
3240N/A // At best, this is a narrowing conversion.
3240N/A return false;
3240N/A }
4183N/A // All conversions are allowed in the enum order between floats and signed ints.
4183N/A // First detect non-signed non-float types (boolean, char, Object, void).
4183N/A boolean floatOrSigned = (((this.format & source.format) & Format.SIGNED) != 0);
4183N/A if (!floatOrSigned) {
4183N/A if (this.isOther()) return true;
4183N/A // can convert char to int or wider, but nothing else
4183N/A if (source.format == Format.CHAR) return true;
4183N/A // no other conversions are classified as widening
3240N/A return false;
3240N/A }
4183N/A // All signed and float conversions in the enum order are widening.
4183N/A assert(this.isFloating() || this.isSigned());
4183N/A assert(source.isFloating() || source.isSigned());
3240N/A return true;
3240N/A }
3240N/A
4183N/A static { assert(checkConvertibleFrom()); }
4183N/A private static boolean checkConvertibleFrom() {
4183N/A // Check the matrix for correct classification of widening conversions.
4183N/A for (Wrapper w : values()) {
4183N/A assert(w.isConvertibleFrom(w));
4183N/A assert(VOID.isConvertibleFrom(w));
4183N/A if (w != VOID) {
4183N/A assert(OBJECT.isConvertibleFrom(w));
4183N/A assert(!w.isConvertibleFrom(VOID));
4183N/A }
4183N/A // check relations with unsigned integral types:
4183N/A if (w != CHAR) {
4183N/A assert(!CHAR.isConvertibleFrom(w));
4183N/A if (!w.isConvertibleFrom(INT))
4183N/A assert(!w.isConvertibleFrom(CHAR));
4183N/A }
4183N/A if (w != BOOLEAN) {
4183N/A assert(!BOOLEAN.isConvertibleFrom(w));
4183N/A if (w != VOID && w != OBJECT)
4183N/A assert(!w.isConvertibleFrom(BOOLEAN));
4183N/A }
4183N/A // check relations with signed integral types:
4183N/A if (w.isSigned()) {
4183N/A for (Wrapper x : values()) {
4183N/A if (w == x) continue;
4183N/A if (x.isFloating())
4183N/A assert(!w.isConvertibleFrom(x));
4183N/A else if (x.isSigned()) {
4183N/A if (w.compareTo(x) < 0)
4183N/A assert(!w.isConvertibleFrom(x));
4183N/A else
4183N/A assert(w.isConvertibleFrom(x));
4183N/A }
4183N/A }
4183N/A }
4183N/A // check relations with floating types:
4183N/A if (w.isFloating()) {
4183N/A for (Wrapper x : values()) {
4183N/A if (w == x) continue;
4183N/A if (x.isSigned())
4183N/A assert(w.isConvertibleFrom(x));
4183N/A else if (x.isFloating()) {
4183N/A if (w.compareTo(x) < 0)
4183N/A assert(!w.isConvertibleFrom(x));
4183N/A else
4183N/A assert(w.isConvertibleFrom(x));
4183N/A }
4183N/A }
4183N/A }
4183N/A }
4183N/A return true; // i.e., assert(true)
4183N/A }
4183N/A
1193N/A /** Produce a zero value for the given wrapper type.
1193N/A * This will be a numeric zero for a number or character,
1193N/A * false for a boolean, and null for a reference or void.
1193N/A * The common thread is that this is what is contained
1193N/A * in a default-initialized variable of the given primitive
1193N/A * type. (For void, it is what a reflective method returns
1193N/A * instead of no value at all.)
1193N/A */
1193N/A public Object zero() { return zero; }
1193N/A
1193N/A /** Produce a zero value for the given wrapper type T.
3240N/A * The optional argument must a type compatible with this wrapper.
1193N/A * Equivalent to {@code this.cast(this.zero(), type)}.
1193N/A */
3240N/A public <T> T zero(Class<T> type) { return convert(zero, type); }
1193N/A
1193N/A// /** Produce a wrapper for the given wrapper or primitive type. */
1193N/A// public static Wrapper valueOf(Class<?> type) {
1193N/A// if (isPrimitiveType(type))
1193N/A// return forPrimitiveType(type);
1193N/A// else
1193N/A// return forWrapperType(type);
1193N/A// }
1193N/A
1193N/A /** Return the wrapper that wraps values of the given type.
1193N/A * The type may be {@code Object}, meaning the {@code OBJECT} wrapper.
1193N/A * Otherwise, the type must be a primitive.
1193N/A * @throws IllegalArgumentException for unexpected types
1193N/A */
1193N/A public static Wrapper forPrimitiveType(Class<?> type) {
2040N/A Wrapper w = findPrimitiveType(type);
2040N/A if (w != null) return w;
2040N/A if (type.isPrimitive())
2040N/A throw new InternalError(); // redo hash function
2040N/A throw newIllegalArgumentException("not primitive: "+type);
2040N/A }
2040N/A
2040N/A static Wrapper findPrimitiveType(Class<?> type) {
1193N/A Wrapper w = FROM_PRIM[hashPrim(type)];
1193N/A if (w != null && w.primitiveType == type) {
1193N/A return w;
1193N/A }
2040N/A return null;
1193N/A }
1193N/A
1193N/A /** Return the wrapper that wraps values into the given wrapper type.
4244N/A * If it is {@code Object}, return {@code OBJECT}.
1193N/A * Otherwise, it must be a wrapper type.
1193N/A * The type must not be a primitive type.
1193N/A * @throws IllegalArgumentException for unexpected types
1193N/A */
1193N/A public static Wrapper forWrapperType(Class<?> type) {
1193N/A Wrapper w = findWrapperType(type);
1193N/A if (w != null) return w;
1193N/A for (Wrapper x : values())
2040N/A if (x.wrapperType == type)
1193N/A throw new InternalError(); // redo hash function
1193N/A throw newIllegalArgumentException("not wrapper: "+type);
1193N/A }
1193N/A
1193N/A static Wrapper findWrapperType(Class<?> type) {
1193N/A Wrapper w = FROM_WRAP[hashWrap(type)];
1193N/A if (w != null && w.wrapperType == type) {
1193N/A return w;
1193N/A }
1193N/A return null;
1193N/A }
1193N/A
1193N/A /** Return the wrapper that corresponds to the given bytecode
1193N/A * signature character. Return {@code OBJECT} for the character 'L'.
1193N/A * @throws IllegalArgumentException for any non-signature character or {@code '['}.
1193N/A */
1193N/A public static Wrapper forBasicType(char type) {
1193N/A Wrapper w = FROM_CHAR[hashChar(type)];
1193N/A if (w != null && w.basicTypeChar == type) {
1193N/A return w;
1193N/A }
1193N/A for (Wrapper x : values())
1193N/A if (w.basicTypeChar == type)
1193N/A throw new InternalError(); // redo hash function
1193N/A throw newIllegalArgumentException("not basic type char: "+type);
1193N/A }
1193N/A
1193N/A /** Return the wrapper for the given type, if it is
1193N/A * a primitive type, else return {@code OBJECT}.
1193N/A */
1193N/A public static Wrapper forBasicType(Class<?> type) {
1193N/A if (type.isPrimitive())
1193N/A return forPrimitiveType(type);
1193N/A return OBJECT; // any reference, including wrappers or arrays
1193N/A }
1193N/A
1193N/A // Note on perfect hashes:
1193N/A // for signature chars c, do (c + (c >> 1)) % 16
1193N/A // for primitive type names n, do (n[0] + n[2]) % 16
1193N/A // The type name hash works for both primitive and wrapper names.
1193N/A // You can add "java/lang/Object" to the primitive names.
1193N/A // But you add the wrapper name Object, use (n[2] + (3*n[1])) % 16.
1193N/A private static final Wrapper[] FROM_PRIM = new Wrapper[16];
1193N/A private static final Wrapper[] FROM_WRAP = new Wrapper[16];
1193N/A private static final Wrapper[] FROM_CHAR = new Wrapper[16];
1193N/A private static int hashPrim(Class<?> x) {
1193N/A String xn = x.getName();
1193N/A if (xn.length() < 3) return 0;
1193N/A return (xn.charAt(0) + xn.charAt(2)) % 16;
1193N/A }
1193N/A private static int hashWrap(Class<?> x) {
1193N/A String xn = x.getName();
1193N/A final int offset = 10; assert(offset == "java.lang.".length());
1193N/A if (xn.length() < offset+3) return 0;
1193N/A return (3*xn.charAt(offset+1) + xn.charAt(offset+2)) % 16;
1193N/A }
1193N/A private static int hashChar(char x) {
1193N/A return (x + (x >> 1)) % 16;
1193N/A }
1193N/A static {
1193N/A for (Wrapper w : values()) {
1193N/A int pi = hashPrim(w.primitiveType);
1193N/A int wi = hashWrap(w.wrapperType);
1193N/A int ci = hashChar(w.basicTypeChar);
1193N/A assert(FROM_PRIM[pi] == null);
1193N/A assert(FROM_WRAP[wi] == null);
1193N/A assert(FROM_CHAR[ci] == null);
1193N/A FROM_PRIM[pi] = w;
1193N/A FROM_WRAP[wi] = w;
1193N/A FROM_CHAR[ci] = w;
1193N/A }
3793N/A //assert(jdk.sun.invoke.util.WrapperTest.test(false));
1193N/A }
1193N/A
1193N/A /** What is the primitive type wrapped by this wrapper? */
1193N/A public Class<?> primitiveType() { return primitiveType; }
1193N/A
1193N/A /** What is the wrapper type for this wrapper? */
1193N/A public Class<?> wrapperType() { return wrapperType; }
1193N/A
1193N/A /** What is the wrapper type for this wrapper?
2040N/A * Otherwise, the example type must be the wrapper type,
1193N/A * or the corresponding primitive type.
2040N/A * (For {@code OBJECT}, the example type can be any non-primitive,
2040N/A * and is normalized to {@code Object.class}.)
1193N/A * The resulting class type has the same type parameter.
1193N/A */
1193N/A public <T> Class<T> wrapperType(Class<T> exampleType) {
1193N/A if (exampleType == wrapperType) {
1193N/A return exampleType;
1193N/A } else if (exampleType == primitiveType ||
1193N/A wrapperType == Object.class ||
1193N/A exampleType.isInterface()) {
1193N/A return forceType(wrapperType, exampleType);
1193N/A }
3240N/A throw newClassCastException(exampleType, primitiveType);
3240N/A }
3240N/A
3240N/A private static ClassCastException newClassCastException(Class<?> actual, Class<?> expected) {
3240N/A return new ClassCastException(actual + " is not compatible with " + expected);
1193N/A }
1193N/A
1193N/A /** If {@code type} is a primitive type, return the corresponding
1193N/A * wrapper type, else return {@code type} unchanged.
1193N/A */
1193N/A public static <T> Class<T> asWrapperType(Class<T> type) {
1193N/A if (type.isPrimitive()) {
1193N/A return forPrimitiveType(type).wrapperType(type);
1193N/A }
1193N/A return type;
1193N/A }
1193N/A
1193N/A /** If {@code type} is a wrapper type, return the corresponding
1193N/A * primitive type, else return {@code type} unchanged.
1193N/A */
1193N/A public static <T> Class<T> asPrimitiveType(Class<T> type) {
1193N/A Wrapper w = findWrapperType(type);
1193N/A if (w != null) {
1193N/A return forceType(w.primitiveType(), type);
1193N/A }
1193N/A return type;
1193N/A }
1193N/A
1193N/A /** Query: Is the given type a wrapper, such as {@code Integer} or {@code Void}? */
1193N/A public static boolean isWrapperType(Class<?> type) {
1193N/A return findWrapperType(type) != null;
1193N/A }
1193N/A
1193N/A /** Query: Is the given type a primitive, such as {@code int} or {@code void}? */
1193N/A public static boolean isPrimitiveType(Class<?> type) {
1193N/A return type.isPrimitive();
1193N/A }
1193N/A
2040N/A /** What is the bytecode signature character for this type?
2040N/A * All non-primitives, including array types, report as 'L', the signature character for references.
2040N/A */
2040N/A public static char basicTypeChar(Class<?> type) {
2040N/A if (!type.isPrimitive())
2040N/A return 'L';
2040N/A else
2040N/A return forPrimitiveType(type).basicTypeChar();
2040N/A }
2040N/A
1193N/A /** What is the bytecode signature character for this wrapper's
1193N/A * primitive type?
1193N/A */
1193N/A public char basicTypeChar() { return basicTypeChar; }
1193N/A
1193N/A /** What is the simple name of the wrapper type?
1193N/A */
5459N/A public String wrapperSimpleName() { return wrapperSimpleName; }
5459N/A
5459N/A /** What is the simple name of the primitive type?
5459N/A */
5459N/A public String primitiveSimpleName() { return primitiveSimpleName; }
1193N/A
1193N/A// /** Wrap a value in the given type, which may be either a primitive or wrapper type.
1193N/A// * Performs standard primitive conversions, including truncation and float conversions.
1193N/A// */
1193N/A// public static <T> T wrap(Object x, Class<T> type) {
1193N/A// return Wrapper.valueOf(type).cast(x, type);
1193N/A// }
1193N/A
1193N/A /** Cast a wrapped value to the given type, which may be either a primitive or wrapper type.
3240N/A * The given target type must be this wrapper's primitive or wrapper type.
3240N/A * If this wrapper is OBJECT, the target type may also be an interface, perform no runtime check.
1193N/A * Performs standard primitive conversions, including truncation and float conversions.
1193N/A * The given type must be compatible with this wrapper. That is, it must either
2040N/A * be the wrapper type (or a subtype, in the case of {@code OBJECT}) or else
1193N/A * it must be the wrapper's primitive type.
3240N/A * Primitive conversions are only performed if the given type is itself a primitive.
1193N/A * @throws ClassCastException if the given type is not compatible with this wrapper
1193N/A */
1193N/A public <T> T cast(Object x, Class<T> type) {
3240N/A return convert(x, type, true);
3240N/A }
3240N/A
3240N/A /** Convert a wrapped value to the given type.
3240N/A * The given target type must be this wrapper's primitive or wrapper type.
3240N/A * This is equivalent to {@link #cast}, except that it refuses to perform
3240N/A * narrowing primitive conversions.
3240N/A */
3240N/A public <T> T convert(Object x, Class<T> type) {
3240N/A return convert(x, type, false);
3240N/A }
3240N/A
3240N/A private <T> T convert(Object x, Class<T> type, boolean isCast) {
3240N/A if (this == OBJECT) {
3240N/A // If the target wrapper is OBJECT, just do a reference cast.
3240N/A // If the target type is an interface, perform no runtime check.
3240N/A // (This loophole is safe, and is allowed by the JVM verifier.)
3240N/A // If the target type is a primitive, change it to a wrapper.
5977N/A assert(!type.isPrimitive());
5977N/A if (!type.isInterface())
5977N/A type.cast(x);
3240N/A @SuppressWarnings("unchecked")
3240N/A T result = (T) x; // unchecked warning is expected here
3240N/A return result;
3240N/A }
1193N/A Class<T> wtype = wrapperType(type);
3240N/A if (wtype.isInstance(x)) {
5459N/A return wtype.cast(x);
3240N/A }
3240N/A if (!isCast) {
5459N/A Class<?> sourceType = x.getClass(); // throw NPE if x is null
3240N/A Wrapper source = findWrapperType(sourceType);
3240N/A if (source == null || !this.isConvertibleFrom(source)) {
3240N/A throw newClassCastException(wtype, sourceType);
3240N/A }
5459N/A } else if (x == null) {
5459N/A @SuppressWarnings("unchecked")
5459N/A T z = (T) zero;
5459N/A return z;
3240N/A }
3240N/A @SuppressWarnings("unchecked")
3240N/A T result = (T) wrap(x); // unchecked warning is expected here
5459N/A assert (result == null ? Void.class : result.getClass()) == wtype;
3240N/A return result;
1193N/A }
1193N/A
1193N/A /** Cast a reference type to another reference type.
1193N/A * If the target type is an interface, perform no runtime check.
1193N/A * (This loophole is safe, and is allowed by the JVM verifier.)
1193N/A * If the target type is a primitive, change it to a wrapper.
1193N/A */
1193N/A static <T> Class<T> forceType(Class<?> type, Class<T> exampleType) {
2040N/A boolean z = (type == exampleType ||
2040N/A type.isPrimitive() && forPrimitiveType(type) == findWrapperType(exampleType) ||
2040N/A exampleType.isPrimitive() && forPrimitiveType(exampleType) == findWrapperType(type) ||
2040N/A type == Object.class && !exampleType.isPrimitive());
2040N/A if (!z)
2040N/A System.out.println(type+" <= "+exampleType);
1193N/A assert(type == exampleType ||
2040N/A type.isPrimitive() && forPrimitiveType(type) == findWrapperType(exampleType) ||
2040N/A exampleType.isPrimitive() && forPrimitiveType(exampleType) == findWrapperType(type) ||
2040N/A type == Object.class && !exampleType.isPrimitive());
2040N/A @SuppressWarnings("unchecked")
1193N/A Class<T> result = (Class<T>) type; // unchecked warning is expected here
1193N/A return result;
1193N/A }
1193N/A
1193N/A /** Wrap a value in this wrapper's type.
1193N/A * Performs standard primitive conversions, including truncation and float conversions.
1193N/A * Performs returns the unchanged reference for {@code OBJECT}.
1193N/A * Returns null for {@code VOID}.
1193N/A * Returns a zero value for a null input.
1193N/A * @throws ClassCastException if this wrapper is numeric and the operand
1193N/A * is not a number, character, boolean, or null
1193N/A */
1193N/A public Object wrap(Object x) {
1193N/A // do non-numeric wrappers first
1193N/A switch (basicTypeChar) {
1193N/A case 'L': return x;
1193N/A case 'V': return null;
1193N/A }
1193N/A Number xn = numberValue(x);
1193N/A switch (basicTypeChar) {
1193N/A case 'I': return Integer.valueOf(xn.intValue());
1193N/A case 'J': return Long.valueOf(xn.longValue());
1193N/A case 'F': return Float.valueOf(xn.floatValue());
1193N/A case 'D': return Double.valueOf(xn.doubleValue());
1193N/A case 'S': return Short.valueOf((short) xn.intValue());
1193N/A case 'B': return Byte.valueOf((byte) xn.intValue());
1193N/A case 'C': return Character.valueOf((char) xn.intValue());
5459N/A case 'Z': return Boolean.valueOf(boolValue(xn.byteValue()));
1193N/A }
1193N/A throw new InternalError("bad wrapper");
1193N/A }
1193N/A
1193N/A /** Wrap a value (an int or smaller value) in this wrapper's type.
1193N/A * Performs standard primitive conversions, including truncation and float conversions.
1193N/A * Produces an {@code Integer} for {@code OBJECT}, although the exact type
1193N/A * of the operand is not known.
1193N/A * Returns null for {@code VOID}.
1193N/A */
1193N/A public Object wrap(int x) {
1193N/A if (basicTypeChar == 'L') return (Integer)x;
1193N/A switch (basicTypeChar) {
1193N/A case 'L': throw newIllegalArgumentException("cannot wrap to object type");
1193N/A case 'V': return null;
5455N/A case 'I': return Integer.valueOf(x);
1193N/A case 'J': return Long.valueOf(x);
1193N/A case 'F': return Float.valueOf(x);
1193N/A case 'D': return Double.valueOf(x);
1193N/A case 'S': return Short.valueOf((short) x);
1193N/A case 'B': return Byte.valueOf((byte) x);
1193N/A case 'C': return Character.valueOf((char) x);
5459N/A case 'Z': return Boolean.valueOf(boolValue((byte) x));
1193N/A }
1193N/A throw new InternalError("bad wrapper");
1193N/A }
1193N/A
1193N/A private static Number numberValue(Object x) {
1193N/A if (x instanceof Number) return (Number)x;
1193N/A if (x instanceof Character) return (int)(Character)x;
1193N/A if (x instanceof Boolean) return (Boolean)x ? 1 : 0;
1193N/A // Remaining allowed case of void: Must be a null reference.
1193N/A return (Number)x;
1193N/A }
1193N/A
5459N/A // Parameter type of boolValue must be byte, because
5459N/A // MethodHandles.explicitCastArguments defines boolean
5459N/A // conversion as first converting to byte.
5459N/A private static boolean boolValue(byte bits) {
4183N/A bits &= 1; // simple 31-bit zero extension
1193N/A return (bits != 0);
1193N/A }
1193N/A
1193N/A private static RuntimeException newIllegalArgumentException(String message, Object x) {
1193N/A return newIllegalArgumentException(message + x);
1193N/A }
1193N/A private static RuntimeException newIllegalArgumentException(String message) {
1193N/A return new IllegalArgumentException(message);
1193N/A }
4183N/A
4183N/A // primitive array support
4183N/A public Object makeArray(int len) {
4183N/A return java.lang.reflect.Array.newInstance(primitiveType, len);
4183N/A }
4183N/A public Class<?> arrayType() {
4183N/A return emptyArray.getClass();
4183N/A }
4183N/A public void copyArrayUnboxing(Object[] values, int vpos, Object a, int apos, int length) {
4183N/A if (a.getClass() != arrayType())
4183N/A arrayType().cast(a); // throw NPE or CCE if bad type
4183N/A for (int i = 0; i < length; i++) {
4183N/A Object value = values[i+vpos];
4183N/A value = convert(value, primitiveType);
4183N/A java.lang.reflect.Array.set(a, i+apos, value);
4183N/A }
4183N/A }
4183N/A public void copyArrayBoxing(Object a, int apos, Object[] values, int vpos, int length) {
4183N/A if (a.getClass() != arrayType())
4183N/A arrayType().cast(a); // throw NPE or CCE if bad type
4183N/A for (int i = 0; i < length; i++) {
4183N/A Object value = java.lang.reflect.Array.get(a, i+apos);
4183N/A //Already done: value = convert(value, primitiveType);
4183N/A assert(value.getClass() == wrapperType);
4183N/A values[i+vpos] = value;
4183N/A }
4183N/A }
1193N/A}