/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
public enum Wrapper {
// These must be in the order defined for widening primitive conversions in JLS 5.1.2
//NULL(Null.class, null.class, 'N', null, null, Format.other(1)),
// VOID must be the last type, since it is "assignable" from any other type:
;
private final char basicTypeChar;
private final int format;
private Wrapper(Class<?> wtype, Class<?> ptype, char tchar, Object zero, Object emptyArray, int format) {
this.wrapperType = wtype;
this.primitiveType = ptype;
this.basicTypeChar = tchar;
this.emptyArray = emptyArray;
}
/** For debugging, give the details of this wrapper. */
return wrapperSimpleName+
}
private static abstract class Format {
static final int
static final int
false);
false);
}
static final int
}
/// format queries:
/** How many bits are in the wrapped value? Returns 0 for OBJECT or VOID. */
/** How many JVM stack slots occupied by the wrapped value? Returns 0 for VOID. */
/** Does the wrapped value occupy a single JVM stack slot? */
/** Does the wrapped value occupy two JVM stack slots? */
/** Is the wrapped type numeric (not void or object)? */
/** Is the wrapped type a primitive other than float, double, or void? */
/** Is the wrapped type one of int, boolean, byte, char, or short? */
/* Is the wrapped value a signed integral type (one of byte, short, int, or long)? */
/* Is the wrapped value an unsigned integral type (one of boolean or char)? */
/** Is the wrapped type either float or double? */
/** Is the wrapped type either void or a reference? */
/** Does the JLS 5.1.2 allow a variable of this wrapper's
* primitive type to be assigned from a value of the given wrapper's primitive type?
* Cases:
* <ul>
* <li>unboxing followed by widening primitive conversion
* <li>any type converted to {@code void} (i.e., dropping a method call's value)
* <li>boxing conversion followed by widening reference conversion to {@code Object}
* </ul>
* These are the cases allowed by MethodHandle.asType.
*/
if (this == source) return true;
// At best, this is a narrowing conversion.
return false;
}
// All conversions are allowed in the enum order between floats and signed ints.
// First detect non-signed non-float types (boolean, char, Object, void).
if (!floatOrSigned) {
if (this.isOther()) return true;
// can convert char to int or wider, but nothing else
// no other conversions are classified as widening
return false;
}
// All signed and float conversions in the enum order are widening.
assert(this.isFloating() || this.isSigned());
return true;
}
static { assert(checkConvertibleFrom()); }
private static boolean checkConvertibleFrom() {
// Check the matrix for correct classification of widening conversions.
assert(w.isConvertibleFrom(w));
assert(VOID.isConvertibleFrom(w));
if (w != VOID) {
assert(OBJECT.isConvertibleFrom(w));
assert(!w.isConvertibleFrom(VOID));
}
// check relations with unsigned integral types:
if (w != CHAR) {
assert(!CHAR.isConvertibleFrom(w));
if (!w.isConvertibleFrom(INT))
assert(!w.isConvertibleFrom(CHAR));
}
if (w != BOOLEAN) {
assert(!BOOLEAN.isConvertibleFrom(w));
assert(!w.isConvertibleFrom(BOOLEAN));
}
// check relations with signed integral types:
if (w.isSigned()) {
if (w == x) continue;
if (x.isFloating())
assert(!w.isConvertibleFrom(x));
else if (x.isSigned()) {
if (w.compareTo(x) < 0)
assert(!w.isConvertibleFrom(x));
else
assert(w.isConvertibleFrom(x));
}
}
}
// check relations with floating types:
if (w.isFloating()) {
if (w == x) continue;
if (x.isSigned())
assert(w.isConvertibleFrom(x));
else if (x.isFloating()) {
if (w.compareTo(x) < 0)
assert(!w.isConvertibleFrom(x));
else
assert(w.isConvertibleFrom(x));
}
}
}
}
return true; // i.e., assert(true)
}
/** Produce a zero value for the given wrapper type.
* This will be a numeric zero for a number or character,
* false for a boolean, and null for a reference or void.
* The common thread is that this is what is contained
* in a default-initialized variable of the given primitive
* type. (For void, it is what a reflective method returns
* instead of no value at all.)
*/
/** Produce a zero value for the given wrapper type T.
* The optional argument must a type compatible with this wrapper.
* Equivalent to {@code this.cast(this.zero(), type)}.
*/
// /** Produce a wrapper for the given wrapper or primitive type. */
// public static Wrapper valueOf(Class<?> type) {
// if (isPrimitiveType(type))
// return forPrimitiveType(type);
// else
// return forWrapperType(type);
// }
/** Return the wrapper that wraps values of the given type.
* The type may be {@code Object}, meaning the {@code OBJECT} wrapper.
* Otherwise, the type must be a primitive.
* @throws IllegalArgumentException for unexpected types
*/
if (w != null) return w;
if (type.isPrimitive())
throw new InternalError(); // redo hash function
}
return w;
}
return null;
}
/** Return the wrapper that wraps values into the given wrapper type.
* If it is {@code Object}, return {@code OBJECT}.
* Otherwise, it must be a wrapper type.
* The type must not be a primitive type.
* @throws IllegalArgumentException for unexpected types
*/
if (w != null) return w;
if (x.wrapperType == type)
throw new InternalError(); // redo hash function
}
return w;
}
return null;
}
/** Return the wrapper that corresponds to the given bytecode
* signature character. Return {@code OBJECT} for the character 'L'.
* @throws IllegalArgumentException for any non-signature character or {@code '['}.
*/
return w;
}
if (w.basicTypeChar == type)
throw new InternalError(); // redo hash function
}
/** Return the wrapper for the given type, if it is
* a primitive type, else return {@code OBJECT}.
*/
if (type.isPrimitive())
return forPrimitiveType(type);
return OBJECT; // any reference, including wrappers or arrays
}
// Note on perfect hashes:
// for signature chars c, do (c + (c >> 1)) % 16
// for primitive type names n, do (n[0] + n[2]) % 16
// The type name hash works for both primitive and wrapper names.
// But you add the wrapper name Object, use (n[2] + (3*n[1])) % 16.
}
}
private static int hashChar(char x) {
return (x + (x >> 1)) % 16;
}
static {
}
//assert(jdk.sun.invoke.util.WrapperTest.test(false));
}
/** What is the primitive type wrapped by this wrapper? */
/** What is the wrapper type for this wrapper? */
/** What is the wrapper type for this wrapper?
* Otherwise, the example type must be the wrapper type,
* or the corresponding primitive type.
* (For {@code OBJECT}, the example type can be any non-primitive,
* and is normalized to {@code Object.class}.)
* The resulting class type has the same type parameter.
*/
if (exampleType == wrapperType) {
return exampleType;
} else if (exampleType == primitiveType ||
wrapperType == Object.class ||
exampleType.isInterface()) {
}
}
}
/** If {@code type} is a primitive type, return the corresponding
* wrapper type, else return {@code type} unchanged.
*/
if (type.isPrimitive()) {
}
return type;
}
/** If {@code type} is a wrapper type, return the corresponding
* primitive type, else return {@code type} unchanged.
*/
if (w != null) {
}
return type;
}
/** Query: Is the given type a wrapper, such as {@code Integer} or {@code Void}? */
}
/** Query: Is the given type a primitive, such as {@code int} or {@code void}? */
return type.isPrimitive();
}
/** What is the bytecode signature character for this type?
* All non-primitives, including array types, report as 'L', the signature character for references.
*/
if (!type.isPrimitive())
return 'L';
else
}
/** What is the bytecode signature character for this wrapper's
* primitive type?
*/
/** What is the simple name of the wrapper type?
*/
/** What is the simple name of the primitive type?
*/
// /** Wrap a value in the given type, which may be either a primitive or wrapper type.
// * Performs standard primitive conversions, including truncation and float conversions.
// */
// public static <T> T wrap(Object x, Class<T> type) {
// return Wrapper.valueOf(type).cast(x, type);
// }
/** Cast a wrapped value to the given type, which may be either a primitive or wrapper type.
* The given target type must be this wrapper's primitive or wrapper type.
* If this wrapper is OBJECT, the target type may also be an interface, perform no runtime check.
* Performs standard primitive conversions, including truncation and float conversions.
* The given type must be compatible with this wrapper. That is, it must either
* be the wrapper type (or a subtype, in the case of {@code OBJECT}) or else
* it must be the wrapper's primitive type.
* Primitive conversions are only performed if the given type is itself a primitive.
* @throws ClassCastException if the given type is not compatible with this wrapper
*/
}
/** Convert a wrapped value to the given type.
* The given target type must be this wrapper's primitive or wrapper type.
* This is equivalent to {@link #cast}, except that it refuses to perform
* narrowing primitive conversions.
*/
}
if (this == OBJECT) {
// If the target wrapper is OBJECT, just do a reference cast.
// If the target type is an interface, perform no runtime check.
// (This loophole is safe, and is allowed by the JVM verifier.)
// If the target type is a primitive, change it to a wrapper.
assert(!type.isPrimitive());
if (!type.isInterface())
@SuppressWarnings("unchecked")
T result = (T) x; // unchecked warning is expected here
return result;
}
if (wtype.isInstance(x)) {
}
if (!isCast) {
}
} else if (x == null) {
@SuppressWarnings("unchecked")
T z = (T) zero;
return z;
}
@SuppressWarnings("unchecked")
return result;
}
/** Cast a reference type to another reference type.
* If the target type is an interface, perform no runtime check.
* (This loophole is safe, and is allowed by the JVM verifier.)
* If the target type is a primitive, change it to a wrapper.
*/
boolean z = (type == exampleType ||
if (!z)
assert(type == exampleType ||
@SuppressWarnings("unchecked")
return result;
}
/** 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
*/
// do non-numeric wrappers first
switch (basicTypeChar) {
case 'L': return x;
case 'V': return null;
}
switch (basicTypeChar) {
}
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}.
*/
switch (basicTypeChar) {
case 'V': return null;
}
throw new InternalError("bad wrapper");
}
// Remaining allowed case of void: Must be a null reference.
return (Number)x;
}
// Parameter type of boolValue must be byte, because
// MethodHandles.explicitCastArguments defines boolean
// conversion as first converting to byte.
return (bits != 0);
}
return newIllegalArgumentException(message + x);
}
return new IllegalArgumentException(message);
}
// primitive array support
}
return emptyArray.getClass();
}
for (int i = 0; i < length; i++) {
}
}
for (int i = 0; i < length; i++) {
//Already done: value = convert(value, primitiveType);
}
}
}