1 /* 2 * Copyright (c) 2008, 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 package java.lang.invoke; 27 28 // Android-changed: Not using Empty 29 //import sun.invoke.empty.Empty; 30 import static java.lang.invoke.MethodHandleStatics.*; 31 import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP; 32 33 /** 34 * A {@code CallSite} is a holder for a variable {@link MethodHandle}, 35 * which is called its {@code target}. 36 * An {@code invokedynamic} instruction linked to a {@code CallSite} delegates 37 * all calls to the site's current target. 38 * A {@code CallSite} may be associated with several {@code invokedynamic} 39 * instructions, or it may be "free floating", associated with none. 40 * In any case, it may be invoked through an associated method handle 41 * called its {@linkplain #dynamicInvoker dynamic invoker}. 42 * <p> 43 * {@code CallSite} is an abstract class which does not allow 44 * direct subclassing by users. It has three immediate, 45 * concrete subclasses that may be either instantiated or subclassed. 46 * <ul> 47 * <li>If a mutable target is not required, an {@code invokedynamic} instruction 48 * may be permanently bound by means of a {@linkplain ConstantCallSite constant call site}. 49 * <li>If a mutable target is required which has volatile variable semantics, 50 * because updates to the target must be immediately and reliably witnessed by other threads, 51 * a {@linkplain VolatileCallSite volatile call site} may be used. 52 * <li>Otherwise, if a mutable target is required, 53 * a {@linkplain MutableCallSite mutable call site} may be used. 54 * </ul> 55 * <p> 56 * A non-constant call site may be <em>relinked</em> by changing its target. 57 * The new target must have the same {@linkplain MethodHandle#type() type} 58 * as the previous target. 59 * Thus, though a call site can be relinked to a series of 60 * successive targets, it cannot change its type. 61 * <p> 62 * Here is a sample use of call sites and bootstrap methods which links every 63 * dynamic call site to print its arguments: 64 <blockquote><pre>{@code 65 static void test() throws Throwable { 66 // THE FOLLOWING LINE IS PSEUDOCODE FOR A JVM INSTRUCTION 67 InvokeDynamic[#bootstrapDynamic].baz("baz arg", 2, 3.14); 68 } 69 private static void printArgs(Object... args) { 70 System.out.println(java.util.Arrays.deepToString(args)); 71 } 72 private static final MethodHandle printArgs; 73 static { 74 MethodHandles.Lookup lookup = MethodHandles.lookup(); 75 Class thisClass = lookup.lookupClass(); // (who am I?) 76 printArgs = lookup.findStatic(thisClass, 77 "printArgs", MethodType.methodType(void.class, Object[].class)); 78 } 79 private static CallSite bootstrapDynamic(MethodHandles.Lookup caller, String name, MethodType type) { 80 // ignore caller and name, but match the type: 81 return new ConstantCallSite(printArgs.asType(type)); 82 } 83 }</pre></blockquote> 84 * @author John Rose, JSR 292 EG 85 */ 86 abstract 87 public class CallSite { 88 // Android-changed: not used. 89 // static { MethodHandleImpl.initStatics(); } 90 91 // The actual payload of this call site: 92 /*package-private*/ 93 MethodHandle target; // Note: This field is known to the JVM. Do not change. 94 95 /** 96 * Make a blank call site object with the given method type. 97 * An initial target method is supplied which will throw 98 * an {@link IllegalStateException} if called. 99 * <p> 100 * Before this {@code CallSite} object is returned from a bootstrap method, 101 * it is usually provided with a more useful target method, 102 * via a call to {@link CallSite#setTarget(MethodHandle) setTarget}. 103 * @throws NullPointerException if the proposed type is null 104 */ 105 /*package-private*/ CallSite(MethodType type)106 CallSite(MethodType type) { 107 // Android-changed: No cache for these so create uninitializedCallSite target here using 108 // method handle transformations to create a method handle that has the expected method 109 // type but throws an IllegalStateException. 110 // target = makeUninitializedCallSite(type); 111 this.target = MethodHandles.throwException(type.returnType(), IllegalStateException.class); 112 this.target = MethodHandles.insertArguments( 113 this.target, 0, new IllegalStateException("uninitialized call site")); 114 if (type.parameterCount() > 0) { 115 this.target = MethodHandles.dropArguments(this.target, 0, type.ptypes()); 116 } 117 118 // Android-changed: Using initializer method for GET_TARGET 119 // rather than complex static initializer. 120 initializeGetTarget(); 121 } 122 123 /** 124 * Make a call site object equipped with an initial target method handle. 125 * @param target the method handle which will be the initial target of the call site 126 * @throws NullPointerException if the proposed target is null 127 */ 128 /*package-private*/ CallSite(MethodHandle target)129 CallSite(MethodHandle target) { 130 target.type(); // null check 131 this.target = target; 132 133 // Android-changed: Using initializer method for GET_TARGET 134 // rather than complex static initializer. 135 initializeGetTarget(); 136 } 137 138 /** 139 * Make a call site object equipped with an initial target method handle. 140 * @param targetType the desired type of the call site 141 * @param createTargetHook a hook which will bind the call site to the target method handle 142 * @throws WrongMethodTypeException if the hook cannot be invoked on the required arguments, 143 * or if the target returned by the hook is not of the given {@code targetType} 144 * @throws NullPointerException if the hook returns a null value 145 * @throws ClassCastException if the hook returns something other than a {@code MethodHandle} 146 * @throws Throwable anything else thrown by the hook function 147 */ 148 /*package-private*/ CallSite(MethodType targetType, MethodHandle createTargetHook)149 CallSite(MethodType targetType, MethodHandle createTargetHook) throws Throwable { 150 this(targetType); 151 ConstantCallSite selfCCS = (ConstantCallSite) this; 152 MethodHandle boundTarget = (MethodHandle) createTargetHook.invokeWithArguments(selfCCS); 153 checkTargetChange(this.target, boundTarget); 154 this.target = boundTarget; 155 156 // Android-changed: Using initializer method for GET_TARGET 157 // rather than complex static initializer. 158 initializeGetTarget(); 159 } 160 161 /** 162 * Returns the type of this call site's target. 163 * Although targets may change, any call site's type is permanent, and can never change to an unequal type. 164 * The {@code setTarget} method enforces this invariant by refusing any new target that does 165 * not have the previous target's type. 166 * @return the type of the current target, which is also the type of any future target 167 */ type()168 public MethodType type() { 169 // warning: do not call getTarget here, because CCS.getTarget can throw IllegalStateException 170 return target.type(); 171 } 172 173 /** 174 * Returns the target method of the call site, according to the 175 * behavior defined by this call site's specific class. 176 * The immediate subclasses of {@code CallSite} document the 177 * class-specific behaviors of this method. 178 * 179 * @return the current linkage state of the call site, its target method handle 180 * @see ConstantCallSite 181 * @see VolatileCallSite 182 * @see #setTarget 183 * @see ConstantCallSite#getTarget 184 * @see MutableCallSite#getTarget 185 * @see VolatileCallSite#getTarget 186 */ getTarget()187 public abstract MethodHandle getTarget(); 188 189 /** 190 * Updates the target method of this call site, according to the 191 * behavior defined by this call site's specific class. 192 * The immediate subclasses of {@code CallSite} document the 193 * class-specific behaviors of this method. 194 * <p> 195 * The type of the new target must be {@linkplain MethodType#equals equal to} 196 * the type of the old target. 197 * 198 * @param newTarget the new target 199 * @throws NullPointerException if the proposed new target is null 200 * @throws WrongMethodTypeException if the proposed new target 201 * has a method type that differs from the previous target 202 * @see CallSite#getTarget 203 * @see ConstantCallSite#setTarget 204 * @see MutableCallSite#setTarget 205 * @see VolatileCallSite#setTarget 206 */ setTarget(MethodHandle newTarget)207 public abstract void setTarget(MethodHandle newTarget); 208 checkTargetChange(MethodHandle oldTarget, MethodHandle newTarget)209 void checkTargetChange(MethodHandle oldTarget, MethodHandle newTarget) { 210 MethodType oldType = oldTarget.type(); 211 MethodType newType = newTarget.type(); // null check! 212 if (!newType.equals(oldType)) 213 throw wrongTargetType(newTarget, oldType); 214 } 215 wrongTargetType(MethodHandle target, MethodType type)216 private static WrongMethodTypeException wrongTargetType(MethodHandle target, MethodType type) { 217 return new WrongMethodTypeException(String.valueOf(target)+" should be of type "+type); 218 } 219 220 /** 221 * Produces a method handle equivalent to an invokedynamic instruction 222 * which has been linked to this call site. 223 * <p> 224 * This method is equivalent to the following code: 225 * <blockquote><pre>{@code 226 * MethodHandle getTarget, invoker, result; 227 * getTarget = MethodHandles.publicLookup().bind(this, "getTarget", MethodType.methodType(MethodHandle.class)); 228 * invoker = MethodHandles.exactInvoker(this.type()); 229 * result = MethodHandles.foldArguments(invoker, getTarget) 230 * }</pre></blockquote> 231 * 232 * @return a method handle which always invokes this call site's current target 233 */ dynamicInvoker()234 public abstract MethodHandle dynamicInvoker(); 235 makeDynamicInvoker()236 /*non-public*/ MethodHandle makeDynamicInvoker() { 237 // Android-changed: Use bindTo() rather than bindArgumentL() (not implemented). 238 MethodHandle getTarget = GET_TARGET.bindTo(this); 239 MethodHandle invoker = MethodHandles.exactInvoker(this.type()); 240 return MethodHandles.foldArguments(invoker, getTarget); 241 } 242 243 // Android-changed: no longer final. GET_TARGET assigned in initializeGetTarget(). 244 private static MethodHandle GET_TARGET = null; 245 initializeGetTarget()246 private void initializeGetTarget() { 247 // Android-changed: moved from static initializer for 248 // GET_TARGET to avoid issues with running early. Called from 249 // constructors. CallSite creation is not performance critical. 250 synchronized (CallSite.class) { 251 if (GET_TARGET == null) { 252 try { 253 GET_TARGET = IMPL_LOOKUP. 254 findVirtual(CallSite.class, "getTarget", 255 MethodType.methodType(MethodHandle.class)); 256 } catch (ReflectiveOperationException e) { 257 throw new InternalError(e); 258 } 259 } 260 } 261 } 262 263 // Android-changed: not used. 264 // /** This guy is rolled into the default target if a MethodType is supplied to the constructor. */ 265 // /*package-private*/ 266 // static Empty uninitializedCallSite() { 267 // throw new IllegalStateException("uninitialized call site"); 268 // } 269 270 // unsafe stuff: 271 private static final long TARGET_OFFSET; 272 static { 273 try { 274 TARGET_OFFSET = UNSAFE.objectFieldOffset(CallSite.class.getDeclaredField("target")); 275 } catch (Exception ex) { throw new Error(ex); } 276 } 277 278 /*package-private*/ setTargetNormal(MethodHandle newTarget)279 void setTargetNormal(MethodHandle newTarget) { 280 // Android-changed: Set value directly. 281 // MethodHandleNatives.setCallSiteTargetNormal(this, newTarget); 282 target = newTarget; 283 } 284 /*package-private*/ getTargetVolatile()285 MethodHandle getTargetVolatile() { 286 return (MethodHandle) UNSAFE.getObjectVolatile(this, TARGET_OFFSET); 287 } 288 /*package-private*/ setTargetVolatile(MethodHandle newTarget)289 void setTargetVolatile(MethodHandle newTarget) { 290 // Android-changed: Set value directly. 291 // MethodHandleNatives.setCallSiteTargetVolatile(this, newTarget); 292 UNSAFE.putObjectVolatile(this, TARGET_OFFSET, newTarget); 293 } 294 295 // Android-changed: not used. 296 // this implements the upcall from the JVM, MethodHandleNatives.makeDynamicCallSite: 297 // static CallSite makeSite(MethodHandle bootstrapMethod, 298 // // Callee information: 299 // String name, MethodType type, 300 // // Extra arguments for BSM, if any: 301 // Object info, 302 // // Caller information: 303 // Class<?> callerClass) { 304 // MethodHandles.Lookup caller = IMPL_LOOKUP.in(callerClass); 305 // CallSite site; 306 // try { 307 // Object binding; 308 // info = maybeReBox(info); 309 // if (info == null) { 310 // binding = bootstrapMethod.invoke(caller, name, type); 311 // } else if (!info.getClass().isArray()) { 312 // binding = bootstrapMethod.invoke(caller, name, type, info); 313 // } else { 314 // Object[] argv = (Object[]) info; 315 // maybeReBoxElements(argv); 316 // switch (argv.length) { 317 // case 0: 318 // binding = bootstrapMethod.invoke(caller, name, type); 319 // break; 320 // case 1: 321 // binding = bootstrapMethod.invoke(caller, name, type, 322 // argv[0]); 323 // break; 324 // case 2: 325 // binding = bootstrapMethod.invoke(caller, name, type, 326 // argv[0], argv[1]); 327 // break; 328 // case 3: 329 // binding = bootstrapMethod.invoke(caller, name, type, 330 // argv[0], argv[1], argv[2]); 331 // break; 332 // case 4: 333 // binding = bootstrapMethod.invoke(caller, name, type, 334 // argv[0], argv[1], argv[2], argv[3]); 335 // break; 336 // case 5: 337 // binding = bootstrapMethod.invoke(caller, name, type, 338 // argv[0], argv[1], argv[2], argv[3], argv[4]); 339 // break; 340 // case 6: 341 // binding = bootstrapMethod.invoke(caller, name, type, 342 // argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]); 343 // break; 344 // default: 345 // final int NON_SPREAD_ARG_COUNT = 3; // (caller, name, type) 346 // if (NON_SPREAD_ARG_COUNT + argv.length > MethodType.MAX_MH_ARITY) 347 // throw new BootstrapMethodError("too many bootstrap method arguments"); 348 // MethodType bsmType = bootstrapMethod.type(); 349 // MethodType invocationType = MethodType.genericMethodType(NON_SPREAD_ARG_COUNT + argv.length); 350 // MethodHandle typedBSM = bootstrapMethod.asType(invocationType); 351 // MethodHandle spreader = invocationType.invokers().spreadInvoker(NON_SPREAD_ARG_COUNT); 352 // binding = spreader.invokeExact(typedBSM, (Object)caller, (Object)name, (Object)type, argv); 353 // } 354 // } 355 // //System.out.println("BSM for "+name+type+" => "+binding); 356 // if (binding instanceof CallSite) { 357 // site = (CallSite) binding; 358 // } else { 359 // throw new ClassCastException("bootstrap method failed to produce a CallSite"); 360 // } 361 // if (!site.getTarget().type().equals(type)) 362 // throw wrongTargetType(site.getTarget(), type); 363 // } catch (Throwable ex) { 364 // BootstrapMethodError bex; 365 // if (ex instanceof BootstrapMethodError) 366 // bex = (BootstrapMethodError) ex; 367 // else 368 // bex = new BootstrapMethodError("call site initialization exception", ex); 369 // throw bex; 370 // } 371 // return site; 372 // } 373 374 // private static Object maybeReBox(Object x) { 375 // if (x instanceof Integer) { 376 // int xi = (int) x; 377 // if (xi == (byte) xi) 378 // x = xi; // must rebox; see JLS 5.1.7 379 // } 380 // return x; 381 // } 382 // private static void maybeReBoxElements(Object[] xa) { 383 // for (int i = 0; i < xa.length; i++) { 384 // xa[i] = maybeReBox(xa[i]); 385 // } 386 // } 387 } 388