Showing the changes to java/pkg/sun/invoke/util/VerifyType.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,231 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. | ||
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| 4 | * | ||
| 5 | * This code is free software; you can redistribute it and/or modify it | ||
| 6 | * under the terms of the GNU General Public License version 2 only, as | ||
| 7 | * published by the Free Software Foundation. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle in the LICENSE file that accompanied this code. | ||
| 10 | * | ||
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 14 | * version 2 for more details (a copy is included in the LICENSE file that | ||
| 15 | * accompanied this code). | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License version | ||
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, | ||
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | * | ||
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | |||
| 26 | package sun.invoke.util; | ||
| 27 | |||
| 28 | import java.lang.invoke.MethodType; | ||
| 29 | import sun.invoke.empty.Empty; | ||
| 30 | |||
| 31 | /** | ||
| 32 | * This class centralizes information about the JVM verifier | ||
| 33 | * and its requirements about type correctness. | ||
| 34 | * @author jrose | ||
| 35 | */ | ||
| 36 | public class VerifyType { | ||
| 37 | |||
| 38 | private VerifyType() { } // cannot instantiate | ||
| 39 | |||
| 40 | /** | ||
| 41 | * True if a value can be stacked as the source type and unstacked as the | ||
| 42 | * destination type, without violating the JVM's type consistency. | ||
| 43 | * | ||
| 44 | * @param call the type of a stacked value | ||
| 45 | * @param recv the type by which we'd like to treat it | ||
| 46 | * @return whether the retyping can be done without motion or reformatting | ||
| 47 | */ | ||
| 48 | public static boolean isNullConversion(Class<?> src, Class<?> dst) { | ||
| 49 | if (src == dst) return true; | ||
| 50 | // Verifier allows any interface to be treated as Object: | ||
| 51 | if (dst.isInterface()) dst = Object.class; | ||
| 52 | if (src.isInterface()) src = Object.class; | ||
| 53 | if (src == dst) return true; // check again | ||
| 54 | if (dst == void.class) return true; // drop any return value | ||
| 55 | if (isNullType(src)) return !dst.isPrimitive(); | ||
| 56 | if (!src.isPrimitive()) return dst.isAssignableFrom(src); | ||
| 57 | if (!dst.isPrimitive()) return false; | ||
| 58 | // Verifier allows an int to carry byte, short, char, or even boolean: | ||
| 59 | Wrapper sw = Wrapper.forPrimitiveType(src); | ||
| 60 | if (dst == int.class) return sw.isSubwordOrInt(); | ||
| 61 | Wrapper dw = Wrapper.forPrimitiveType(dst); | ||
| 62 | if (!sw.isSubwordOrInt()) return false; | ||
| 63 | if (!dw.isSubwordOrInt()) return false; | ||
| 64 | if (!dw.isSigned() && sw.isSigned()) return false; | ||
| 65 | return dw.bitWidth() > sw.bitWidth(); | ||
| 66 | } | ||
| 67 | |||
| 68 | /** | ||
| 69 | * Specialization of isNullConversion to reference types. | ||
| 70 | |||
| 71 | * @param call the type of a stacked value | ||
| 72 | * @param recv the reference type by which we'd like to treat it | ||
| 73 | * @return whether the retyping can be done without a cast | ||
| 74 | */ | ||
| 75 | public static boolean isNullReferenceConversion(Class<?> src, Class<?> dst) { | ||
| 76 | assert(!dst.isPrimitive()); | ||
| 77 | if (dst.isInterface()) return true; // verifier allows this | ||
| 78 | if (isNullType(src)) return true; | ||
| 79 | return dst.isAssignableFrom(src); | ||
| 80 | } | ||
| 81 | |||
| 82 | /** | ||
| 83 | * Is the given type java.lang.Null or an equivalent null-only type? | ||
| 84 | */ | ||
| 85 | public static boolean isNullType(Class<?> type) { | ||
| 86 | if (type == null) return false; | ||
| 87 | return type == NULL_CLASS | ||
| 88 | // This one may also be used as a null type. | ||
| 89 | // TO DO: Decide if we really want to legitimize it here. | ||
| 90 | // Probably we do, unless java.lang.Null really makes it into Java 7 | ||
| 91 | //|| type == Void.class | ||
| 92 | // Locally known null-only class: | ||
| 93 | || type == Empty.class | ||
| 94 | ; | ||
| 95 | } | ||
| 96 | private static final Class<?> NULL_CLASS; | ||
| 97 | static { | ||
| 98 | Class<?> nullClass = null; | ||
| 99 | try { | ||
| 100 | nullClass = Class.forName("java.lang.Null"); | ||
| 101 | } catch (ClassNotFoundException ex) { | ||
| 102 | // OK, we'll cope | ||
| 103 | } | ||
| 104 | NULL_CLASS = nullClass; | ||
| 105 | } | ||
| 106 | |||
| 107 | /** | ||
| 108 | * True if a method handle can receive a call under a slightly different | ||
| 109 | * method type, without moving or reformatting any stack elements. | ||
| 110 | * | ||
| 111 | * @param call the type of call being made | ||
| 112 | * @param recv the type of the method handle receiving the call | ||
| 113 | * @return whether the retyping can be done without motion or reformatting | ||
| 114 | */ | ||
| 115 | public static boolean isNullConversion(MethodType call, MethodType recv) { | ||
| 116 | if (call == recv) return true; | ||
| 117 | int len = call.parameterCount(); | ||
| 118 | if (len != recv.parameterCount()) return false; | ||
| 119 | for (int i = 0; i < len; i++) | ||
| 120 | if (!isNullConversion(call.parameterType(i), recv.parameterType(i))) | ||
| 121 | return false; | ||
| 122 | return isNullConversion(recv.returnType(), call.returnType()); | ||
| 123 | } | ||
| 124 | |||
| 125 | //TO DO: isRawConversion | ||
| 126 | |||
| 127 | /** | ||
| 128 | * Determine if the JVM verifier allows a value of type call to be | ||
| 129 | * passed to a formal parameter (or return variable) of type recv. | ||
| 130 | * Returns 1 if the verifier allows the types to match without conversion. | ||
| 131 | * Returns -1 if the types can be made to match by a JVM-supported adapter. | ||
| 132 | * Cases supported are: | ||
| 133 | * <ul><li>checkcast | ||
| 134 | * </li><li>conversion between any two integral types (but not floats) | ||
| 135 | * </li><li>unboxing from a wrapper to its corresponding primitive type | ||
| 136 | * </li><li>conversion in either direction between float and double | ||
| 137 | * </li></ul> | ||
| 138 | * (Autoboxing is not supported here; it must be done via Java code.) | ||
| 139 | * Returns 0 otherwise. | ||
| 140 | */ | ||
| 141 | public static int canPassUnchecked(Class<?> src, Class<?> dst) { | ||
| 142 | if (src == dst) | ||
| 143 | return 1; | ||
| 144 | |||
| 145 | if (dst.isPrimitive()) { | ||
| 146 | if (dst == void.class) | ||
| 147 | // Return anything to a caller expecting void. | ||
| 148 | // This is a property of the implementation, which links | ||
| 149 | // return values via a register rather than via a stack push. | ||
| 150 | // This makes it possible to ignore cleanly. | ||
| 151 | return 1; | ||
| 152 | if (src == void.class) | ||
| 153 | return 0; // void-to-something? | ||
| 154 | if (!src.isPrimitive()) | ||
| 155 | // Cannot pass a reference to any primitive type (exc. void). | ||
| 156 | return 0; | ||
| 157 | Wrapper sw = Wrapper.forPrimitiveType(src); | ||
| 158 | Wrapper dw = Wrapper.forPrimitiveType(dst); | ||
| 159 | if (sw.isSubwordOrInt() && dw.isSubwordOrInt()) { | ||
| 160 | if (sw.bitWidth() >= dw.bitWidth()) | ||
| 161 | return -1; // truncation may be required | ||
| 162 | if (!dw.isSigned() && sw.isSigned()) | ||
| 163 | return -1; // sign elimination may be required | ||
| 164 | return 1; | ||
| 165 | } | ||
| 166 | if (src == float.class || dst == float.class) { | ||
| 167 | if (src == double.class || dst == double.class) | ||
| 168 | return -1; // floating conversion may be required | ||
| 169 | else | ||
| 170 | return 0; // other primitive conversions NYI | ||
| 171 | } else { | ||
| 172 | // all fixed-point conversions are supported | ||
| 173 | return 0; | ||
| 174 | } | ||
| 175 | } else if (src.isPrimitive()) { | ||
| 176 | // Cannot pass a primitive to any reference type. | ||
| 177 | // (Maybe allow null.class?) | ||
| 178 | return 0; | ||
| 179 | } | ||
| 180 | |||
| 181 | // Handle reference types in the rest of the block: | ||
| 182 | |||
| 183 | // The verifier treats interfaces exactly like Object. | ||
| 184 | if (isNullReferenceConversion(src, dst)) | ||
| 185 | // pass any reference to object or an arb. interface | ||
| 186 | return 1; | ||
| 187 | // else it's a definite "maybe" (cast is required) | ||
| 188 | return -1; | ||
| 189 | } | ||
| 190 | |||
| 191 | public static int canPassRaw(Class<?> src, Class<?> dst) { | ||
| 192 | if (dst.isPrimitive()) { | ||
| 193 | if (dst == void.class) | ||
| 194 | // As above, return anything to a caller expecting void. | ||
| 195 | return 1; | ||
| 196 | if (src == void.class) | ||
| 197 | // Special permission for raw conversions: allow a void | ||
| 198 | // to be captured as a garbage int. | ||
| 199 | // Caller promises that the actual value will be disregarded. | ||
| 200 | return dst == int.class ? 1 : 0; | ||
| 201 | if (isNullType(src)) | ||
| 202 | // Special permission for raw conversions: allow a null | ||
| 203 | // to be reinterpreted as anything. For objects, it is safe, | ||
| 204 | // and for primitives you get a garbage value (probably zero). | ||
| 205 | return 1; | ||
| 206 | if (!src.isPrimitive()) | ||
| 207 | return 0; | ||
| 208 | Wrapper sw = Wrapper.forPrimitiveType(src); | ||
| 209 | Wrapper dw = Wrapper.forPrimitiveType(dst); | ||
| 210 | if (sw.stackSlots() == dw.stackSlots()) | ||
| 211 | return 1; // can do a reinterpret-cast on a stacked primitive | ||
| 212 | if (sw.isSubwordOrInt() && dw == Wrapper.VOID) | ||
| 213 | return 1; // can drop an outgoing int value | ||
| 214 | return 0; | ||
| 215 | } else if (src.isPrimitive()) { | ||
| 216 | return 0; | ||
| 217 | } | ||
| 218 | |||
| 219 | // Both references. | ||
| 220 | if (isNullReferenceConversion(src, dst)) | ||
| 221 | return 1; | ||
| 222 | return -1; | ||
| 223 | } | ||
| 224 | |||
| 225 | public static boolean isSpreadArgType(Class<?> spreadArg) { | ||
| 226 | return spreadArg.isArray(); | ||
| 227 | } | ||
| 228 | public static Class<?> spreadArgElementType(Class<?> spreadArg, int i) { | ||
| 229 | return spreadArg.getComponentType(); | ||
| 230 | } | ||
| 231 | } | ||