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