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