1.1 --- a/rt/emul/compact/src/main/java/sun/invoke/util/VerifyType.java Sun Aug 17 20:09:05 2014 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,194 +0,0 @@
1.4 -/*
1.5 - * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 - *
1.8 - * This code is free software; you can redistribute it and/or modify it
1.9 - * under the terms of the GNU General Public License version 2 only, as
1.10 - * published by the Free Software Foundation. Oracle designates this
1.11 - * particular file as subject to the "Classpath" exception as provided
1.12 - * by Oracle in the LICENSE file that accompanied this code.
1.13 - *
1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 - * version 2 for more details (a copy is included in the LICENSE file that
1.18 - * accompanied this code).
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License version
1.21 - * 2 along with this work; if not, write to the Free Software Foundation,
1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 - *
1.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 - * or visit www.oracle.com if you need additional information or have any
1.26 - * questions.
1.27 - */
1.28 -
1.29 -package sun.invoke.util;
1.30 -
1.31 -import java.lang.invoke.MethodType;
1.32 -import sun.invoke.empty.Empty;
1.33 -
1.34 -/**
1.35 - * This class centralizes information about the JVM verifier
1.36 - * and its requirements about type correctness.
1.37 - * @author jrose
1.38 - */
1.39 -public class VerifyType {
1.40 -
1.41 - private VerifyType() { } // cannot instantiate
1.42 -
1.43 - /**
1.44 - * True if a value can be stacked as the source type and unstacked as the
1.45 - * destination type, without violating the JVM's type consistency.
1.46 - *
1.47 - * @param src the type of a stacked value
1.48 - * @param dst the type by which we'd like to treat it
1.49 - * @return whether the retyping can be done without motion or reformatting
1.50 - */
1.51 - public static boolean isNullConversion(Class<?> src, Class<?> dst) {
1.52 - if (src == dst) return true;
1.53 - // Verifier allows any interface to be treated as Object:
1.54 - if (dst.isInterface()) dst = Object.class;
1.55 - if (src.isInterface()) src = Object.class;
1.56 - if (src == dst) return true; // check again
1.57 - if (dst == void.class) return true; // drop any return value
1.58 - if (isNullType(src)) return !dst.isPrimitive();
1.59 - if (!src.isPrimitive()) return dst.isAssignableFrom(src);
1.60 - if (!dst.isPrimitive()) return false;
1.61 - // Verifier allows an int to carry byte, short, char, or even boolean:
1.62 - Wrapper sw = Wrapper.forPrimitiveType(src);
1.63 - if (dst == int.class) return sw.isSubwordOrInt();
1.64 - Wrapper dw = Wrapper.forPrimitiveType(dst);
1.65 - if (!sw.isSubwordOrInt()) return false;
1.66 - if (!dw.isSubwordOrInt()) return false;
1.67 - if (!dw.isSigned() && sw.isSigned()) return false;
1.68 - return dw.bitWidth() > sw.bitWidth();
1.69 - }
1.70 -
1.71 - /**
1.72 - * Specialization of isNullConversion to reference types.
1.73 - * @param src the type of a stacked value
1.74 - * @param dst the reference type by which we'd like to treat it
1.75 - * @return whether the retyping can be done without a cast
1.76 - */
1.77 - public static boolean isNullReferenceConversion(Class<?> src, Class<?> dst) {
1.78 - assert(!dst.isPrimitive());
1.79 - if (dst.isInterface()) return true; // verifier allows this
1.80 - if (isNullType(src)) return true;
1.81 - return dst.isAssignableFrom(src);
1.82 - }
1.83 -
1.84 - /**
1.85 - * Is the given type java.lang.Null or an equivalent null-only type?
1.86 - */
1.87 - public static boolean isNullType(Class<?> type) {
1.88 - if (type == null) return false;
1.89 - return type == NULL_CLASS
1.90 - // This one may also be used as a null type.
1.91 - // TO DO: Decide if we really want to legitimize it here.
1.92 - // Probably we do, unless java.lang.Null really makes it into Java 7
1.93 - //|| type == Void.class
1.94 - // Locally known null-only class:
1.95 - || type == Empty.class
1.96 - ;
1.97 - }
1.98 - private static final Class<?> NULL_CLASS;
1.99 - static {
1.100 - Class<?> nullClass = null;
1.101 - try {
1.102 - nullClass = Class.forName("java.lang.Null");
1.103 - } catch (ClassNotFoundException ex) {
1.104 - // OK, we'll cope
1.105 - }
1.106 - NULL_CLASS = nullClass;
1.107 - }
1.108 -
1.109 - /**
1.110 - * True if a method handle can receive a call under a slightly different
1.111 - * method type, without moving or reformatting any stack elements.
1.112 - *
1.113 - * @param call the type of call being made
1.114 - * @param recv the type of the method handle receiving the call
1.115 - * @return whether the retyping can be done without motion or reformatting
1.116 - */
1.117 - public static boolean isNullConversion(MethodType call, MethodType recv) {
1.118 - if (call == recv) return true;
1.119 - int len = call.parameterCount();
1.120 - if (len != recv.parameterCount()) return false;
1.121 - for (int i = 0; i < len; i++)
1.122 - if (!isNullConversion(call.parameterType(i), recv.parameterType(i)))
1.123 - return false;
1.124 - return isNullConversion(recv.returnType(), call.returnType());
1.125 - }
1.126 -
1.127 - /**
1.128 - * Determine if the JVM verifier allows a value of type call to be
1.129 - * passed to a formal parameter (or return variable) of type recv.
1.130 - * Returns 1 if the verifier allows the types to match without conversion.
1.131 - * Returns -1 if the types can be made to match by a JVM-supported adapter.
1.132 - * Cases supported are:
1.133 - * <ul><li>checkcast
1.134 - * </li><li>conversion between any two integral types (but not floats)
1.135 - * </li><li>unboxing from a wrapper to its corresponding primitive type
1.136 - * </li><li>conversion in either direction between float and double
1.137 - * </li></ul>
1.138 - * (Autoboxing is not supported here; it must be done via Java code.)
1.139 - * Returns 0 otherwise.
1.140 - */
1.141 - public static int canPassUnchecked(Class<?> src, Class<?> dst) {
1.142 - if (src == dst)
1.143 - return 1;
1.144 -
1.145 - if (dst.isPrimitive()) {
1.146 - if (dst == void.class)
1.147 - // Return anything to a caller expecting void.
1.148 - // This is a property of the implementation, which links
1.149 - // return values via a register rather than via a stack push.
1.150 - // This makes it possible to ignore cleanly.
1.151 - return 1;
1.152 - if (src == void.class)
1.153 - return 0; // void-to-something?
1.154 - if (!src.isPrimitive())
1.155 - // Cannot pass a reference to any primitive type (exc. void).
1.156 - return 0;
1.157 - Wrapper sw = Wrapper.forPrimitiveType(src);
1.158 - Wrapper dw = Wrapper.forPrimitiveType(dst);
1.159 - if (sw.isSubwordOrInt() && dw.isSubwordOrInt()) {
1.160 - if (sw.bitWidth() >= dw.bitWidth())
1.161 - return -1; // truncation may be required
1.162 - if (!dw.isSigned() && sw.isSigned())
1.163 - return -1; // sign elimination may be required
1.164 - return 1;
1.165 - }
1.166 - if (src == float.class || dst == float.class) {
1.167 - if (src == double.class || dst == double.class)
1.168 - return -1; // floating conversion may be required
1.169 - else
1.170 - return 0; // other primitive conversions NYI
1.171 - } else {
1.172 - // all fixed-point conversions are supported
1.173 - return 0;
1.174 - }
1.175 - } else if (src.isPrimitive()) {
1.176 - // Cannot pass a primitive to any reference type.
1.177 - // (Maybe allow null.class?)
1.178 - return 0;
1.179 - }
1.180 -
1.181 - // Handle reference types in the rest of the block:
1.182 -
1.183 - // The verifier treats interfaces exactly like Object.
1.184 - if (isNullReferenceConversion(src, dst))
1.185 - // pass any reference to object or an arb. interface
1.186 - return 1;
1.187 - // else it's a definite "maybe" (cast is required)
1.188 - return -1;
1.189 - }
1.190 -
1.191 - public static boolean isSpreadArgType(Class<?> spreadArg) {
1.192 - return spreadArg.isArray();
1.193 - }
1.194 - public static Class<?> spreadArgElementType(Class<?> spreadArg, int i) {
1.195 - return spreadArg.getComponentType();
1.196 - }
1.197 -}