Bootstrap.java revision 1186:4a2dfd2ec3f3
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 26package jdk.nashorn.internal.runtime.linker; 27 28import static jdk.nashorn.internal.codegen.CompilerConstants.staticCallNoLookup; 29import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; 30 31import java.lang.invoke.CallSite; 32import java.lang.invoke.ConstantCallSite; 33import java.lang.invoke.MethodHandle; 34import java.lang.invoke.MethodHandles; 35import java.lang.invoke.MethodHandles.Lookup; 36import java.lang.invoke.MethodType; 37import jdk.internal.dynalink.CallSiteDescriptor; 38import jdk.internal.dynalink.DynamicLinker; 39import jdk.internal.dynalink.DynamicLinkerFactory; 40import jdk.internal.dynalink.GuardedInvocationFilter; 41import jdk.internal.dynalink.beans.BeansLinker; 42import jdk.internal.dynalink.beans.StaticClass; 43import jdk.internal.dynalink.linker.GuardedInvocation; 44import jdk.internal.dynalink.linker.LinkRequest; 45import jdk.internal.dynalink.linker.LinkerServices; 46import jdk.internal.dynalink.linker.MethodTypeConversionStrategy; 47import jdk.internal.dynalink.support.TypeUtilities; 48import jdk.nashorn.api.scripting.JSObject; 49import jdk.nashorn.internal.codegen.CompilerConstants.Call; 50import jdk.nashorn.internal.codegen.ObjectClassGenerator; 51import jdk.nashorn.internal.codegen.RuntimeCallSite; 52import jdk.nashorn.internal.lookup.MethodHandleFactory; 53import jdk.nashorn.internal.lookup.MethodHandleFunctionality; 54import jdk.nashorn.internal.objects.ScriptFunctionImpl; 55import jdk.nashorn.internal.runtime.ECMAException; 56import jdk.nashorn.internal.runtime.JSType; 57import jdk.nashorn.internal.runtime.OptimisticReturnFilters; 58import jdk.nashorn.internal.runtime.ScriptFunction; 59import jdk.nashorn.internal.runtime.ScriptRuntime; 60import jdk.nashorn.internal.runtime.options.Options; 61 62/** 63 * This class houses bootstrap method for invokedynamic instructions generated by compiler. 64 */ 65public final class Bootstrap { 66 /** Reference to the seed boostrap function */ 67 public static final Call BOOTSTRAP = staticCallNoLookup(Bootstrap.class, "bootstrap", CallSite.class, Lookup.class, String.class, MethodType.class, int.class); 68 69 private static final MethodHandleFunctionality MH = MethodHandleFactory.getFunctionality(); 70 71 private static final MethodHandle VOID_TO_OBJECT = MH.constant(Object.class, ScriptRuntime.UNDEFINED); 72 73 /** 74 * The default dynalink relink threshold for megamorphisism is 8. In the case 75 * of object fields only, it is fine. However, with dual fields, in order to get 76 * performance on benchmarks with a lot of object instantiation and then field 77 * reassignment, it can take slightly more relinks to become stable with type 78 * changes swapping out an entire proprety map and making a map guard fail. 79 * Therefore the relink threshold is set to 16 for dual fields (now the default). 80 * This doesn't seem to have any other negative performance implication. 81 * 82 * See for example octane.gbemu, run with --log=fields:warning to study 83 * megamorphic behavior 84 */ 85 private static final int NASHORN_DEFAULT_UNSTABLE_RELINK_THRESHOLD = 86 ObjectClassGenerator.OBJECT_FIELDS_ONLY ? 87 8 : 88 16; 89 90 // do not create me!! 91 private Bootstrap() { 92 } 93 94 private static final DynamicLinker dynamicLinker; 95 static { 96 final DynamicLinkerFactory factory = new DynamicLinkerFactory(); 97 final NashornBeansLinker nashornBeansLinker = new NashornBeansLinker(); 98 factory.setPrioritizedLinkers( 99 new NashornLinker(), 100 new NashornPrimitiveLinker(), 101 new NashornStaticClassLinker(), 102 new BoundCallableLinker(), 103 new JavaSuperAdapterLinker(), 104 new JSObjectLinker(nashornBeansLinker), 105 new BrowserJSObjectLinker(nashornBeansLinker), 106 new ReflectionCheckLinker()); 107 factory.setFallbackLinkers(nashornBeansLinker, new NashornBottomLinker()); 108 factory.setSyncOnRelink(true); 109 factory.setPrelinkFilter(new GuardedInvocationFilter() { 110 @Override 111 public GuardedInvocation filter(final GuardedInvocation inv, final LinkRequest request, final LinkerServices linkerServices) { 112 final CallSiteDescriptor desc = request.getCallSiteDescriptor(); 113 return OptimisticReturnFilters.filterOptimisticReturnValue(inv, desc).asType(linkerServices, desc.getMethodType()); 114 } 115 }); 116 factory.setAutoConversionStrategy(new MethodTypeConversionStrategy() { 117 @Override 118 public MethodHandle asType(final MethodHandle target, final MethodType newType) { 119 return unboxReturnType(target, newType); 120 } 121 }); 122 factory.setInternalObjectsFilter(NashornBeansLinker.createHiddenObjectFilter()); 123 final int relinkThreshold = Options.getIntProperty("nashorn.unstable.relink.threshold", NASHORN_DEFAULT_UNSTABLE_RELINK_THRESHOLD); 124 if (relinkThreshold > -1) { 125 factory.setUnstableRelinkThreshold(relinkThreshold); 126 } 127 128 // Linkers for any additional language runtimes deployed alongside Nashorn will be picked up by the factory. 129 factory.setClassLoader(Bootstrap.class.getClassLoader()); 130 131 dynamicLinker = factory.createLinker(); 132 } 133 134 /** 135 * Returns if the given object is a "callable" 136 * @param obj object to be checked for callability 137 * @return true if the obj is callable 138 */ 139 public static boolean isCallable(final Object obj) { 140 if (obj == ScriptRuntime.UNDEFINED || obj == null) { 141 return false; 142 } 143 144 return obj instanceof ScriptFunction || 145 isJSObjectFunction(obj) || 146 BeansLinker.isDynamicMethod(obj) || 147 obj instanceof BoundCallable || 148 isFunctionalInterfaceObject(obj) || 149 obj instanceof StaticClass; 150 } 151 152 /** 153 * Returns true if the given object is a strict callable 154 * @param callable the callable object to be checked for strictness 155 * @return true if the obj is a strict callable, false if it is a non-strict callable. 156 * @throws ECMAException with {@code TypeError} if the object is not a callable. 157 */ 158 public static boolean isStrictCallable(final Object callable) { 159 if (callable instanceof ScriptFunction) { 160 return ((ScriptFunction)callable).isStrict(); 161 } else if (isJSObjectFunction(callable)) { 162 return ((JSObject)callable).isStrictFunction(); 163 } else if (callable instanceof BoundCallable) { 164 return isStrictCallable(((BoundCallable)callable).getCallable()); 165 } else if (BeansLinker.isDynamicMethod(callable) || callable instanceof StaticClass) { 166 return false; 167 } 168 throw notFunction(callable); 169 } 170 171 private static ECMAException notFunction(final Object obj) { 172 return typeError("not.a.function", ScriptRuntime.safeToString(obj)); 173 } 174 175 private static boolean isJSObjectFunction(final Object obj) { 176 return obj instanceof JSObject && ((JSObject)obj).isFunction(); 177 } 178 179 /** 180 * Returns if the given object is a dynalink Dynamic method 181 * @param obj object to be checked 182 * @return true if the obj is a dynamic method 183 */ 184 public static boolean isDynamicMethod(final Object obj) { 185 return BeansLinker.isDynamicMethod(obj instanceof BoundCallable ? ((BoundCallable)obj).getCallable() : obj); 186 } 187 188 /** 189 * Returns if the given object is an instance of an interface annotated with 190 * java.lang.FunctionalInterface 191 * @param obj object to be checked 192 * @return true if the obj is an instance of @FunctionalInterface interface 193 */ 194 public static boolean isFunctionalInterfaceObject(final Object obj) { 195 return !JSType.isPrimitive(obj) && (NashornBeansLinker.getFunctionalInterfaceMethod(obj.getClass()) != null); 196 } 197 198 /** 199 * Create a call site and link it for Nashorn. This version of the method conforms to the invokedynamic bootstrap 200 * method expected signature and is referenced from Nashorn generated bytecode as the bootstrap method for all 201 * invokedynamic instructions. 202 * @param lookup MethodHandle lookup. Ignored as Nashorn only uses public lookup. 203 * @param opDesc Dynalink dynamic operation descriptor. 204 * @param type Method type. 205 * @param flags flags for call type, trace/profile etc. 206 * @return CallSite with MethodHandle to appropriate method or null if not found. 207 */ 208 public static CallSite bootstrap(final Lookup lookup, final String opDesc, final MethodType type, final int flags) { 209 return dynamicLinker.link(LinkerCallSite.newLinkerCallSite(lookup, opDesc, type, flags)); 210 } 211 212 /** 213 * Bootstrapper for a specialized Runtime call 214 * 215 * @param lookup lookup 216 * @param initialName initial name for callsite 217 * @param type method type for call site 218 * 219 * @return callsite for a runtime node 220 */ 221 public static CallSite runtimeBootstrap(final MethodHandles.Lookup lookup, final String initialName, final MethodType type) { 222 return new RuntimeCallSite(type, initialName); 223 } 224 225 /** 226 * Boostrapper for math calls that may overflow 227 * @param lookup lookup 228 * @param name name of operation 229 * @param type method type 230 * @param programPoint program point to bind to callsite 231 * 232 * @return callsite for a math instrinic node 233 */ 234 public static CallSite mathBootstrap(final MethodHandles.Lookup lookup, final String name, final MethodType type, final int programPoint) { 235 final MethodHandle mh; 236 switch (name) { 237 case "iadd": 238 mh = JSType.ADD_EXACT.methodHandle(); 239 break; 240 case "isub": 241 mh = JSType.SUB_EXACT.methodHandle(); 242 break; 243 case "imul": 244 mh = JSType.MUL_EXACT.methodHandle(); 245 break; 246 case "idiv": 247 mh = JSType.DIV_EXACT.methodHandle(); 248 break; 249 case "irem": 250 mh = JSType.REM_EXACT.methodHandle(); 251 break; 252 case "ineg": 253 mh = JSType.NEGATE_EXACT.methodHandle(); 254 break; 255 case "ladd": 256 mh = JSType.ADD_EXACT_LONG.methodHandle(); 257 break; 258 case "lsub": 259 mh = JSType.SUB_EXACT_LONG.methodHandle(); 260 break; 261 case "lmul": 262 mh = JSType.MUL_EXACT_LONG.methodHandle(); 263 break; 264 case "ldiv": 265 mh = JSType.DIV_EXACT_LONG.methodHandle(); 266 break; 267 case "lrem": 268 mh = JSType.REM_EXACT_LONG.methodHandle(); 269 break; 270 case "lneg": 271 mh = JSType.NEGATE_EXACT_LONG.methodHandle(); 272 break; 273 default: 274 throw new AssertionError("unsupported math intrinsic"); 275 } 276 return new ConstantCallSite(MH.insertArguments(mh, mh.type().parameterCount() - 1, programPoint)); 277 } 278 279 /** 280 * Returns a dynamic invoker for a specified dynamic operation using the public lookup. You can use this method to 281 * create a method handle that when invoked acts completely as if it were a Nashorn-linked call site. An overview of 282 * available dynamic operations can be found in the 283 * <a href="https://github.com/szegedi/dynalink/wiki/User-Guide-0.6">Dynalink User Guide</a>, but we'll show few 284 * examples here: 285 * <ul> 286 * <li>Get a named property with fixed name: 287 * <pre> 288 * MethodHandle getColor = Boostrap.createDynamicInvoker("dyn:getProp:color", Object.class, Object.class); 289 * Object obj = ...; // somehow obtain the object 290 * Object color = getColor.invokeExact(obj); 291 * </pre> 292 * </li> 293 * <li>Get a named property with variable name: 294 * <pre> 295 * MethodHandle getProperty = Boostrap.createDynamicInvoker("dyn:getElem", Object.class, Object.class, String.class); 296 * Object obj = ...; // somehow obtain the object 297 * Object color = getProperty.invokeExact(obj, "color"); 298 * Object shape = getProperty.invokeExact(obj, "shape"); 299 * MethodHandle getNumProperty = Boostrap.createDynamicInvoker("dyn:getElem", Object.class, Object.class, int.class); 300 * Object elem42 = getNumProperty.invokeExact(obj, 42); 301 * </pre> 302 * </li> 303 * <li>Set a named property with fixed name: 304 * <pre> 305 * MethodHandle setColor = Boostrap.createDynamicInvoker("dyn:setProp:color", void.class, Object.class, Object.class); 306 * Object obj = ...; // somehow obtain the object 307 * setColor.invokeExact(obj, Color.BLUE); 308 * </pre> 309 * </li> 310 * <li>Set a property with variable name: 311 * <pre> 312 * MethodHandle setProperty = Boostrap.createDynamicInvoker("dyn:setElem", void.class, Object.class, String.class, Object.class); 313 * Object obj = ...; // somehow obtain the object 314 * setProperty.invokeExact(obj, "color", Color.BLUE); 315 * setProperty.invokeExact(obj, "shape", Shape.CIRCLE); 316 * </pre> 317 * </li> 318 * <li>Call a function on an object; two-step variant. This is the actual variant used by Nashorn-generated code: 319 * <pre> 320 * MethodHandle findFooFunction = Boostrap.createDynamicInvoker("dyn:getMethod:foo", Object.class, Object.class); 321 * Object obj = ...; // somehow obtain the object 322 * Object foo_fn = findFooFunction.invokeExact(obj); 323 * MethodHandle callFunctionWithTwoArgs = Boostrap.createDynamicInvoker("dyn:call", Object.class, Object.class, Object.class, Object.class, Object.class); 324 * // Note: "call" operation takes a function, then a "this" value, then the arguments: 325 * Object foo_retval = callFunctionWithTwoArgs.invokeExact(foo_fn, obj, arg1, arg2); 326 * </pre> 327 * </li> 328 * <li>Call a function on an object; single-step variant. Although Nashorn doesn't use this variant and never 329 * emits any INVOKEDYNAMIC instructions with {@code dyn:getMethod}, it still supports this standard Dynalink 330 * operation: 331 * <pre> 332 * MethodHandle callFunctionFooWithTwoArgs = Boostrap.createDynamicInvoker("dyn:callMethod:foo", Object.class, Object.class, Object.class, Object.class); 333 * Object obj = ...; // somehow obtain the object 334 * Object foo_retval = callFunctionFooWithTwoArgs.invokeExact(obj, arg1, arg2); 335 * </pre> 336 * </li> 337 * </ul> 338 * Few additional remarks: 339 * <ul> 340 * <li>Just as Nashorn works with any Java object, the invokers returned from this method can also be applied to 341 * arbitrary Java objects in addition to Nashorn JavaScript objects.</li> 342 * <li>For invoking a named function on an object, you can also use the {@link InvokeByName} convenience class.</li> 343 * <li>For Nashorn objects {@code getElem}, {@code getProp}, and {@code getMethod} are handled almost identically, 344 * since JavaScript doesn't distinguish between different kinds of properties on an object. Either can be used with 345 * fixed property name or a variable property name. The only significant difference is handling of missing 346 * properties: {@code getMethod} for a missing member will link to a potential invocation of 347 * {@code __noSuchMethod__} on the object, {@code getProp} for a missing member will link to a potential invocation 348 * of {@code __noSuchProperty__}, while {@code getElem} for a missing member will link to an empty getter.</li> 349 * <li>In similar vein, {@code setElem} and {@code setProp} are handled identically on Nashorn objects.</li> 350 * <li>There's no rule that the variable property identifier has to be a {@code String} for {@code getProp/setProp} 351 * and {@code int} for {@code getElem/setElem}. You can declare their type to be {@code int}, {@code double}, 352 * {@code Object}, and so on regardless of the kind of the operation.</li> 353 * <li>You can be as specific in parameter types as you want. E.g. if you know that the receiver of the operation 354 * will always be {@code ScriptObject}, you can pass {@code ScriptObject.class} as its parameter type. If you happen 355 * to link to a method that expects different types, (you can use these invokers on POJOs too, after all, and end up 356 * linking with their methods that have strongly-typed signatures), all necessary conversions allowed by either Java 357 * or JavaScript will be applied: if invoked methods specify either primitive or wrapped Java numeric types, or 358 * {@code String} or {@code boolean/Boolean}, then the parameters might be subjected to standard ECMAScript 359 * {@code ToNumber}, {@code ToString}, and {@code ToBoolean} conversion, respectively. Less obviously, if the 360 * expected parameter type is a SAM type, and you pass a JavaScript function, a proxy object implementing the SAM 361 * type and delegating to the function will be passed. Linkage can often be optimized when linkers have more 362 * specific type information than "everything can be an object".</li> 363 * <li>You can also be as specific in return types as you want. For return types any necessary type conversion 364 * available in either Java or JavaScript will be automatically applied, similar to the process described for 365 * parameters, only in reverse direction: if you specify any either primitive or wrapped Java numeric type, or 366 * {@code String} or {@code boolean/Boolean}, then the return values will be subjected to standard ECMAScript 367 * {@code ToNumber}, {@code ToString}, and {@code ToBoolean} conversion, respectively. Less obviously, if the return 368 * type is a SAM type, and the return value is a JavaScript function, a proxy object implementing the SAM type and 369 * delegating to the function will be returned.</li> 370 * </ul> 371 * @param opDesc Dynalink dynamic operation descriptor. 372 * @param rtype the return type for the operation 373 * @param ptypes the parameter types for the operation 374 * @return MethodHandle for invoking the operation. 375 */ 376 public static MethodHandle createDynamicInvoker(final String opDesc, final Class<?> rtype, final Class<?>... ptypes) { 377 return createDynamicInvoker(opDesc, MethodType.methodType(rtype, ptypes)); 378 } 379 380 /** 381 * Returns a dynamic invoker for a specified dynamic operation using the public lookup. Similar to 382 * {@link #createDynamicInvoker(String, Class, Class...)} but with an additional parameter to 383 * set the call site flags of the dynamic invoker. 384 * @param opDesc Dynalink dynamic operation descriptor. 385 * @param flags the call site flags for the operation 386 * @param rtype the return type for the operation 387 * @param ptypes the parameter types for the operation 388 * @return MethodHandle for invoking the operation. 389 */ 390 public static MethodHandle createDynamicInvoker(final String opDesc, final int flags, final Class<?> rtype, final Class<?>... ptypes) { 391 return bootstrap(MethodHandles.publicLookup(), opDesc, MethodType.methodType(rtype, ptypes), flags).dynamicInvoker(); 392 } 393 394 /** 395 * Returns a dynamic invoker for a specified dynamic operation using the public lookup. Similar to 396 * {@link #createDynamicInvoker(String, Class, Class...)} but with return and parameter types composed into a 397 * method type in the signature. See the discussion of that method for details. 398 * @param opDesc Dynalink dynamic operation descriptor. 399 * @param type the method type for the operation 400 * @return MethodHandle for invoking the operation. 401 */ 402 public static MethodHandle createDynamicInvoker(final String opDesc, final MethodType type) { 403 return bootstrap(MethodHandles.publicLookup(), opDesc, type, 0).dynamicInvoker(); 404 } 405 406 /** 407 * Binds any object Nashorn can use as a [[Callable]] to a receiver and optionally arguments. 408 * @param callable the callable to bind 409 * @param boundThis the bound "this" value. 410 * @param boundArgs the bound arguments. Can be either null or empty array to signify no arguments are bound. 411 * @return a bound callable. 412 * @throws ECMAException with {@code TypeError} if the object is not a callable. 413 */ 414 public static Object bindCallable(final Object callable, final Object boundThis, final Object[] boundArgs) { 415 if (callable instanceof ScriptFunctionImpl) { 416 return ((ScriptFunctionImpl)callable).makeBoundFunction(boundThis, boundArgs); 417 } else if (callable instanceof BoundCallable) { 418 return ((BoundCallable)callable).bind(boundArgs); 419 } else if (isCallable(callable)) { 420 return new BoundCallable(callable, boundThis, boundArgs); 421 } 422 throw notFunction(callable); 423 } 424 425 /** 426 * Creates a super-adapter for an adapter, that is, an adapter to the adapter that allows invocation of superclass 427 * methods on it. 428 * @param adapter the original adapter 429 * @return a new adapter that can be used to invoke super methods on the original adapter. 430 */ 431 public static Object createSuperAdapter(final Object adapter) { 432 return new JavaSuperAdapter(adapter); 433 } 434 435 /** 436 * If the given class is a reflection-specific class (anything in {@code java.lang.reflect} and 437 * {@code java.lang.invoke} package, as well a {@link Class} and any subclass of {@link ClassLoader}) and there is 438 * a security manager in the system, then it checks the {@code nashorn.JavaReflection} {@code RuntimePermission}. 439 * @param clazz the class being tested 440 * @param isStatic is access checked for static members (or instance members) 441 */ 442 public static void checkReflectionAccess(final Class<?> clazz, final boolean isStatic) { 443 ReflectionCheckLinker.checkReflectionAccess(clazz, isStatic); 444 } 445 446 /** 447 * Returns the Nashorn's internally used dynamic linker's services object. Note that in code that is processing a 448 * linking request, you will normally use the {@code LinkerServices} object passed by whatever top-level linker 449 * invoked the linking (if the call site is in Nashorn-generated code, you'll get this object anyway). You should 450 * only resort to retrieving a linker services object using this method when you need some linker services (e.g. 451 * type converter method handles) outside of a code path that is linking a call site. 452 * @return Nashorn's internal dynamic linker's services object. 453 */ 454 public static LinkerServices getLinkerServices() { 455 return dynamicLinker.getLinkerServices(); 456 } 457 458 /** 459 * Takes a guarded invocation, and ensures its method and guard conform to the type of the call descriptor, using 460 * all type conversions allowed by the linker's services. This method is used by Nashorn's linkers as a last step 461 * before returning guarded invocations. Most of the code used to produce the guarded invocations does not make an 462 * effort to coordinate types of the methods, and so a final type adjustment before a guarded invocation is returned 463 * to the aggregating linker is the responsibility of the linkers themselves. 464 * @param inv the guarded invocation that needs to be type-converted. Can be null. 465 * @param linkerServices the linker services object providing the type conversions. 466 * @param desc the call site descriptor to whose method type the invocation needs to conform. 467 * @return the type-converted guarded invocation. If input is null, null is returned. If the input invocation 468 * already conforms to the requested type, it is returned unchanged. 469 */ 470 static GuardedInvocation asTypeSafeReturn(final GuardedInvocation inv, final LinkerServices linkerServices, final CallSiteDescriptor desc) { 471 return inv == null ? null : inv.asTypeSafeReturn(linkerServices, desc.getMethodType()); 472 } 473 474 /** 475 * Adapts the return type of the method handle with {@code explicitCastArguments} when it is an unboxing 476 * conversion. This will ensure that nulls are unwrapped to false or 0. 477 * @param target the target method handle 478 * @param newType the desired new type. Note that this method does not adapt the method handle completely to the 479 * new type, it only adapts the return type; this is allowed as per 480 * {@link DynamicLinkerFactory#setAutoConversionStrategy(MethodTypeConversionStrategy)}, which is what this method 481 * is used for. 482 * @return the method handle with adapted return type, if it required an unboxing conversion. 483 */ 484 private static MethodHandle unboxReturnType(final MethodHandle target, final MethodType newType) { 485 final MethodType targetType = target.type(); 486 final Class<?> oldReturnType = targetType.returnType(); 487 final Class<?> newReturnType = newType.returnType(); 488 if (TypeUtilities.isWrapperType(oldReturnType)) { 489 if (newReturnType.isPrimitive()) { 490 // The contract of setAutoConversionStrategy is such that the difference between newType and targetType 491 // can only be JLS method invocation conversions. 492 assert TypeUtilities.isMethodInvocationConvertible(oldReturnType, newReturnType); 493 return MethodHandles.explicitCastArguments(target, targetType.changeReturnType(newReturnType)); 494 } 495 } else if (oldReturnType == void.class && newReturnType == Object.class) { 496 return MethodHandles.filterReturnValue(target, VOID_TO_OBJECT); 497 } 498 return target; 499 } 500} 501