1 /*
2  * Copyright (c) 2010, 2013, 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 /*
27  * This file is available under and governed by the GNU General Public
28  * License version 2 only, as published by the Free Software Foundation.
29  * However, the following notice accompanied the original version of this
30  * file, and Oracle licenses the original version of this file under the BSD
31  * license:
32  */
33 /*
34    Copyright 2009-2013 Attila Szegedi
35 
36    Licensed under both the Apache License, Version 2.0 (the "Apache License")
37    and the BSD License (the "BSD License"), with licensee being free to
38    choose either of the two at their discretion.
39 
40    You may not use this file except in compliance with either the Apache
41    License or the BSD License.
42 
43    If you choose to use this file in compliance with the Apache License, the
44    following notice applies to you:
45 
46        You may obtain a copy of the Apache License at
47 
48            http://www.apache.org/licenses/LICENSE-2.0
49 
50        Unless required by applicable law or agreed to in writing, software
51        distributed under the License is distributed on an "AS IS" BASIS,
52        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
53        implied. See the License for the specific language governing
54        permissions and limitations under the License.
55 
56    If you choose to use this file in compliance with the BSD License, the
57    following notice applies to you:
58 
59        Redistribution and use in source and binary forms, with or without
60        modification, are permitted provided that the following conditions are
61        met:
62        * Redistributions of source code must retain the above copyright
63          notice, this list of conditions and the following disclaimer.
64        * Redistributions in binary form must reproduce the above copyright
65          notice, this list of conditions and the following disclaimer in the
66          documentation and/or other materials provided with the distribution.
67        * Neither the name of the copyright holder nor the names of
68          contributors may be used to endorse or promote products derived from
69          this software without specific prior written permission.
70 
71        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
72        IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
73        TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
74        PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
75        BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
76        CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
77        SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
78        BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
79        WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
80        OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
81        ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
82 */
83 
84 package jdk.internal.dynalink.support;
85 
86 import java.lang.invoke.MethodHandle;
87 import java.lang.invoke.MethodHandles;
88 import java.lang.invoke.MethodType;
89 import java.util.logging.Level;
90 import java.util.logging.Logger;
91 import jdk.internal.dynalink.DynamicLinker;
92 import jdk.internal.dynalink.linker.LinkerServices;
93 
94 /**
95  * Utility methods for creating typical guards. TODO: introduce reasonable caching of created guards.
96  *
97  * @author Attila Szegedi
98  */
99 public class Guards {
100     private static final Logger LOG = Logger
101             .getLogger(Guards.class.getName(), "jdk.internal.dynalink.support.messages");
102 
Guards()103     private Guards() {
104     }
105 
106     /**
107      * Creates a guard method handle with arguments of a specified type, but with boolean return value. When invoked, it
108      * returns true if the first argument is of the specified class (exactly of it, not a subclass). The rest of the
109      * arguments will be ignored.
110      *
111      * @param clazz the class of the first argument to test for
112      * @param type the method type
113      * @return a method handle testing whether its first argument is of the specified class.
114      */
115     @SuppressWarnings("boxing")
isOfClass(final Class<?> clazz, final MethodType type)116     public static MethodHandle isOfClass(final Class<?> clazz, final MethodType type) {
117         final Class<?> declaredType = type.parameterType(0);
118         if(clazz == declaredType) {
119             LOG.log(Level.WARNING, "isOfClassGuardAlwaysTrue", new Object[] { clazz.getName(), 0, type, DynamicLinker.getLinkedCallSiteLocation() });
120             return constantTrue(type);
121         }
122         if(!declaredType.isAssignableFrom(clazz)) {
123             LOG.log(Level.WARNING, "isOfClassGuardAlwaysFalse", new Object[] { clazz.getName(), 0, type, DynamicLinker.getLinkedCallSiteLocation() });
124             return constantFalse(type);
125         }
126         return getClassBoundArgumentTest(IS_OF_CLASS, clazz, 0, type);
127     }
128 
129     /**
130      * Creates a method handle with arguments of a specified type, but with boolean return value. When invoked, it
131      * returns true if the first argument is instance of the specified class or its subclass). The rest of the arguments
132      * will be ignored.
133      *
134      * @param clazz the class of the first argument to test for
135      * @param type the method type
136      * @return a method handle testing whether its first argument is of the specified class or subclass.
137      */
isInstance(final Class<?> clazz, final MethodType type)138     public static MethodHandle isInstance(final Class<?> clazz, final MethodType type) {
139         return isInstance(clazz, 0, type);
140     }
141 
142     /**
143      * Creates a method handle with arguments of a specified type, but with boolean return value. When invoked, it
144      * returns true if the n'th argument is instance of the specified class or its subclass). The rest of the arguments
145      * will be ignored.
146      *
147      * @param clazz the class of the first argument to test for
148      * @param pos the position on the argument list to test
149      * @param type the method type
150      * @return a method handle testing whether its first argument is of the specified class or subclass.
151      */
152     @SuppressWarnings("boxing")
isInstance(final Class<?> clazz, final int pos, final MethodType type)153     public static MethodHandle isInstance(final Class<?> clazz, final int pos, final MethodType type) {
154         final Class<?> declaredType = type.parameterType(pos);
155         if(clazz.isAssignableFrom(declaredType)) {
156             LOG.log(Level.WARNING, "isInstanceGuardAlwaysTrue", new Object[] { clazz.getName(), pos, type, DynamicLinker.getLinkedCallSiteLocation() });
157             return constantTrue(type);
158         }
159         if(!declaredType.isAssignableFrom(clazz)) {
160             LOG.log(Level.WARNING, "isInstanceGuardAlwaysFalse", new Object[] { clazz.getName(), pos, type, DynamicLinker.getLinkedCallSiteLocation() });
161             return constantFalse(type);
162         }
163         return getClassBoundArgumentTest(IS_INSTANCE, clazz, pos, type);
164     }
165 
166     /**
167      * Creates a method handle that returns true if the argument in the specified position is a Java array.
168      *
169      * @param pos the position in the argument lit
170      * @param type the method type of the handle
171      * @return a method handle that returns true if the argument in the specified position is a Java array; the rest of
172      * the arguments are ignored.
173      */
174     @SuppressWarnings("boxing")
isArray(final int pos, final MethodType type)175     public static MethodHandle isArray(final int pos, final MethodType type) {
176         final Class<?> declaredType = type.parameterType(pos);
177         if(declaredType.isArray()) {
178             LOG.log(Level.WARNING, "isArrayGuardAlwaysTrue", new Object[] { pos, type, DynamicLinker.getLinkedCallSiteLocation() });
179             return constantTrue(type);
180         }
181         if(!declaredType.isAssignableFrom(Object[].class)) {
182             LOG.log(Level.WARNING, "isArrayGuardAlwaysFalse", new Object[] { pos, type, DynamicLinker.getLinkedCallSiteLocation() });
183             return constantFalse(type);
184         }
185         return asType(IS_ARRAY, pos, type);
186     }
187 
188     /**
189      * Return true if it is safe to strongly reference a class from the referred class loader from a class associated
190      * with the referring class loader without risking a class loader memory leak.
191      *
192      * @param referrerLoader the referrer class loader
193      * @param referredLoader the referred class loader
194      * @return true if it is safe to strongly reference the class
195      */
canReferenceDirectly(final ClassLoader referrerLoader, final ClassLoader referredLoader)196     public static boolean canReferenceDirectly(final ClassLoader referrerLoader, final ClassLoader referredLoader) {
197         if(referredLoader == null) {
198             // Can always refer directly to a system class
199             return true;
200         }
201         if(referrerLoader == null) {
202             // System classes can't refer directly to any non-system class
203             return false;
204         }
205         // Otherwise, can only refer directly to classes residing in same or
206         // parent class loader.
207 
208         ClassLoader referrer = referrerLoader;
209         do {
210             if(referrer == referredLoader) {
211                 return true;
212             }
213             referrer = referrer.getParent();
214         } while(referrer != null);
215         return false;
216     }
217 
getClassBoundArgumentTest(final MethodHandle test, final Class<?> clazz, final int pos, final MethodType type)218     private static MethodHandle getClassBoundArgumentTest(final MethodHandle test, final Class<?> clazz, final int pos, final MethodType type) {
219         // Bind the class to the first argument of the test
220         return asType(test.bindTo(clazz), pos, type);
221     }
222 
223     /**
224      * Takes a guard-test method handle, and adapts it to the requested type, returning a boolean. Only applies
225      * conversions as per {@link MethodHandle#asType(MethodType)}.
226      * @param test the test method handle
227      * @param type the type to adapt the method handle to
228      * @return the adapted method handle
229      */
asType(final MethodHandle test, final MethodType type)230     public static MethodHandle asType(final MethodHandle test, final MethodType type) {
231         return test.asType(getTestType(test, type));
232     }
233 
234     /**
235      * Takes a guard-test method handle, and adapts it to the requested type, returning a boolean. Applies the passed
236      * {@link LinkerServices} object's {@link LinkerServices#asType(MethodHandle, MethodType)}.
237      * @param linkerServices the linker services to use for type conversions
238      * @param test the test method handle
239      * @param type the type to adapt the method handle to
240      * @return the adapted method handle
241      */
asType(final LinkerServices linkerServices, final MethodHandle test, final MethodType type)242     public static MethodHandle asType(final LinkerServices linkerServices, final MethodHandle test, final MethodType type) {
243         return linkerServices.asType(test, getTestType(test, type));
244     }
245 
getTestType(final MethodHandle test, final MethodType type)246     private static MethodType getTestType(final MethodHandle test, final MethodType type) {
247         return type.dropParameterTypes(test.type().parameterCount(),
248                 type.parameterCount()).changeReturnType(boolean.class);
249     }
250 
asType(final MethodHandle test, final int pos, final MethodType type)251     private static MethodHandle asType(final MethodHandle test, final int pos, final MethodType type) {
252         assert test != null;
253         assert type != null;
254         assert type.parameterCount() > 0;
255         assert pos >= 0 && pos < type.parameterCount();
256         assert test.type().parameterCount() == 1;
257         assert test.type().returnType() == Boolean.TYPE;
258         return MethodHandles.permuteArguments(test.asType(test.type().changeParameterType(0, type.parameterType(pos))),
259                 type.changeReturnType(Boolean.TYPE), new int[] { pos });
260     }
261 
262     private static final MethodHandle IS_INSTANCE = Lookup.PUBLIC.findVirtual(Class.class, "isInstance",
263             MethodType.methodType(Boolean.TYPE, Object.class));
264 
265     private static final MethodHandle IS_OF_CLASS;
266     private static final MethodHandle IS_ARRAY;
267     private static final MethodHandle IS_IDENTICAL;
268     private static final MethodHandle IS_NULL;
269     private static final MethodHandle IS_NOT_NULL;
270 
271     static {
272         final Lookup lookup = new Lookup(MethodHandles.lookup());
273 
274         IS_OF_CLASS  = lookup.findOwnStatic("isOfClass",   Boolean.TYPE, Class.class, Object.class);
275         IS_ARRAY     = lookup.findOwnStatic("isArray",     Boolean.TYPE, Object.class);
276         IS_IDENTICAL = lookup.findOwnStatic("isIdentical", Boolean.TYPE, Object.class, Object.class);
277         IS_NULL      = lookup.findOwnStatic("isNull",      Boolean.TYPE, Object.class);
278         IS_NOT_NULL  = lookup.findOwnStatic("isNotNull",   Boolean.TYPE, Object.class);
279     }
280 
281     /**
282      * Creates a guard method that tests its only argument for being of an exact particular class.
283      * @param clazz the class to test for.
284      * @return the desired guard method.
285      */
286     public static MethodHandle getClassGuard(final Class<?> clazz) {
287         return IS_OF_CLASS.bindTo(clazz);
288     }
289 
290     /**
291      * Creates a guard method that tests its only argument for being an instance of a particular class.
292      * @param clazz the class to test for.
293      * @return the desired guard method.
294      */
295     public static MethodHandle getInstanceOfGuard(final Class<?> clazz) {
296         return IS_INSTANCE.bindTo(clazz);
297     }
298 
299     /**
300      * Creates a guard method that tests its only argument for being referentially identical to another object
301      * @param obj the object used as referential identity test
302      * @return the desired guard method.
303      */
304     public static MethodHandle getIdentityGuard(final Object obj) {
305         return IS_IDENTICAL.bindTo(obj);
306     }
307 
308     /**
309      * Returns a guard that tests whether the first argument is null.
310      * @return a guard that tests whether the first argument is null.
311      */
312     public static MethodHandle isNull() {
313         return IS_NULL;
314     }
315 
316     /**
317      * Returns a guard that tests whether the first argument is not null.
318      * @return a guard that tests whether the first argument is not null.
319      */
320     public static MethodHandle isNotNull() {
321         return IS_NOT_NULL;
322     }
323 
324     @SuppressWarnings("unused")
325     private static boolean isNull(final Object obj) {
326         return obj == null;
327     }
328 
329     @SuppressWarnings("unused")
330     private static boolean isNotNull(final Object obj) {
331         return obj != null;
332     }
333 
334     @SuppressWarnings("unused")
335     private static boolean isArray(final Object o) {
336         return o != null && o.getClass().isArray();
337     }
338 
339     @SuppressWarnings("unused")
340     private static boolean isOfClass(final Class<?> c, final Object o) {
341         return o != null && o.getClass() == c;
342     }
343 
344     @SuppressWarnings("unused")
345     private static boolean isIdentical(final Object o1, final Object o2) {
346         return o1 == o2;
347     }
348 
349     private static MethodHandle constantTrue(final MethodType type) {
350         return constantBoolean(Boolean.TRUE, type);
351     }
352 
353     private static MethodHandle constantFalse(final MethodType type) {
354         return constantBoolean(Boolean.FALSE, type);
355     }
356 
357     private static MethodHandle constantBoolean(final Boolean value, final MethodType type) {
358         return MethodHandles.permuteArguments(MethodHandles.constant(Boolean.TYPE, value),
359                 type.changeReturnType(Boolean.TYPE));
360     }
361 }
362