1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/jdk/internal/dynalink/support/Guards.java Thu Feb 14 13:22:26 2013 +0100 1.3 @@ -0,0 +1,360 @@ 1.4 +/* 1.5 + * Copyright (c) 2010, 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 +/* 1.30 + * This file is available under and governed by the GNU General Public 1.31 + * License version 2 only, as published by the Free Software Foundation. 1.32 + * However, the following notice accompanied the original version of this 1.33 + * file, and Oracle licenses the original version of this file under the BSD 1.34 + * license: 1.35 + */ 1.36 +/* 1.37 + Copyright 2009-2013 Attila Szegedi 1.38 + 1.39 + Licensed under both the Apache License, Version 2.0 (the "Apache License") 1.40 + and the BSD License (the "BSD License"), with licensee being free to 1.41 + choose either of the two at their discretion. 1.42 + 1.43 + You may not use this file except in compliance with either the Apache 1.44 + License or the BSD License. 1.45 + 1.46 + If you choose to use this file in compliance with the Apache License, the 1.47 + following notice applies to you: 1.48 + 1.49 + You may obtain a copy of the Apache License at 1.50 + 1.51 + http://www.apache.org/licenses/LICENSE-2.0 1.52 + 1.53 + Unless required by applicable law or agreed to in writing, software 1.54 + distributed under the License is distributed on an "AS IS" BASIS, 1.55 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 1.56 + implied. See the License for the specific language governing 1.57 + permissions and limitations under the License. 1.58 + 1.59 + If you choose to use this file in compliance with the BSD License, the 1.60 + following notice applies to you: 1.61 + 1.62 + Redistribution and use in source and binary forms, with or without 1.63 + modification, are permitted provided that the following conditions are 1.64 + met: 1.65 + * Redistributions of source code must retain the above copyright 1.66 + notice, this list of conditions and the following disclaimer. 1.67 + * Redistributions in binary form must reproduce the above copyright 1.68 + notice, this list of conditions and the following disclaimer in the 1.69 + documentation and/or other materials provided with the distribution. 1.70 + * Neither the name of the copyright holder nor the names of 1.71 + contributors may be used to endorse or promote products derived from 1.72 + this software without specific prior written permission. 1.73 + 1.74 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 1.75 + IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 1.76 + TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 1.77 + PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER 1.78 + BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.79 + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.80 + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 1.81 + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 1.82 + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 1.83 + OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 1.84 + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.85 +*/ 1.86 + 1.87 +package jdk.internal.dynalink.support; 1.88 + 1.89 +import java.lang.invoke.MethodHandle; 1.90 +import java.lang.invoke.MethodHandles; 1.91 +import java.lang.invoke.MethodType; 1.92 +import java.util.logging.Level; 1.93 +import java.util.logging.Logger; 1.94 +import jdk.internal.dynalink.linker.LinkerServices; 1.95 + 1.96 + 1.97 +/** 1.98 + * Utility methods for creating typical guards. TODO: introduce reasonable caching of created guards. 1.99 + * 1.100 + * @author Attila Szegedi 1.101 + */ 1.102 +public class Guards { 1.103 + private static final Logger LOG = Logger 1.104 + .getLogger(Guards.class.getName(), "jdk.internal.dynalink.support.messages"); 1.105 + 1.106 + private Guards() { 1.107 + } 1.108 + 1.109 + /** 1.110 + * Creates a guard method handle with arguments of a specified type, but with boolean return value. When invoked, it 1.111 + * returns true if the first argument is of the specified class (exactly of it, not a subclass). The rest of the 1.112 + * arguments will be ignored. 1.113 + * 1.114 + * @param clazz the class of the first argument to test for 1.115 + * @param type the method type 1.116 + * @return a method handle testing whether its first argument is of the specified class. 1.117 + */ 1.118 + @SuppressWarnings("boxing") 1.119 + public static MethodHandle isOfClass(Class<?> clazz, MethodType type) { 1.120 + final Class<?> declaredType = type.parameterType(0); 1.121 + if(clazz == declaredType) { 1.122 + LOG.log(Level.WARNING, "isOfClassGuardAlwaysTrue", new Object[] { clazz.getName(), 0, type }); 1.123 + return constantTrue(type); 1.124 + } 1.125 + if(!declaredType.isAssignableFrom(clazz)) { 1.126 + LOG.log(Level.WARNING, "isOfClassGuardAlwaysFalse", new Object[] { clazz.getName(), 0, type }); 1.127 + return constantFalse(type); 1.128 + } 1.129 + return getClassBoundArgumentTest(IS_OF_CLASS, clazz, 0, type); 1.130 + } 1.131 + 1.132 + /** 1.133 + * Creates a method handle with arguments of a specified type, but with boolean return value. When invoked, it 1.134 + * returns true if the first argument is instance of the specified class or its subclass). The rest of the arguments 1.135 + * will be ignored. 1.136 + * 1.137 + * @param clazz the class of the first argument to test for 1.138 + * @param type the method type 1.139 + * @return a method handle testing whether its first argument is of the specified class or subclass. 1.140 + */ 1.141 + public static MethodHandle isInstance(Class<?> clazz, MethodType type) { 1.142 + return isInstance(clazz, 0, type); 1.143 + } 1.144 + 1.145 + /** 1.146 + * Creates a method handle with arguments of a specified type, but with boolean return value. When invoked, it 1.147 + * returns true if the n'th argument is instance of the specified class or its subclass). The rest of the arguments 1.148 + * will be ignored. 1.149 + * 1.150 + * @param clazz the class of the first argument to test for 1.151 + * @param pos the position on the argument list to test 1.152 + * @param type the method type 1.153 + * @return a method handle testing whether its first argument is of the specified class or subclass. 1.154 + */ 1.155 + @SuppressWarnings("boxing") 1.156 + public static MethodHandle isInstance(Class<?> clazz, int pos, MethodType type) { 1.157 + final Class<?> declaredType = type.parameterType(pos); 1.158 + if(clazz.isAssignableFrom(declaredType)) { 1.159 + LOG.log(Level.WARNING, "isInstanceGuardAlwaysTrue", new Object[] { clazz.getName(), pos, type }); 1.160 + return constantTrue(type); 1.161 + } 1.162 + if(!declaredType.isAssignableFrom(clazz)) { 1.163 + LOG.log(Level.WARNING, "isInstanceGuardAlwaysFalse", new Object[] { clazz.getName(), pos, type }); 1.164 + return constantFalse(type); 1.165 + } 1.166 + return getClassBoundArgumentTest(IS_INSTANCE, clazz, pos, type); 1.167 + } 1.168 + 1.169 + /** 1.170 + * Creates a method handle that returns true if the argument in the specified position is a Java array. 1.171 + * 1.172 + * @param pos the position in the argument lit 1.173 + * @param type the method type of the handle 1.174 + * @return a method handle that returns true if the argument in the specified position is a Java array; the rest of 1.175 + * the arguments are ignored. 1.176 + */ 1.177 + @SuppressWarnings("boxing") 1.178 + public static MethodHandle isArray(int pos, MethodType type) { 1.179 + final Class<?> declaredType = type.parameterType(pos); 1.180 + if(declaredType.isArray()) { 1.181 + LOG.log(Level.WARNING, "isArrayGuardAlwaysTrue", new Object[] { pos, type }); 1.182 + return constantTrue(type); 1.183 + } 1.184 + if(!declaredType.isAssignableFrom(Object[].class)) { 1.185 + LOG.log(Level.WARNING, "isArrayGuardAlwaysFalse", new Object[] { pos, type }); 1.186 + return constantFalse(type); 1.187 + } 1.188 + return asType(IS_ARRAY, pos, type); 1.189 + } 1.190 + 1.191 + /** 1.192 + * Return true if it is safe to strongly reference a class from the referred class loader from a class associated 1.193 + * with the referring class loader without risking a class loader memory leak. 1.194 + * 1.195 + * @param referrerLoader the referrer class loader 1.196 + * @param referredLoader the referred class loader 1.197 + * @return true if it is safe to strongly reference the class 1.198 + */ 1.199 + public static boolean canReferenceDirectly(ClassLoader referrerLoader, final ClassLoader referredLoader) { 1.200 + if(referredLoader == null) { 1.201 + // Can always refer directly to a system class 1.202 + return true; 1.203 + } 1.204 + if(referrerLoader == null) { 1.205 + // System classes can't refer directly to any non-system class 1.206 + return false; 1.207 + } 1.208 + // Otherwise, can only refer directly to classes residing in same or 1.209 + // parent class loader. 1.210 + 1.211 + ClassLoader referrer = referrerLoader; 1.212 + do { 1.213 + if(referrer == referredLoader) { 1.214 + return true; 1.215 + } 1.216 + referrer = referrer.getParent(); 1.217 + } while(referrer != null); 1.218 + return false; 1.219 + } 1.220 + 1.221 + private static MethodHandle getClassBoundArgumentTest(MethodHandle test, Class<?> clazz, int pos, MethodType type) { 1.222 + // Bind the class to the first argument of the test 1.223 + return asType(test.bindTo(clazz), pos, type); 1.224 + } 1.225 + 1.226 + /** 1.227 + * Takes a guard-test method handle, and adapts it to the requested type, returning a boolean. Only applies 1.228 + * conversions as per {@link MethodHandle#asType(MethodType)}. 1.229 + * @param test the test method handle 1.230 + * @param type the type to adapt the method handle to 1.231 + * @return the adapted method handle 1.232 + */ 1.233 + public static MethodHandle asType(MethodHandle test, MethodType type) { 1.234 + return test.asType(getTestType(test, type)); 1.235 + } 1.236 + 1.237 + /** 1.238 + * Takes a guard-test method handle, and adapts it to the requested type, returning a boolean. Applies the passed 1.239 + * {@link LinkerServices} object's {@link LinkerServices#asType(MethodHandle, MethodType)}. 1.240 + * @param linkerServices the linker services to use for type conversions 1.241 + * @param test the test method handle 1.242 + * @param type the type to adapt the method handle to 1.243 + * @return the adapted method handle 1.244 + */ 1.245 + public static MethodHandle asType(LinkerServices linkerServices, MethodHandle test, MethodType type) { 1.246 + return linkerServices.asType(test, getTestType(test, type)); 1.247 + } 1.248 + 1.249 + private static MethodType getTestType(MethodHandle test, MethodType type) { 1.250 + return type.dropParameterTypes(test.type().parameterCount(), 1.251 + type.parameterCount()).changeReturnType(boolean.class); 1.252 + } 1.253 + 1.254 + private static MethodHandle asType(MethodHandle test, int pos, MethodType type) { 1.255 + assert test != null; 1.256 + assert type != null; 1.257 + assert type.parameterCount() > 0; 1.258 + assert pos >= 0 && pos < type.parameterCount(); 1.259 + assert test.type().parameterCount() == 1; 1.260 + assert test.type().returnType() == Boolean.TYPE; 1.261 + return MethodHandles.permuteArguments(test.asType(test.type().changeParameterType(0, type.parameterType(pos))), 1.262 + type.changeReturnType(Boolean.TYPE), new int[] { pos }); 1.263 + } 1.264 + 1.265 + private static final MethodHandle IS_OF_CLASS = new Lookup(MethodHandles.lookup()).findStatic(Guards.class, 1.266 + "isOfClass", MethodType.methodType(Boolean.TYPE, Class.class, Object.class)); 1.267 + 1.268 + private static final MethodHandle IS_INSTANCE = Lookup.PUBLIC.findVirtual(Class.class, "isInstance", 1.269 + MethodType.methodType(Boolean.TYPE, Object.class)); 1.270 + 1.271 + private static final MethodHandle IS_ARRAY = new Lookup(MethodHandles.lookup()).findStatic(Guards.class, "isArray", 1.272 + MethodType.methodType(Boolean.TYPE, Object.class)); 1.273 + 1.274 + private static final MethodHandle IS_IDENTICAL = new Lookup(MethodHandles.lookup()).findStatic(Guards.class, 1.275 + "isIdentical", MethodType.methodType(Boolean.TYPE, Object.class, Object.class)); 1.276 + 1.277 + private static final MethodHandle IS_NULL = new Lookup(MethodHandles.lookup()).findStatic(Guards.class, 1.278 + "isNull", MethodType.methodType(Boolean.TYPE, Object.class)); 1.279 + 1.280 + private static final MethodHandle IS_NOT_NULL = new Lookup(MethodHandles.lookup()).findStatic(Guards.class, 1.281 + "isNotNull", MethodType.methodType(Boolean.TYPE, Object.class)); 1.282 + 1.283 + /** 1.284 + * Creates a guard method that tests its only argument for being of an exact particular class. 1.285 + * @param clazz the class to test for. 1.286 + * @return the desired guard method. 1.287 + */ 1.288 + public static MethodHandle getClassGuard(Class<?> clazz) { 1.289 + return IS_OF_CLASS.bindTo(clazz); 1.290 + } 1.291 + 1.292 + /** 1.293 + * Creates a guard method that tests its only argument for being an instance of a particular class. 1.294 + * @param clazz the class to test for. 1.295 + * @return the desired guard method. 1.296 + */ 1.297 + public static MethodHandle getInstanceOfGuard(Class<?> clazz) { 1.298 + return IS_INSTANCE.bindTo(clazz); 1.299 + } 1.300 + 1.301 + /** 1.302 + * Creates a guard method that tests its only argument for being referentially identical to another object 1.303 + * @param obj the object used as referential identity test 1.304 + * @return the desired guard method. 1.305 + */ 1.306 + public static MethodHandle getIdentityGuard(Object obj) { 1.307 + return IS_IDENTICAL.bindTo(obj); 1.308 + } 1.309 + 1.310 + /** 1.311 + * Returns a guard that tests whether the first argument is null. 1.312 + * @return a guard that tests whether the first argument is null. 1.313 + */ 1.314 + public static MethodHandle isNull() { 1.315 + return IS_NULL; 1.316 + } 1.317 + 1.318 + /** 1.319 + * Returns a guard that tests whether the first argument is not null. 1.320 + * @return a guard that tests whether the first argument is not null. 1.321 + */ 1.322 + public static MethodHandle isNotNull() { 1.323 + return IS_NOT_NULL; 1.324 + } 1.325 + 1.326 + @SuppressWarnings("unused") 1.327 + private static boolean isNull(Object obj) { 1.328 + return obj == null; 1.329 + } 1.330 + 1.331 + @SuppressWarnings("unused") 1.332 + private static boolean isNotNull(Object obj) { 1.333 + return obj != null; 1.334 + } 1.335 + 1.336 + @SuppressWarnings("unused") 1.337 + private static boolean isArray(Object o) { 1.338 + return o != null && o.getClass().isArray(); 1.339 + } 1.340 + 1.341 + @SuppressWarnings("unused") 1.342 + private static boolean isOfClass(Class<?> c, Object o) { 1.343 + return o != null && o.getClass() == c; 1.344 + } 1.345 + 1.346 + @SuppressWarnings("unused") 1.347 + private static boolean isIdentical(Object o1, Object o2) { 1.348 + return o1 == o2; 1.349 + } 1.350 + 1.351 + private static MethodHandle constantTrue(MethodType type) { 1.352 + return constantBoolean(Boolean.TRUE, type); 1.353 + } 1.354 + 1.355 + private static MethodHandle constantFalse(MethodType type) { 1.356 + return constantBoolean(Boolean.FALSE, type); 1.357 + } 1.358 + 1.359 + private static MethodHandle constantBoolean(Boolean value, MethodType type) { 1.360 + return MethodHandles.permuteArguments(MethodHandles.constant(Boolean.TYPE, value), 1.361 + type.changeReturnType(Boolean.TYPE)); 1.362 + } 1.363 +} 1.364 \ No newline at end of file