CallSiteDescriptor.java revision 1645:15d52fdd9168
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 84package jdk.dynalink; 85 86import java.lang.invoke.MethodHandles.Lookup; 87import java.lang.invoke.MethodType; 88import java.util.Objects; 89 90/** 91 * Call site descriptors contain all the information necessary for linking a 92 * call site. This information is normally passed as parameters to bootstrap 93 * methods and consists of the {@code MethodHandles.Lookup} object on the caller 94 * class in which the call site occurs, the dynamic operation at the call 95 * site, and the method type of the call site. {@code CallSiteDescriptor} 96 * objects are used in Dynalink to capture and store these parameters for 97 * subsequent use by the {@link DynamicLinker}. 98 * <p> 99 * The constructors of built-in {@link RelinkableCallSite} implementations all 100 * take a call site descriptor. 101 * <p> 102 * Call site descriptors must be immutable. You can use this class as-is or you 103 * can subclass it, especially if you need to add further information to the 104 * descriptors (typically, values passed in additional parameters to the 105 * bootstrap method. Since the descriptors must be immutable, you can set up a 106 * cache for equivalent descriptors to have the call sites share them. 107 * <p> 108 * The class extends {@link SecureLookupSupplier} for security-checked access to 109 * the {@code MethodHandles.Lookup} object it carries. This lookup should be used 110 * to find method handles to set as targets of the call site described by this 111 * descriptor. 112 */ 113public class CallSiteDescriptor extends SecureLookupSupplier { 114 private final Operation operation; 115 private final MethodType methodType; 116 117 /** 118 * Creates a new call site descriptor. 119 * @param lookup the lookup object describing the class the call site belongs to. 120 * When creating descriptors from a {@link java.lang.invoke} bootstrap method, 121 * it should be the lookup passed to the bootstrap. 122 * @param operation the dynamic operation at the call site. 123 * @param methodType the method type of the call site. When creating 124 * descriptors from a {@link java.lang.invoke} bootstrap method, it should be 125 * the method type passed to the bootstrap. 126 */ 127 public CallSiteDescriptor(final Lookup lookup, final Operation operation, final MethodType methodType) { 128 super(lookup); 129 this.operation = Objects.requireNonNull(operation, "name"); 130 this.methodType = Objects.requireNonNull(methodType, "methodType"); 131 } 132 133 /** 134 * Returns the operation at the call site. 135 * @return the operation at the call site. 136 */ 137 public final Operation getOperation() { 138 return operation; 139 } 140 141 /** 142 * The type of the method at the call site. 143 * 144 * @return type of the method at the call site. 145 */ 146 public final MethodType getMethodType() { 147 return methodType; 148 } 149 150 /** 151 * Creates a new call site descriptor from this descriptor, which is 152 * identical to this, except it changes the method type. Invokes 153 * {@link #changeMethodTypeInternal(MethodType)} and checks that it returns 154 * a descriptor of the same class as this descriptor. 155 * 156 * @param newMethodType the new method type 157 * @return a new call site descriptor, with the method type changed. 158 * @throws RuntimeException if {@link #changeMethodTypeInternal(MethodType)} 159 * returned a descriptor of different class than this object. 160 * @throws NullPointerException if {@link #changeMethodTypeInternal(MethodType)} 161 * returned null. 162 */ 163 public final CallSiteDescriptor changeMethodType(final MethodType newMethodType) { 164 final CallSiteDescriptor changed = Objects.requireNonNull( 165 changeMethodTypeInternal(newMethodType), 166 "changeMethodTypeInternal() must not return null."); 167 168 if (getClass() != changed.getClass()) { 169 throw new RuntimeException( 170 "changeMethodTypeInternal() must return an object of the same class it is invoked on."); 171 } 172 173 return changed; 174 } 175 176 /** 177 * Creates a new call site descriptor from this descriptor, which is 178 * identical to this, except it changes the method type. Subclasses must 179 * override this method to return an object of their exact class. 180 * 181 * @param newMethodType the new method type 182 * @return a new call site descriptor, with the method type changed. 183 */ 184 protected CallSiteDescriptor changeMethodTypeInternal(final MethodType newMethodType) { 185 return new CallSiteDescriptor(getLookupPrivileged(), operation, newMethodType); 186 } 187 188 /** 189 * Returns true if this call site descriptor is equal to the passed object. 190 * It is considered equal if the other object is of the exact same class, 191 * their operations and method types are equal, and their lookups have the 192 * same {@link java.lang.invoke.MethodHandles.Lookup#lookupClass()} and 193 * {@link java.lang.invoke.MethodHandles.Lookup#lookupModes()}. 194 */ 195 @Override 196 public boolean equals(final Object obj) { 197 if (obj == this) { 198 return true; 199 } else if (obj == null) { 200 return false; 201 } else if (obj.getClass() != getClass()) { 202 return false; 203 } 204 final CallSiteDescriptor other = (CallSiteDescriptor)obj; 205 return operation.equals(other.operation) && 206 methodType.equals(other.methodType) && 207 lookupsEqual(getLookupPrivileged(), other.getLookupPrivileged()); 208 } 209 210 /** 211 * Compares two lookup objects for value-based equality. They are considered 212 * equal if they have the same 213 * {@link java.lang.invoke.MethodHandles.Lookup#lookupClass()} and 214 * {@link java.lang.invoke.MethodHandles.Lookup#lookupModes()}. 215 * @param l1 first lookup 216 * @param l2 second lookup 217 * @return true if the two lookups are equal, false otherwise. 218 */ 219 private static boolean lookupsEqual(final Lookup l1, final Lookup l2) { 220 return l1.lookupClass() == l2.lookupClass() && l1.lookupModes() == l2.lookupModes(); 221 } 222 223 /** 224 * Returns a value-based hash code of this call site descriptor computed 225 * from its operation, method type, and lookup object's lookup class and 226 * lookup modes. 227 * @return value-based hash code for this call site descriptor. 228 */ 229 @Override 230 public int hashCode() { 231 return operation.hashCode() + 31 * methodType.hashCode() + 31 * 31 * lookupHashCode(getLookupPrivileged()); 232 } 233 234 /** 235 * Returns a value-based hash code for the passed lookup object. It is 236 * based on the lookup object's 237 * {@link java.lang.invoke.MethodHandles.Lookup#lookupClass()} and 238 * {@link java.lang.invoke.MethodHandles.Lookup#lookupModes()} values. 239 * @param lookup the lookup object. 240 * @return a hash code for the object.. 241 */ 242 private static int lookupHashCode(final Lookup lookup) { 243 return lookup.lookupClass().hashCode() + 31 * lookup.lookupModes(); 244 } 245 246 /** 247 * Returns the string representation of this call site descriptor, of the 248 * format {@code name(parameterTypes)returnType@lookup}. 249 */ 250 @Override 251 public String toString() { 252 final String mt = methodType.toString(); 253 final String l = getLookupPrivileged().toString(); 254 final String o = operation.toString(); 255 final StringBuilder b = new StringBuilder(o.length() + mt.length() + 1 + l.length()); 256 return b.append(o).append(mt).append('@').append(l).toString(); 257 } 258} 259