661 lines · 28.5 KB · Java
RawDownload
    }     /** Wrap a value in this wrapper's type.     * Performs standard primitive conversions, including truncation and float conversions.     * Performs returns the unchanged reference for {@code OBJECT}.     * Returns null for {@code VOID}.     * Returns a zero value for a null input.     * @throws ClassCastException if this wrapper is numeric and the operand     *                            is not a number, character, boolean, or null     */    public Object wrap(Object x) {        // do non-numeric wrappers first        switch (basicTypeChar) {            case 'L': return x;            case 'V': return null;        }        Number xn = numberValue(x);        switch (basicTypeChar) {            case 'I': return Integer.valueOf(xn.intValue());            case 'J': return Long.valueOf(xn.longValue());            case 'F': return Float.valueOf(xn.floatValue());            case 'D': return Double.valueOf(xn.doubleValue());            case 'S': return Short.valueOf((short) xn.intValue());            case 'B': return Byte.valueOf((byte) xn.intValue());            case 'C': return Character.valueOf((char) xn.intValue());            case 'Z': return Boolean.valueOf(boolValue(xn.longValue()));        }        throw new InternalError("bad wrapper");    }     /** Wrap a value (an int or smaller value) in this wrapper's type.     * Performs standard primitive conversions, including truncation and float conversions.     * Produces an {@code Integer} for {@code OBJECT}, although the exact type     * of the operand is not known.     * Returns null for {@code VOID}.     */    public Object wrap(int x) {        if (basicTypeChar == 'L')  return (Integer)x;        switch (basicTypeChar) {            case 'L': throw newIllegalArgumentException("cannot wrap to object type");            case 'V': return null;            case 'I': return Integer.valueOf((int)x);            case 'J': return Long.valueOf(x);            case 'F': return Float.valueOf(x);            case 'D': return Double.valueOf(x);            case 'S': return Short.valueOf((short) x);            case 'B': return Byte.valueOf((byte) x);            case 'C': return Character.valueOf((char) x);            case 'Z': return Boolean.valueOf(boolValue(x));        }        throw new InternalError("bad wrapper");    }     /** Wrap a value (a long or smaller value) in this wrapper's type.     * Does not perform floating point conversion.     * Produces a {@code Long} for {@code OBJECT}, although the exact type     * of the operand is not known.     * Returns null for {@code VOID}.     */    public Object wrapRaw(long x) {        switch (basicTypeChar) {            case 'F':  return Float.valueOf(Float.intBitsToFloat((int)x));            case 'D':  return Double.valueOf(Double.longBitsToDouble(x));            case 'L':  // same as 'J':            case 'J':  return (Long) x;        }        // Other wrapping operations are just the same, given that the        // operand is already promoted to an int.        return wrap((int)x);    }     /** Produce bitwise value which encodes the given wrapped value.     * Does not perform floating point conversion.     * Returns zero for {@code VOID}.     */    public long unwrapRaw(Object x) {        switch (basicTypeChar) {            case 'F':  return Float.floatToRawIntBits((Float) x);            case 'D':  return Double.doubleToRawLongBits((Double) x);             case 'L': throw newIllegalArgumentException("cannot unwrap from sobject type");            case 'V': return 0;            case 'I': return (int)(Integer) x;            case 'J': return (long)(Long) x;            case 'S': return (short)(Short) x;            case 'B': return (byte)(Byte) x;            case 'C': return (char)(Character) x;            case 'Z': return (boolean)(Boolean) x ? 1 : 0;        }        throw new InternalError("bad wrapper");    }     /** Report what primitive type holds this guy's raw value. */    public Class<?> rawPrimitiveType() {        return rawPrimitive().primitiveType();    }     /** Report, as a wrapper, what primitive type holds this guy's raw value.     *  Returns self for INT, LONG, OBJECT; returns LONG for DOUBLE,     *  else returns INT.     */    public Wrapper rawPrimitive() {        switch (basicTypeChar) {            case 'S': case 'B':            case 'C': case 'Z':            case 'V':            case 'F':                return INT;            case 'D':                return LONG;        }        return this;    }     private static Number numberValue(Object x) {        if (x instanceof Number)     return (Number)x;        if (x instanceof Character)  return (int)(Character)x;        if (x instanceof Boolean)    return (Boolean)x ? 1 : 0;        // Remaining allowed case of void:  Must be a null reference.        return (Number)x;    }     private static boolean boolValue(long bits) {        bits &= 1;  // simple 31-bit zero extension        return (bits != 0);    }     private static RuntimeException newIllegalArgumentException(String message, Object x) {        return newIllegalArgumentException(message + x);    }    private static RuntimeException newIllegalArgumentException(String message) {        return new IllegalArgumentException(message);    }     // primitive array support    public Object makeArray(int len) {        return java.lang.reflect.Array.newInstance(primitiveType, len);    }    public Class<?> arrayType() {        return emptyArray.getClass();    }    public void copyArrayUnboxing(Object[] values, int vpos, Object a, int apos, int length) {        if (a.getClass() != arrayType())            arrayType().cast(a);  // throw NPE or CCE if bad type        for (int i = 0; i < length; i++) {            Object value = values[i+vpos];            value = convert(value, primitiveType);            java.lang.reflect.Array.set(a, i+apos, value);        }    }    public void copyArrayBoxing(Object a, int apos, Object[] values, int vpos, int length) {        if (a.getClass() != arrayType())            arrayType().cast(a);  // throw NPE or CCE if bad type        for (int i = 0; i < length; i++) {            Object value = java.lang.reflect.Array.get(a, i+apos);            //Already done: value = convert(value, primitiveType);            assert(value.getClass() == wrapperType);            values[i+vpos] = value;        }    }}

Lines 501–661 of 661. Previous lines