Path: blob/master/src/jdk.dynalink/share/classes/jdk/dynalink/linker/LinkerServices.java
41161 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* This file is available under and governed by the GNU General Public27* License version 2 only, as published by the Free Software Foundation.28* However, the following notice accompanied the original version of this29* file, and Oracle licenses the original version of this file under the BSD30* license:31*/32/*33Copyright 2009-2013 Attila Szegedi3435Redistribution and use in source and binary forms, with or without36modification, are permitted provided that the following conditions are37met:38* Redistributions of source code must retain the above copyright39notice, this list of conditions and the following disclaimer.40* Redistributions in binary form must reproduce the above copyright41notice, this list of conditions and the following disclaimer in the42documentation and/or other materials provided with the distribution.43* Neither the name of the copyright holder nor the names of44contributors may be used to endorse or promote products derived from45this software without specific prior written permission.4647THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS48IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED49TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A50PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER51BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR52CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF53SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR54BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,55WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR56OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF57ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.58*/5960package jdk.dynalink.linker;6162import java.lang.invoke.MethodHandle;63import java.lang.invoke.MethodHandles;64import java.lang.invoke.MethodType;65import java.util.function.Supplier;66import jdk.dynalink.DynamicLinker;67import jdk.dynalink.DynamicLinkerFactory;68import jdk.dynalink.SecureLookupSupplier;69import jdk.dynalink.linker.ConversionComparator.Comparison;70import jdk.dynalink.linker.support.TypeUtilities;7172/**73* Interface for services provided to {@link GuardingDynamicLinker} instances by74* the {@link DynamicLinker} that owns them.75*/76public interface LinkerServices {77/**78* Similar to {@link MethodHandle#asType(MethodType)} except it also hooks79* in method handles produced by all available80* {@link GuardingTypeConverterFactory} implementations, providing for81* language-specific type coercing of parameters. It will apply82* {@link MethodHandle#asType(MethodType)} for all primitive-to-primitive,83* wrapper-to-primitive, primitive-to-wrapper conversions as well as for all84* upcasts. For all other conversions, it'll insert85* {@link MethodHandles#filterArguments(MethodHandle, int, MethodHandle...)}86* with composite filters provided by {@link GuardingTypeConverterFactory}87* implementations.88*89* @param handle target method handle90* @param fromType the types of source arguments91* @return a method handle that is a suitable combination of92* {@link MethodHandle#asType(MethodType)},93* {@link MethodHandles#filterArguments(MethodHandle, int, MethodHandle...)},94* and {@link MethodHandles#filterReturnValue(MethodHandle, MethodHandle)}95* with {@link GuardingTypeConverterFactory}-produced type converters as96* filters.97*/98public MethodHandle asType(MethodHandle handle, MethodType fromType);99100/**101* Similar to {@link #asType(MethodHandle, MethodType)} except it treats102* return value type conversion specially. It only converts the return type103* of the method handle when it can be done using a conversion that loses104* neither precision nor magnitude, otherwise it leaves it unchanged. These105* are the only return value conversions that should be performed by106* individual language-specific linkers, and107* {@link DynamicLinkerFactory#setPrelinkTransformer(GuardedInvocationTransformer)108* pre-link transformer of the dynamic linker} should implement the strategy109* for dealing with potentially lossy return type conversions in a manner110* specific to the language runtime where the call site is located.111*112* @param handle target method handle113* @param fromType the types of source arguments114* @return a method handle that is a suitable combination of115* {@link MethodHandle#asType(MethodType)}, and116* {@link MethodHandles#filterArguments(MethodHandle, int, MethodHandle...)}117* with {@link GuardingTypeConverterFactory}-produced type converters as filters.118*/119public default MethodHandle asTypeLosslessReturn(final MethodHandle handle, final MethodType fromType) {120final Class<?> handleReturnType = handle.type().returnType();121return asType(handle, TypeUtilities.isConvertibleWithoutLoss(handleReturnType, fromType.returnType()) ?122fromType : fromType.changeReturnType(handleReturnType));123}124125/**126* Given a source and target type, returns a method handle that converts127* between them. Never returns null; in worst case it will return an128* identity conversion (that might fail for some values at runtime). You129* rarely need to use this method directly and should mostly rely on130* {@link #asType(MethodHandle, MethodType)} instead. This method is needed131* when you need to reuse existing type conversion machinery outside the132* context of processing a link request.133* @param sourceType the type to convert from134* @param targetType the type to convert to135* @return a method handle performing the conversion.136*/137public MethodHandle getTypeConverter(Class<?> sourceType, Class<?> targetType);138139/**140* Returns true if there might exist a conversion between the requested141* types (either an automatic JVM conversion, or one provided by any142* available {@link GuardingTypeConverterFactory}), or false if there143* definitely does not exist a conversion between the requested types. Note144* that returning true does not guarantee that the conversion will succeed145* at runtime for all values (especially if the "from" or "to" types are146* sufficiently generic), but returning false guarantees that it would fail.147*148* @param from the source type for the conversion149* @param to the target type for the conversion150* @return true if there can be a conversion, false if there can not.151*/152public boolean canConvert(Class<?> from, Class<?> to);153154/**155* Creates a guarded invocation delegating back to the {@link DynamicLinker}156* that exposes this linker services object. The dynamic linker will then157* itself delegate the linking to all of its managed158* {@link GuardingDynamicLinker}s including potentially this one if no159* linker responds earlier, so beware of infinite recursion. You'll160* typically craft the link request so that it will be different than the161* one you are currently trying to link.162*163* @param linkRequest a request for linking the invocation164* @return a guarded invocation linked by some of the guarding dynamic165* linkers managed by the top-level dynamic linker. Can be null if no166* available linker is able to link the invocation. You will typically use167* the elements of the returned invocation to compose your own invocation.168* @throws Exception in case the top-level linker throws an exception169*/170public GuardedInvocation getGuardedInvocation(LinkRequest linkRequest) throws Exception;171172/**173* Determines which of the two type conversions from a source type to the174* two target types is preferred. This is used for dynamic overloaded method175* resolution. If the source type is convertible to exactly one target type176* with a method invocation conversion, it is chosen, otherwise available177* {@link ConversionComparator}s are consulted.178* @param sourceType the source type.179* @param targetType1 one potential target type180* @param targetType2 another potential target type.181* @return one of Comparison constants that establish which – if any182* – of the target types is preferable for the conversion.183*/184public Comparison compareConversion(Class<?> sourceType, Class<?> targetType1, Class<?> targetType2);185186/**187* Modifies the method handle so that any parameters that can receive188* potentially internal language runtime objects will have a filter added on189* them to prevent them from escaping, potentially by wrapping them. It can190* also potentially add an unwrapping filter to the return value. Basically191* transforms the method handle using the transformer configured by192* {@link DynamicLinkerFactory#setInternalObjectsFilter(MethodHandleTransformer)}.193* @param target the target method handle194* @return a method handle with parameters and/or return type potentially195* filtered for wrapping and unwrapping.196*/197public MethodHandle filterInternalObjects(final MethodHandle target);198199/**200* Executes an operation within the context of a particular201* {@code MethodHandles.Lookup} lookup object. Normally, methods on202* {@code LinkerServices} are invoked as part of the linking mechanism in203* which case Dynalink internally maintains a per-thread current lookup204* (the one belonging to the descriptor of the call site being linked). This205* lookup can be retrieved by any {@link GuardingTypeConverterFactory}206* involved in linking if it needs to generate lookup-sensitive converters.207* However, linker services' methods can be invoked outside the linking208* process too when implementing invocation-time dispatch schemes, invoking209* conversions at runtime, etc. If it becomes necessary to use any type210* converter in this situation, and it needs a lookup, it will normally only211* get {@link MethodHandles#publicLookup()} as the thread is not engaged in212* a linking operation. If there is a way to meaningfully associate the213* operation to the context of some caller class, consider performing it214* within an invocation of this method and passing a full-strength lookup215* for that class, as it will associate that lookup with the current thread216* for the duration of the operation. Note that since you are passing a217* {@link SecureLookupSupplier}, any invoked type converter factories will218* still need to hold the necessary runtime permission to be able to get the219* lookup should they need it.220* @param <T> the type of the return value provided by the passed-in supplier.221* @param operation the operation to execute in context of the specified lookup.222* @param lookupSupplier secure supplier of the lookup223* @return the return value of the action224* @throws NullPointerException if either action or lookupSupplier are null.225* @see GuardingTypeConverterFactory#convertToType(Class, Class, Supplier)226*/227public <T> T getWithLookup(final Supplier<T> operation, final SecureLookupSupplier lookupSupplier);228}229230231