Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/invoke/util/Wrapper.java
38918 views
/*1* Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.invoke.util;2627public enum Wrapper {28// wrapperType primitiveType char zero emptyArray format29BOOLEAN( 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.231BYTE ( Byte.class, byte.class, 'B', (Byte)(byte)0, new byte[0], Format.signed( 8)),32SHORT ( Short.class, short.class, 'S', (Short)(short)0, new short[0], Format.signed( 16)),33CHAR (Character.class, char.class, 'C', (Character)(char)0, new char[0], Format.unsigned(16)),34INT ( Integer.class, int.class, 'I', (Integer)/*(int)*/0, new int[0], Format.signed( 32)),35LONG ( Long.class, long.class, 'J', (Long)(long)0, new long[0], Format.signed( 64)),36FLOAT ( Float.class, float.class, 'F', (Float)(float)0, new float[0], Format.floating(32)),37DOUBLE ( Double.class, double.class, 'D', (Double)(double)0, new double[0], Format.floating(64)),38OBJECT ( Object.class, Object.class, 'L', null, new Object[0], Format.other( 1)),39// VOID must be the last type, since it is "assignable" from any other type:40VOID ( Void.class, void.class, 'V', null, null, Format.other( 0)),41;4243private final Class<?> wrapperType;44private final Class<?> primitiveType;45private final char basicTypeChar;46private final Object zero;47private final Object emptyArray;48private final int format;49private final String wrapperSimpleName;50private final String primitiveSimpleName;5152private Wrapper(Class<?> wtype, Class<?> ptype, char tchar, Object zero, Object emptyArray, int format) {53this.wrapperType = wtype;54this.primitiveType = ptype;55this.basicTypeChar = tchar;56this.zero = zero;57this.emptyArray = emptyArray;58this.format = format;59this.wrapperSimpleName = wtype.getSimpleName();60this.primitiveSimpleName = ptype.getSimpleName();61}6263/** For debugging, give the details of this wrapper. */64public String detailString() {65return wrapperSimpleName+66java.util.Arrays.asList(wrapperType, primitiveType,67basicTypeChar, zero,68"0x"+Integer.toHexString(format));69}7071private static abstract class Format {72static final int SLOT_SHIFT = 0, SIZE_SHIFT = 2, KIND_SHIFT = 12;73static final int74SIGNED = (-1) << KIND_SHIFT,75UNSIGNED = 0 << KIND_SHIFT,76FLOATING = 1 << KIND_SHIFT;77static final int78SLOT_MASK = ((1<<(SIZE_SHIFT-SLOT_SHIFT))-1),79SIZE_MASK = ((1<<(KIND_SHIFT-SIZE_SHIFT))-1);80static int format(int kind, int size, int slots) {81assert(((kind >> KIND_SHIFT) << KIND_SHIFT) == kind);82assert((size & (size-1)) == 0); // power of two83assert((kind == SIGNED) ? (size > 0) :84(kind == UNSIGNED) ? (size > 0) :85(kind == FLOATING) ? (size == 32 || size == 64) :86false);87assert((slots == 2) ? (size == 64) :88(slots == 1) ? (size <= 32) :89false);90return kind | (size << SIZE_SHIFT) | (slots << SLOT_SHIFT);91}92static final int93INT = SIGNED | (32 << SIZE_SHIFT) | (1 << SLOT_SHIFT),94SHORT = SIGNED | (16 << SIZE_SHIFT) | (1 << SLOT_SHIFT),95BOOLEAN = UNSIGNED | (1 << SIZE_SHIFT) | (1 << SLOT_SHIFT),96CHAR = UNSIGNED | (16 << SIZE_SHIFT) | (1 << SLOT_SHIFT),97FLOAT = FLOATING | (32 << SIZE_SHIFT) | (1 << SLOT_SHIFT),98VOID = UNSIGNED | (0 << SIZE_SHIFT) | (0 << SLOT_SHIFT),99NUM_MASK = (-1) << SIZE_SHIFT;100static int signed(int size) { return format(SIGNED, size, (size > 32 ? 2 : 1)); }101static int unsigned(int size) { return format(UNSIGNED, size, (size > 32 ? 2 : 1)); }102static int floating(int size) { return format(FLOATING, size, (size > 32 ? 2 : 1)); }103static int other(int slots) { return slots << SLOT_SHIFT; }104}105106/// format queries:107108/** How many bits are in the wrapped value? Returns 0 for OBJECT or VOID. */109public int bitWidth() { return (format >> Format.SIZE_SHIFT) & Format.SIZE_MASK; }110/** How many JVM stack slots occupied by the wrapped value? Returns 0 for VOID. */111public int stackSlots() { return (format >> Format.SLOT_SHIFT) & Format.SLOT_MASK; }112/** Does the wrapped value occupy a single JVM stack slot? */113public boolean isSingleWord() { return (format & (1 << Format.SLOT_SHIFT)) != 0; }114/** Does the wrapped value occupy two JVM stack slots? */115public boolean isDoubleWord() { return (format & (2 << Format.SLOT_SHIFT)) != 0; }116/** Is the wrapped type numeric (not void or object)? */117public boolean isNumeric() { return (format & Format.NUM_MASK) != 0; }118/** Is the wrapped type a primitive other than float, double, or void? */119public boolean isIntegral() { return isNumeric() && format < Format.FLOAT; }120/** Is the wrapped type one of int, boolean, byte, char, or short? */121public boolean isSubwordOrInt() { return isIntegral() && isSingleWord(); }122/* Is the wrapped value a signed integral type (one of byte, short, int, or long)? */123public boolean isSigned() { return format < Format.VOID; }124/* Is the wrapped value an unsigned integral type (one of boolean or char)? */125public boolean isUnsigned() { return format >= Format.BOOLEAN && format < Format.FLOAT; }126/** Is the wrapped type either float or double? */127public boolean isFloating() { return format >= Format.FLOAT; }128/** Is the wrapped type either void or a reference? */129public boolean isOther() { return (format & ~Format.SLOT_MASK) == 0; }130131/** Does the JLS 5.1.2 allow a variable of this wrapper's132* primitive type to be assigned from a value of the given wrapper's primitive type?133* Cases:134* <ul>135* <li>unboxing followed by widening primitive conversion136* <li>any type converted to {@code void} (i.e., dropping a method call's value)137* <li>boxing conversion followed by widening reference conversion to {@code Object}138* </ul>139* These are the cases allowed by MethodHandle.asType.140*/141public boolean isConvertibleFrom(Wrapper source) {142if (this == source) return true;143if (this.compareTo(source) < 0) {144// At best, this is a narrowing conversion.145return false;146}147// All conversions are allowed in the enum order between floats and signed ints.148// First detect non-signed non-float types (boolean, char, Object, void).149boolean floatOrSigned = (((this.format & source.format) & Format.SIGNED) != 0);150if (!floatOrSigned) {151if (this.isOther()) return true;152// can convert char to int or wider, but nothing else153if (source.format == Format.CHAR) return true;154// no other conversions are classified as widening155return false;156}157// All signed and float conversions in the enum order are widening.158assert(this.isFloating() || this.isSigned());159assert(source.isFloating() || source.isSigned());160return true;161}162163static { assert(checkConvertibleFrom()); }164private static boolean checkConvertibleFrom() {165// Check the matrix for correct classification of widening conversions.166for (Wrapper w : values()) {167assert(w.isConvertibleFrom(w));168assert(VOID.isConvertibleFrom(w));169if (w != VOID) {170assert(OBJECT.isConvertibleFrom(w));171assert(!w.isConvertibleFrom(VOID));172}173// check relations with unsigned integral types:174if (w != CHAR) {175assert(!CHAR.isConvertibleFrom(w));176if (!w.isConvertibleFrom(INT))177assert(!w.isConvertibleFrom(CHAR));178}179if (w != BOOLEAN) {180assert(!BOOLEAN.isConvertibleFrom(w));181if (w != VOID && w != OBJECT)182assert(!w.isConvertibleFrom(BOOLEAN));183}184// check relations with signed integral types:185if (w.isSigned()) {186for (Wrapper x : values()) {187if (w == x) continue;188if (x.isFloating())189assert(!w.isConvertibleFrom(x));190else if (x.isSigned()) {191if (w.compareTo(x) < 0)192assert(!w.isConvertibleFrom(x));193else194assert(w.isConvertibleFrom(x));195}196}197}198// check relations with floating types:199if (w.isFloating()) {200for (Wrapper x : values()) {201if (w == x) continue;202if (x.isSigned())203assert(w.isConvertibleFrom(x));204else if (x.isFloating()) {205if (w.compareTo(x) < 0)206assert(!w.isConvertibleFrom(x));207else208assert(w.isConvertibleFrom(x));209}210}211}212}213return true; // i.e., assert(true)214}215216/** Produce a zero value for the given wrapper type.217* This will be a numeric zero for a number or character,218* false for a boolean, and null for a reference or void.219* The common thread is that this is what is contained220* in a default-initialized variable of the given primitive221* type. (For void, it is what a reflective method returns222* instead of no value at all.)223*/224public Object zero() { return zero; }225226/** Produce a zero value for the given wrapper type T.227* The optional argument must a type compatible with this wrapper.228* Equivalent to {@code this.cast(this.zero(), type)}.229*/230public <T> T zero(Class<T> type) { return convert(zero, type); }231232/** Return the wrapper that wraps values of the given type.233* The type may be {@code Object}, meaning the {@code OBJECT} wrapper.234* Otherwise, the type must be a primitive.235* @throws IllegalArgumentException for unexpected types236*/237public static Wrapper forPrimitiveType(Class<?> type) {238Wrapper w = findPrimitiveType(type);239if (w != null) return w;240if (type.isPrimitive())241throw new InternalError(); // redo hash function242throw newIllegalArgumentException("not primitive: "+type);243}244245static Wrapper findPrimitiveType(Class<?> type) {246Wrapper w = FROM_PRIM[hashPrim(type)];247if (w != null && w.primitiveType == type) {248return w;249}250return null;251}252253/** Return the wrapper that wraps values into the given wrapper type.254* If it is {@code Object}, return {@code OBJECT}.255* Otherwise, it must be a wrapper type.256* The type must not be a primitive type.257* @throws IllegalArgumentException for unexpected types258*/259public static Wrapper forWrapperType(Class<?> type) {260Wrapper w = findWrapperType(type);261if (w != null) return w;262for (Wrapper x : values())263if (x.wrapperType == type)264throw new InternalError(); // redo hash function265throw newIllegalArgumentException("not wrapper: "+type);266}267268static Wrapper findWrapperType(Class<?> type) {269Wrapper w = FROM_WRAP[hashWrap(type)];270if (w != null && w.wrapperType == type) {271return w;272}273return null;274}275276/** Return the wrapper that corresponds to the given bytecode277* signature character. Return {@code OBJECT} for the character 'L'.278* @throws IllegalArgumentException for any non-signature character or {@code '['}.279*/280public static Wrapper forBasicType(char type) {281Wrapper w = FROM_CHAR[hashChar(type)];282if (w != null && w.basicTypeChar == type) {283return w;284}285for (Wrapper x : values())286if (w.basicTypeChar == type)287throw new InternalError(); // redo hash function288throw newIllegalArgumentException("not basic type char: "+type);289}290291/** Return the wrapper for the given type, if it is292* a primitive type, else return {@code OBJECT}.293*/294public static Wrapper forBasicType(Class<?> type) {295if (type.isPrimitive())296return forPrimitiveType(type);297return OBJECT; // any reference, including wrappers or arrays298}299300// Note on perfect hashes:301// for signature chars c, do (c + (c >> 1)) % 16302// for primitive type names n, do (n[0] + n[2]) % 16303// The type name hash works for both primitive and wrapper names.304// You can add "java/lang/Object" to the primitive names.305// But you add the wrapper name Object, use (n[2] + (3*n[1])) % 16.306private static final Wrapper[] FROM_PRIM = new Wrapper[16];307private static final Wrapper[] FROM_WRAP = new Wrapper[16];308private static final Wrapper[] FROM_CHAR = new Wrapper[16];309private static int hashPrim(Class<?> x) {310String xn = x.getName();311if (xn.length() < 3) return 0;312return (xn.charAt(0) + xn.charAt(2)) % 16;313}314private static int hashWrap(Class<?> x) {315String xn = x.getName();316final int offset = 10; assert(offset == "java.lang.".length());317if (xn.length() < offset+3) return 0;318return (3*xn.charAt(offset+1) + xn.charAt(offset+2)) % 16;319}320private static int hashChar(char x) {321return (x + (x >> 1)) % 16;322}323static {324for (Wrapper w : values()) {325int pi = hashPrim(w.primitiveType);326int wi = hashWrap(w.wrapperType);327int ci = hashChar(w.basicTypeChar);328assert(FROM_PRIM[pi] == null);329assert(FROM_WRAP[wi] == null);330assert(FROM_CHAR[ci] == null);331FROM_PRIM[pi] = w;332FROM_WRAP[wi] = w;333FROM_CHAR[ci] = w;334}335//assert(jdk.sun.invoke.util.WrapperTest.test(false));336}337338/** What is the primitive type wrapped by this wrapper? */339public Class<?> primitiveType() { return primitiveType; }340341/** What is the wrapper type for this wrapper? */342public Class<?> wrapperType() { return wrapperType; }343344/** What is the wrapper type for this wrapper?345* Otherwise, the example type must be the wrapper type,346* or the corresponding primitive type.347* (For {@code OBJECT}, the example type can be any non-primitive,348* and is normalized to {@code Object.class}.)349* The resulting class type has the same type parameter.350*/351public <T> Class<T> wrapperType(Class<T> exampleType) {352if (exampleType == wrapperType) {353return exampleType;354} else if (exampleType == primitiveType ||355wrapperType == Object.class ||356exampleType.isInterface()) {357return forceType(wrapperType, exampleType);358}359throw newClassCastException(exampleType, primitiveType);360}361362private static ClassCastException newClassCastException(Class<?> actual, Class<?> expected) {363return new ClassCastException(actual + " is not compatible with " + expected);364}365366/** If {@code type} is a primitive type, return the corresponding367* wrapper type, else return {@code type} unchanged.368*/369public static <T> Class<T> asWrapperType(Class<T> type) {370if (type.isPrimitive()) {371return forPrimitiveType(type).wrapperType(type);372}373return type;374}375376/** If {@code type} is a wrapper type, return the corresponding377* primitive type, else return {@code type} unchanged.378*/379public static <T> Class<T> asPrimitiveType(Class<T> type) {380Wrapper w = findWrapperType(type);381if (w != null) {382return forceType(w.primitiveType(), type);383}384return type;385}386387/** Query: Is the given type a wrapper, such as {@code Integer} or {@code Void}? */388public static boolean isWrapperType(Class<?> type) {389return findWrapperType(type) != null;390}391392/** Query: Is the given type a primitive, such as {@code int} or {@code void}? */393public static boolean isPrimitiveType(Class<?> type) {394return type.isPrimitive();395}396397/** What is the bytecode signature character for this type?398* All non-primitives, including array types, report as 'L', the signature character for references.399*/400public static char basicTypeChar(Class<?> type) {401if (!type.isPrimitive())402return 'L';403else404return forPrimitiveType(type).basicTypeChar();405}406407/** What is the bytecode signature character for this wrapper's408* primitive type?409*/410public char basicTypeChar() { return basicTypeChar; }411412/** What is the simple name of the wrapper type?413*/414public String wrapperSimpleName() { return wrapperSimpleName; }415416/** What is the simple name of the primitive type?417*/418public String primitiveSimpleName() { return primitiveSimpleName; }419420// /** Wrap a value in the given type, which may be either a primitive or wrapper type.421// * Performs standard primitive conversions, including truncation and float conversions.422// */423// public static <T> T wrap(Object x, Class<T> type) {424// return Wrapper.valueOf(type).cast(x, type);425// }426427/** Cast a wrapped value to the given type, which may be either a primitive or wrapper type.428* The given target type must be this wrapper's primitive or wrapper type.429* If this wrapper is OBJECT, the target type may also be an interface, perform no runtime check.430* Performs standard primitive conversions, including truncation and float conversions.431* The given type must be compatible with this wrapper. That is, it must either432* be the wrapper type (or a subtype, in the case of {@code OBJECT}) or else433* it must be the wrapper's primitive type.434* Primitive conversions are only performed if the given type is itself a primitive.435* @throws ClassCastException if the given type is not compatible with this wrapper436*/437public <T> T cast(Object x, Class<T> type) {438return convert(x, type, true);439}440441/** Convert a wrapped value to the given type.442* The given target type must be this wrapper's primitive or wrapper type.443* This is equivalent to {@link #cast}, except that it refuses to perform444* narrowing primitive conversions.445*/446public <T> T convert(Object x, Class<T> type) {447return convert(x, type, false);448}449450private <T> T convert(Object x, Class<T> type, boolean isCast) {451if (this == OBJECT) {452// If the target wrapper is OBJECT, just do a reference cast.453// If the target type is an interface, perform no runtime check.454// (This loophole is safe, and is allowed by the JVM verifier.)455// If the target type is a primitive, change it to a wrapper.456assert(!type.isPrimitive());457if (!type.isInterface())458type.cast(x);459@SuppressWarnings("unchecked")460T result = (T) x; // unchecked warning is expected here461return result;462}463Class<T> wtype = wrapperType(type);464if (wtype.isInstance(x)) {465return wtype.cast(x);466}467if (!isCast) {468Class<?> sourceType = x.getClass(); // throw NPE if x is null469Wrapper source = findWrapperType(sourceType);470if (source == null || !this.isConvertibleFrom(source)) {471throw newClassCastException(wtype, sourceType);472}473} else if (x == null) {474@SuppressWarnings("unchecked")475T z = (T) zero;476return z;477}478@SuppressWarnings("unchecked")479T result = (T) wrap(x); // unchecked warning is expected here480assert (result == null ? Void.class : result.getClass()) == wtype;481return result;482}483484/** Cast a reference type to another reference type.485* If the target type is an interface, perform no runtime check.486* (This loophole is safe, and is allowed by the JVM verifier.)487* If the target type is a primitive, change it to a wrapper.488*/489static <T> Class<T> forceType(Class<?> type, Class<T> exampleType) {490boolean z = (type == exampleType ||491type.isPrimitive() && forPrimitiveType(type) == findWrapperType(exampleType) ||492exampleType.isPrimitive() && forPrimitiveType(exampleType) == findWrapperType(type) ||493type == Object.class && !exampleType.isPrimitive());494if (!z)495System.out.println(type+" <= "+exampleType);496assert(type == exampleType ||497type.isPrimitive() && forPrimitiveType(type) == findWrapperType(exampleType) ||498exampleType.isPrimitive() && forPrimitiveType(exampleType) == findWrapperType(type) ||499type == Object.class && !exampleType.isPrimitive());500@SuppressWarnings("unchecked")501Class<T> result = (Class<T>) type; // unchecked warning is expected here502return result;503}504505/** Wrap a value in this wrapper's type.506* Performs standard primitive conversions, including truncation and float conversions.507* Performs returns the unchanged reference for {@code OBJECT}.508* Returns null for {@code VOID}.509* Returns a zero value for a null input.510* @throws ClassCastException if this wrapper is numeric and the operand511* is not a number, character, boolean, or null512*/513public Object wrap(Object x) {514// do non-numeric wrappers first515switch (basicTypeChar) {516case 'L': return x;517case 'V': return null;518}519Number xn = numberValue(x);520switch (basicTypeChar) {521case 'I': return Integer.valueOf(xn.intValue());522case 'J': return Long.valueOf(xn.longValue());523case 'F': return Float.valueOf(xn.floatValue());524case 'D': return Double.valueOf(xn.doubleValue());525case 'S': return Short.valueOf((short) xn.intValue());526case 'B': return Byte.valueOf((byte) xn.intValue());527case 'C': return Character.valueOf((char) xn.intValue());528case 'Z': return Boolean.valueOf(boolValue(xn.byteValue()));529}530throw new InternalError("bad wrapper");531}532533/** Wrap a value (an int or smaller value) in this wrapper's type.534* Performs standard primitive conversions, including truncation and float conversions.535* Produces an {@code Integer} for {@code OBJECT}, although the exact type536* of the operand is not known.537* Returns null for {@code VOID}.538*/539public Object wrap(int x) {540if (basicTypeChar == 'L') return (Integer)x;541switch (basicTypeChar) {542case 'L': throw newIllegalArgumentException("cannot wrap to object type");543case 'V': return null;544case 'I': return Integer.valueOf(x);545case 'J': return Long.valueOf(x);546case 'F': return Float.valueOf(x);547case 'D': return Double.valueOf(x);548case 'S': return Short.valueOf((short) x);549case 'B': return Byte.valueOf((byte) x);550case 'C': return Character.valueOf((char) x);551case 'Z': return Boolean.valueOf(boolValue((byte) x));552}553throw new InternalError("bad wrapper");554}555556private static Number numberValue(Object x) {557if (x instanceof Number) return (Number)x;558if (x instanceof Character) return (int)(Character)x;559if (x instanceof Boolean) return (Boolean)x ? 1 : 0;560// Remaining allowed case of void: Must be a null reference.561return (Number)x;562}563564// Parameter type of boolValue must be byte, because565// MethodHandles.explicitCastArguments defines boolean566// conversion as first converting to byte.567private static boolean boolValue(byte bits) {568bits &= 1; // simple 31-bit zero extension569return (bits != 0);570}571572private static RuntimeException newIllegalArgumentException(String message, Object x) {573return newIllegalArgumentException(message + x);574}575private static RuntimeException newIllegalArgumentException(String message) {576return new IllegalArgumentException(message);577}578579// primitive array support580public Object makeArray(int len) {581return java.lang.reflect.Array.newInstance(primitiveType, len);582}583public Class<?> arrayType() {584return emptyArray.getClass();585}586public void copyArrayUnboxing(Object[] values, int vpos, Object a, int apos, int length) {587if (a.getClass() != arrayType())588arrayType().cast(a); // throw NPE or CCE if bad type589for (int i = 0; i < length; i++) {590Object value = values[i+vpos];591value = convert(value, primitiveType);592java.lang.reflect.Array.set(a, i+apos, value);593}594}595public void copyArrayBoxing(Object a, int apos, Object[] values, int vpos, int length) {596if (a.getClass() != arrayType())597arrayType().cast(a); // throw NPE or CCE if bad type598for (int i = 0; i < length; i++) {599Object value = java.lang.reflect.Array.get(a, i+apos);600//Already done: value = convert(value, primitiveType);601assert(value.getClass() == wrapperType);602values[i+vpos] = value;603}604}605}606607608