jaroslav@1646: /* jaroslav@1646: * Copyright (c) 2008, 2013, 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: import java.lang.invoke.MethodType; jaroslav@1646: import sun.invoke.empty.Empty; jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * This class centralizes information about the JVM verifier jaroslav@1646: * and its requirements about type correctness. jaroslav@1646: * @author jrose jaroslav@1646: */ jaroslav@1646: public class VerifyType { jaroslav@1646: jaroslav@1646: private VerifyType() { } // cannot instantiate jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * True if a value can be stacked as the source type and unstacked as the jaroslav@1646: * destination type, without violating the JVM's type consistency. jaroslav@1646: * jaroslav@1646: * @param src the type of a stacked value jaroslav@1646: * @param dst the type by which we'd like to treat it jaroslav@1646: * @return whether the retyping can be done without motion or reformatting jaroslav@1646: */ jaroslav@1646: public static boolean isNullConversion(Class src, Class dst) { jaroslav@1646: if (src == dst) return true; jaroslav@1646: // Verifier allows any interface to be treated as Object: jaroslav@1646: if (dst.isInterface()) dst = Object.class; jaroslav@1646: if (src.isInterface()) src = Object.class; jaroslav@1646: if (src == dst) return true; // check again jaroslav@1646: if (dst == void.class) return true; // drop any return value jaroslav@1646: if (isNullType(src)) return !dst.isPrimitive(); jaroslav@1646: if (!src.isPrimitive()) return dst.isAssignableFrom(src); jaroslav@1646: if (!dst.isPrimitive()) return false; jaroslav@1646: // Verifier allows an int to carry byte, short, char, or even boolean: jaroslav@1646: Wrapper sw = Wrapper.forPrimitiveType(src); jaroslav@1646: if (dst == int.class) return sw.isSubwordOrInt(); jaroslav@1646: Wrapper dw = Wrapper.forPrimitiveType(dst); jaroslav@1646: if (!sw.isSubwordOrInt()) return false; jaroslav@1646: if (!dw.isSubwordOrInt()) return false; jaroslav@1646: if (!dw.isSigned() && sw.isSigned()) return false; jaroslav@1646: return dw.bitWidth() > sw.bitWidth(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Specialization of isNullConversion to reference types. jaroslav@1646: * @param src the type of a stacked value jaroslav@1646: * @param dst the reference type by which we'd like to treat it jaroslav@1646: * @return whether the retyping can be done without a cast jaroslav@1646: */ jaroslav@1646: public static boolean isNullReferenceConversion(Class src, Class dst) { jaroslav@1646: assert(!dst.isPrimitive()); jaroslav@1646: if (dst.isInterface()) return true; // verifier allows this jaroslav@1646: if (isNullType(src)) return true; jaroslav@1646: return dst.isAssignableFrom(src); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Is the given type java.lang.Null or an equivalent null-only type? jaroslav@1646: */ jaroslav@1646: public static boolean isNullType(Class type) { jaroslav@1646: if (type == null) return false; jaroslav@1646: return type == NULL_CLASS jaroslav@1646: // This one may also be used as a null type. jaroslav@1646: // TO DO: Decide if we really want to legitimize it here. jaroslav@1646: // Probably we do, unless java.lang.Null really makes it into Java 7 jaroslav@1646: //|| type == Void.class jaroslav@1646: // Locally known null-only class: jaroslav@1646: || type == Empty.class jaroslav@1646: ; jaroslav@1646: } jaroslav@1646: private static final Class NULL_CLASS; jaroslav@1646: static { jaroslav@1646: Class nullClass = null; jaroslav@1646: try { jaroslav@1646: nullClass = Class.forName("java.lang.Null"); jaroslav@1646: } catch (ClassNotFoundException ex) { jaroslav@1646: // OK, we'll cope jaroslav@1646: } jaroslav@1646: NULL_CLASS = nullClass; jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * True if a method handle can receive a call under a slightly different jaroslav@1646: * method type, without moving or reformatting any stack elements. jaroslav@1646: * jaroslav@1646: * @param call the type of call being made jaroslav@1646: * @param recv the type of the method handle receiving the call jaroslav@1646: * @return whether the retyping can be done without motion or reformatting jaroslav@1646: */ jaroslav@1646: public static boolean isNullConversion(MethodType call, MethodType recv) { jaroslav@1646: if (call == recv) return true; jaroslav@1646: int len = call.parameterCount(); jaroslav@1646: if (len != recv.parameterCount()) return false; jaroslav@1646: for (int i = 0; i < len; i++) jaroslav@1646: if (!isNullConversion(call.parameterType(i), recv.parameterType(i))) jaroslav@1646: return false; jaroslav@1646: return isNullConversion(recv.returnType(), call.returnType()); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Determine if the JVM verifier allows a value of type call to be jaroslav@1646: * passed to a formal parameter (or return variable) of type recv. jaroslav@1646: * Returns 1 if the verifier allows the types to match without conversion. jaroslav@1646: * Returns -1 if the types can be made to match by a JVM-supported adapter. jaroslav@1646: * Cases supported are: jaroslav@1646: * jaroslav@1646: * (Autoboxing is not supported here; it must be done via Java code.) jaroslav@1646: * Returns 0 otherwise. jaroslav@1646: */ jaroslav@1646: public static int canPassUnchecked(Class src, Class dst) { jaroslav@1646: if (src == dst) jaroslav@1646: return 1; jaroslav@1646: jaroslav@1646: if (dst.isPrimitive()) { jaroslav@1646: if (dst == void.class) jaroslav@1646: // Return anything to a caller expecting void. jaroslav@1646: // This is a property of the implementation, which links jaroslav@1646: // return values via a register rather than via a stack push. jaroslav@1646: // This makes it possible to ignore cleanly. jaroslav@1646: return 1; jaroslav@1646: if (src == void.class) jaroslav@1646: return 0; // void-to-something? jaroslav@1646: if (!src.isPrimitive()) jaroslav@1646: // Cannot pass a reference to any primitive type (exc. void). jaroslav@1646: return 0; jaroslav@1646: Wrapper sw = Wrapper.forPrimitiveType(src); jaroslav@1646: Wrapper dw = Wrapper.forPrimitiveType(dst); jaroslav@1646: if (sw.isSubwordOrInt() && dw.isSubwordOrInt()) { jaroslav@1646: if (sw.bitWidth() >= dw.bitWidth()) jaroslav@1646: return -1; // truncation may be required jaroslav@1646: if (!dw.isSigned() && sw.isSigned()) jaroslav@1646: return -1; // sign elimination may be required jaroslav@1646: return 1; jaroslav@1646: } jaroslav@1646: if (src == float.class || dst == float.class) { jaroslav@1646: if (src == double.class || dst == double.class) jaroslav@1646: return -1; // floating conversion may be required jaroslav@1646: else jaroslav@1646: return 0; // other primitive conversions NYI jaroslav@1646: } else { jaroslav@1646: // all fixed-point conversions are supported jaroslav@1646: return 0; jaroslav@1646: } jaroslav@1646: } else if (src.isPrimitive()) { jaroslav@1646: // Cannot pass a primitive to any reference type. jaroslav@1646: // (Maybe allow null.class?) jaroslav@1646: return 0; jaroslav@1646: } jaroslav@1646: jaroslav@1646: // Handle reference types in the rest of the block: jaroslav@1646: jaroslav@1646: // The verifier treats interfaces exactly like Object. jaroslav@1646: if (isNullReferenceConversion(src, dst)) jaroslav@1646: // pass any reference to object or an arb. interface jaroslav@1646: return 1; jaroslav@1646: // else it's a definite "maybe" (cast is required) jaroslav@1646: return -1; jaroslav@1646: } jaroslav@1646: jaroslav@1646: public static boolean isSpreadArgType(Class spreadArg) { jaroslav@1646: return spreadArg.isArray(); jaroslav@1646: } jaroslav@1646: public static Class spreadArgElementType(Class spreadArg, int i) { jaroslav@1646: return spreadArg.getComponentType(); jaroslav@1646: } jaroslav@1646: }