Path: blob/master/src/jdk.dynalink/share/classes/jdk/dynalink/CallSiteDescriptor.java
41154 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;6162import java.lang.invoke.MethodHandles;63import java.lang.invoke.MethodHandles.Lookup;64import java.lang.invoke.MethodType;65import java.util.Objects;66import java.util.function.Supplier;6768/**69* Call site descriptors contain all the information necessary for linking a70* call site. This information is normally passed as parameters to bootstrap71* methods and consists of the {@code MethodHandles.Lookup} object on the caller72* class in which the call site occurs, the dynamic operation at the call73* site, and the method type of the call site. {@code CallSiteDescriptor}74* objects are used in Dynalink to capture and store these parameters for75* subsequent use by the {@link DynamicLinker}.76* <p>77* The constructors of built-in {@link RelinkableCallSite} implementations all78* take a call site descriptor.79* <p>80* Call site descriptors must be immutable. You can use this class as-is or you81* can subclass it, especially if you need to add further information to the82* descriptors (typically, values passed in additional parameters to the83* bootstrap method. Since the descriptors must be immutable, you can set up a84* cache for equivalent descriptors to have the call sites share them.85* <p>86* The class extends {@link SecureLookupSupplier} for security-checked access to87* the {@code MethodHandles.Lookup} object it carries. This lookup should be used88* to find method handles to set as targets of the call site described by this89* descriptor.90*/91public class CallSiteDescriptor extends SecureLookupSupplier {92private final Operation operation;93private final MethodType methodType;9495/**96* Creates a new call site descriptor.97* @param lookup the lookup object describing the class the call site belongs to.98* When creating descriptors from a {@link java.lang.invoke} bootstrap method,99* it should be the lookup passed to the bootstrap.100* @param operation the dynamic operation at the call site.101* @param methodType the method type of the call site. When creating102* descriptors from a {@link java.lang.invoke} bootstrap method, it should be103* the method type passed to the bootstrap.104*/105public CallSiteDescriptor(final Lookup lookup, final Operation operation, final MethodType methodType) {106super(lookup);107this.operation = Objects.requireNonNull(operation, "name");108this.methodType = Objects.requireNonNull(methodType, "methodType");109}110111/**112* Returns the operation at the call site.113* @return the operation at the call site.114*/115public final Operation getOperation() {116return operation;117}118119/**120* The type of the method at the call site.121*122* @return type of the method at the call site.123*/124public final MethodType getMethodType() {125return methodType;126}127128/**129* Finds or creates a call site descriptor that only differs in its130* method type from this descriptor.131* Invokes {@link #changeMethodTypeInternal(MethodType)}.132*133* @param newMethodType the new method type134* @return a call site descriptor with changed method type.135* @throws NullPointerException if {@code newMethodType} is null.136*/137public final CallSiteDescriptor changeMethodType(final MethodType newMethodType) {138final CallSiteDescriptor changed = changeMethodTypeInternal(newMethodType);139140if (getClass() != CallSiteDescriptor.class) {141assertChangeInvariants(changed, "changeMethodTypeInternal");142alwaysAssert(operation == changed.operation, () -> "changeMethodTypeInternal must not change the descriptor's operation");143alwaysAssert(newMethodType == changed.methodType, () -> "changeMethodTypeInternal didn't set the correct new method type");144}145return changed;146}147148/**149* Finds or creates a call site descriptor that only differs in its150* method type from this descriptor. Subclasses must override this method151* to return an object of their exact class. If an overridden method changes152* something other than the method type in the descriptor (its class, lookup,153* or operation), or returns null, an {@code AssertionError} will be thrown154* from {@link #changeMethodType(MethodType)}.155*156* @param newMethodType the new method type157* @return a call site descriptor with the changed method type.158*/159protected CallSiteDescriptor changeMethodTypeInternal(final MethodType newMethodType) {160return new CallSiteDescriptor(getLookupPrivileged(), operation, newMethodType);161}162163/**164* Finds or creates a call site descriptor that only differs in its165* operation from this descriptor.166* Invokes {@link #changeOperationInternal(Operation)}.167*168* @param newOperation the new operation169* @return a call site descriptor with the changed operation.170* @throws NullPointerException if {@code newOperation} is null.171* @throws SecurityException if the descriptor's lookup isn't the172* {@link MethodHandles#publicLookup()}, and a security manager is present,173* and a check for {@code RuntimePermission("dynalink.getLookup")} fails.174* This is necessary as changing the operation in the call site descriptor175* allows fabrication of descriptors for arbitrary operations with the lookup.176*/177public final CallSiteDescriptor changeOperation(final Operation newOperation) {178getLookup(); // force security check179final CallSiteDescriptor changed = changeOperationInternal(newOperation);180181if (getClass() != CallSiteDescriptor.class) {182assertChangeInvariants(changed, "changeOperationInternal");183alwaysAssert(methodType == changed.methodType, () -> "changeOperationInternal must not change the descriptor's method type");184alwaysAssert(newOperation == changed.operation, () -> "changeOperationInternal didn't set the correct new operation");185}186return changed;187}188189/**190* Finds or creates a call site descriptor that only differs in its191* operation from this descriptor. Subclasses must override this method192* to return an object of their exact class. If an overridden method changes193* something other than the operation in the descriptor (its class, lookup,194* or method type), or returns null, an {@code AssertionError} will be thrown195* from {@link #changeOperation(Operation)}.196*197* @param newOperation the new operation198* @return a call site descriptor with the changed operation.199*/200protected CallSiteDescriptor changeOperationInternal(final Operation newOperation) {201return new CallSiteDescriptor(getLookupPrivileged(), newOperation, methodType);202}203204/**205* Returns true if this call site descriptor is equal to the passed object.206* It is considered equal if the other object is of the exact same class,207* their operations and method types are equal, and their lookups have the208* same {@link java.lang.invoke.MethodHandles.Lookup#lookupClass()} and209* {@link java.lang.invoke.MethodHandles.Lookup#lookupModes()}.210*/211@Override212public boolean equals(final Object obj) {213if (obj == this) {214return true;215} else if (obj == null) {216return false;217} else if (obj.getClass() != getClass()) {218return false;219}220final CallSiteDescriptor other = (CallSiteDescriptor)obj;221return operation.equals(other.operation) &&222methodType.equals(other.methodType) &&223lookupsEqual(getLookupPrivileged(), other.getLookupPrivileged());224}225226/**227* Compares two lookup objects for value-based equality. They are considered228* equal if they have the same229* {@link java.lang.invoke.MethodHandles.Lookup#lookupClass()} and230* {@link java.lang.invoke.MethodHandles.Lookup#lookupModes()}.231* @param l1 first lookup232* @param l2 second lookup233* @return true if the two lookups are equal, false otherwise.234*/235private static boolean lookupsEqual(final Lookup l1, final Lookup l2) {236return l1.lookupClass() == l2.lookupClass() && l1.lookupModes() == l2.lookupModes();237}238239/**240* Returns a value-based hash code of this call site descriptor computed241* from its operation, method type, and lookup object's lookup class and242* lookup modes.243* @return value-based hash code for this call site descriptor.244*/245@Override246public int hashCode() {247return operation.hashCode() + 31 * methodType.hashCode() + 31 * 31 * lookupHashCode(getLookupPrivileged());248}249250/**251* Returns a value-based hash code for the passed lookup object. It is252* based on the lookup object's253* {@link java.lang.invoke.MethodHandles.Lookup#lookupClass()} and254* {@link java.lang.invoke.MethodHandles.Lookup#lookupModes()} values.255* @param lookup the lookup object.256* @return a hash code for the object..257*/258private static int lookupHashCode(final Lookup lookup) {259return lookup.lookupClass().hashCode() + 31 * lookup.lookupModes();260}261262/**263* Returns the string representation of this call site descriptor, of the264* format {@code name(parameterTypes)returnType@lookup}.265*/266@Override267public String toString() {268final String mt = methodType.toString();269final String l = getLookupPrivileged().toString();270final String o = operation.toString();271return o + mt + '@' + l;272}273274private void assertChangeInvariants(final CallSiteDescriptor changed, final String caller) {275alwaysAssert(changed != null, () -> caller + " must not return null.");276alwaysAssert(getClass() == changed.getClass(), () -> caller + " must not change the descriptor's class");277alwaysAssert(lookupsEqual(getLookupPrivileged(), changed.getLookupPrivileged()), () -> caller + " must not change the descriptor's lookup");278}279280private static void alwaysAssert(final boolean cond, final Supplier<String> errorMessage) {281if (!cond) {282throw new AssertionError(errorMessage.get());283}284}285}286287288