1 /*
   2  * Copyright (c) 2014, 2018, 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 import java.lang.reflect.Field;
  29 import java.lang.reflect.Modifier;
  30 
  31 import static java.lang.invoke.MethodHandleStatics.UNSAFE;
  32 
  33 final class VarHandles {
  34 
  35     static VarHandle makeFieldHandle(MemberName f, Class<?> refc, Class<?> type, boolean isWriteAllowedOnFinalFields) {
  36         if (!f.isStatic()) {
  37             long foffset = MethodHandleNatives.objectFieldOffset(f);
  38             if (!type.isPrimitive()) {
  39                 if (f.isFlattened()) {
  40                     return f.isFinal() && !isWriteAllowedOnFinalFields
  41                         ? new VarHandleReferences.FlatValueFieldInstanceReadOnly(refc, foffset, type)
  42                         : new VarHandleReferences.FlatValueFieldInstanceReadWrite(refc, foffset, type);
  43                 } else {
  44                     return f.isFinal() && !isWriteAllowedOnFinalFields
  45                        ? new VarHandleReferences.FieldInstanceReadOnly(refc, foffset, type)
  46                        : new VarHandleReferences.FieldInstanceReadWrite(refc, foffset, type, f.isInlineableField());
  47                 }
  48             }
  49             else if (type == boolean.class) {
  50                 return f.isFinal() && !isWriteAllowedOnFinalFields
  51                        ? new VarHandleBooleans.FieldInstanceReadOnly(refc, foffset)
  52                        : new VarHandleBooleans.FieldInstanceReadWrite(refc, foffset);
  53             }
  54             else if (type == byte.class) {
  55                 return f.isFinal() && !isWriteAllowedOnFinalFields
  56                        ? new VarHandleBytes.FieldInstanceReadOnly(refc, foffset)
  57                        : new VarHandleBytes.FieldInstanceReadWrite(refc, foffset);
  58             }
  59             else if (type == short.class) {
  60                 return f.isFinal() && !isWriteAllowedOnFinalFields
  61                        ? new VarHandleShorts.FieldInstanceReadOnly(refc, foffset)
  62                        : new VarHandleShorts.FieldInstanceReadWrite(refc, foffset);
  63             }
  64             else if (type == char.class) {
  65                 return f.isFinal() && !isWriteAllowedOnFinalFields
  66                        ? new VarHandleChars.FieldInstanceReadOnly(refc, foffset)
  67                        : new VarHandleChars.FieldInstanceReadWrite(refc, foffset);
  68             }
  69             else if (type == int.class) {
  70                 return f.isFinal() && !isWriteAllowedOnFinalFields
  71                        ? new VarHandleInts.FieldInstanceReadOnly(refc, foffset)
  72                        : new VarHandleInts.FieldInstanceReadWrite(refc, foffset);
  73             }
  74             else if (type == long.class) {
  75                 return f.isFinal() && !isWriteAllowedOnFinalFields
  76                        ? new VarHandleLongs.FieldInstanceReadOnly(refc, foffset)
  77                        : new VarHandleLongs.FieldInstanceReadWrite(refc, foffset);
  78             }
  79             else if (type == float.class) {
  80                 return f.isFinal() && !isWriteAllowedOnFinalFields
  81                        ? new VarHandleFloats.FieldInstanceReadOnly(refc, foffset)
  82                        : new VarHandleFloats.FieldInstanceReadWrite(refc, foffset);
  83             }
  84             else if (type == double.class) {
  85                 return f.isFinal() && !isWriteAllowedOnFinalFields
  86                        ? new VarHandleDoubles.FieldInstanceReadOnly(refc, foffset)
  87                        : new VarHandleDoubles.FieldInstanceReadWrite(refc, foffset);
  88             }
  89             else {
  90                 throw new UnsupportedOperationException();
  91             }
  92         }
  93         else {
  94             // TODO This is not lazy on first invocation
  95             // and might cause some circular initialization issues
  96 
  97             // Replace with something similar to direct method handles
  98             // where a barrier is used then elided after use
  99 
 100             if (UNSAFE.shouldBeInitialized(refc))
 101                 UNSAFE.ensureClassInitialized(refc);
 102 
 103             Object base = MethodHandleNatives.staticFieldBase(f);
 104             long foffset = MethodHandleNatives.staticFieldOffset(f);
 105             if (!type.isPrimitive()) {
 106                 assert(!f.isFlattened());   // static field is not flattened
 107                 return f.isFinal() && !isWriteAllowedOnFinalFields
 108                        ? new VarHandleReferences.FieldStaticReadOnly(base, foffset, type)
 109                        : new VarHandleReferences.FieldStaticReadWrite(base, foffset, type, f.isInlineableField());
 110             }
 111             else if (type == boolean.class) {
 112                 return f.isFinal() && !isWriteAllowedOnFinalFields
 113                        ? new VarHandleBooleans.FieldStaticReadOnly(base, foffset)
 114                        : new VarHandleBooleans.FieldStaticReadWrite(base, foffset);
 115             }
 116             else if (type == byte.class) {
 117                 return f.isFinal() && !isWriteAllowedOnFinalFields
 118                        ? new VarHandleBytes.FieldStaticReadOnly(base, foffset)
 119                        : new VarHandleBytes.FieldStaticReadWrite(base, foffset);
 120             }
 121             else if (type == short.class) {
 122                 return f.isFinal() && !isWriteAllowedOnFinalFields
 123                        ? new VarHandleShorts.FieldStaticReadOnly(base, foffset)
 124                        : new VarHandleShorts.FieldStaticReadWrite(base, foffset);
 125             }
 126             else if (type == char.class) {
 127                 return f.isFinal() && !isWriteAllowedOnFinalFields
 128                        ? new VarHandleChars.FieldStaticReadOnly(base, foffset)
 129                        : new VarHandleChars.FieldStaticReadWrite(base, foffset);
 130             }
 131             else if (type == int.class) {
 132                 return f.isFinal() && !isWriteAllowedOnFinalFields
 133                        ? new VarHandleInts.FieldStaticReadOnly(base, foffset)
 134                        : new VarHandleInts.FieldStaticReadWrite(base, foffset);
 135             }
 136             else if (type == long.class) {
 137                 return f.isFinal() && !isWriteAllowedOnFinalFields
 138                        ? new VarHandleLongs.FieldStaticReadOnly(base, foffset)
 139                        : new VarHandleLongs.FieldStaticReadWrite(base, foffset);
 140             }
 141             else if (type == float.class) {
 142                 return f.isFinal() && !isWriteAllowedOnFinalFields
 143                        ? new VarHandleFloats.FieldStaticReadOnly(base, foffset)
 144                        : new VarHandleFloats.FieldStaticReadWrite(base, foffset);
 145             }
 146             else if (type == double.class) {
 147                 return f.isFinal() && !isWriteAllowedOnFinalFields
 148                        ? new VarHandleDoubles.FieldStaticReadOnly(base, foffset)
 149                        : new VarHandleDoubles.FieldStaticReadWrite(base, foffset);
 150             }
 151             else {
 152                 throw new UnsupportedOperationException();
 153             }
 154         }
 155     }
 156 
 157     // Required by instance field handles
 158     static Field getFieldFromReceiverAndOffset(Class<?> receiverType,
 159                                                long offset,
 160                                                Class<?> fieldType) {
 161         for (Field f : receiverType.getDeclaredFields()) {
 162             if (Modifier.isStatic(f.getModifiers())) continue;
 163 
 164             if (offset == UNSAFE.objectFieldOffset(f)) {
 165                 assert f.getType() == fieldType;
 166                 return f;
 167             }
 168         }
 169         throw new InternalError("Field not found at offset");
 170     }
 171 
 172     // Required by instance static field handles
 173     static Field getStaticFieldFromBaseAndOffset(Object base,
 174                                                  long offset,
 175                                                  Class<?> fieldType) {
 176         // @@@ This is a little fragile assuming the base is the class
 177         Class<?> receiverType = (Class<?>) base;
 178         for (Field f : receiverType.getDeclaredFields()) {
 179             if (!Modifier.isStatic(f.getModifiers())) continue;
 180 
 181             if (offset == UNSAFE.staticFieldOffset(f)) {
 182                 assert f.getType() == fieldType;
 183                 return f;
 184             }
 185         }
 186         throw new InternalError("Static field not found at offset");
 187     }
 188 
 189     static VarHandle makeArrayElementHandle(Class<?> arrayClass) {
 190         if (!arrayClass.isArray())
 191             throw new IllegalArgumentException("not an array: " + arrayClass);
 192 
 193         Class<?> componentType = arrayClass.getComponentType();
 194 
 195         int aoffset = UNSAFE.arrayBaseOffset(arrayClass);
 196         int ascale = UNSAFE.arrayIndexScale(arrayClass);
 197         int ashift = 31 - Integer.numberOfLeadingZeros(ascale);
 198 
 199         if (!componentType.isPrimitive()) {
 200             // the redundant componentType.isValue() check is there to
 201             // minimize the performance impact to non-value array.
 202             // It should be removed when Unsafe::isFlattenedArray is intrinsified.
 203             return componentType.isInlineClass() && UNSAFE.isFlattenedArray(arrayClass)
 204                 ? new VarHandleReferences.ValueArray(aoffset, ashift, arrayClass)
 205                 : new VarHandleReferences.Array(aoffset, ashift, arrayClass);
 206         }
 207         else if (componentType == boolean.class) {
 208             return new VarHandleBooleans.Array(aoffset, ashift);
 209         }
 210         else if (componentType == byte.class) {
 211             return new VarHandleBytes.Array(aoffset, ashift);
 212         }
 213         else if (componentType == short.class) {
 214             return new VarHandleShorts.Array(aoffset, ashift);
 215         }
 216         else if (componentType == char.class) {
 217             return new VarHandleChars.Array(aoffset, ashift);
 218         }
 219         else if (componentType == int.class) {
 220             return new VarHandleInts.Array(aoffset, ashift);
 221         }
 222         else if (componentType == long.class) {
 223             return new VarHandleLongs.Array(aoffset, ashift);
 224         }
 225         else if (componentType == float.class) {
 226             return new VarHandleFloats.Array(aoffset, ashift);
 227         }
 228         else if (componentType == double.class) {
 229             return new VarHandleDoubles.Array(aoffset, ashift);
 230         }
 231         else {
 232             throw new UnsupportedOperationException();
 233         }
 234     }
 235 
 236     static VarHandle byteArrayViewHandle(Class<?> viewArrayClass,
 237                                          boolean be) {
 238         if (!viewArrayClass.isArray())
 239             throw new IllegalArgumentException("not an array: " + viewArrayClass);
 240 
 241         Class<?> viewComponentType = viewArrayClass.getComponentType();
 242 
 243         if (viewComponentType == long.class) {
 244             return new VarHandleByteArrayAsLongs.ArrayHandle(be);
 245         }
 246         else if (viewComponentType == int.class) {
 247             return new VarHandleByteArrayAsInts.ArrayHandle(be);
 248         }
 249         else if (viewComponentType == short.class) {
 250             return new VarHandleByteArrayAsShorts.ArrayHandle(be);
 251         }
 252         else if (viewComponentType == char.class) {
 253             return new VarHandleByteArrayAsChars.ArrayHandle(be);
 254         }
 255         else if (viewComponentType == double.class) {
 256             return new VarHandleByteArrayAsDoubles.ArrayHandle(be);
 257         }
 258         else if (viewComponentType == float.class) {
 259             return new VarHandleByteArrayAsFloats.ArrayHandle(be);
 260         }
 261 
 262         throw new UnsupportedOperationException();
 263     }
 264 
 265     static VarHandle makeByteBufferViewHandle(Class<?> viewArrayClass,
 266                                               boolean be) {
 267         if (!viewArrayClass.isArray())
 268             throw new IllegalArgumentException("not an array: " + viewArrayClass);
 269 
 270         Class<?> viewComponentType = viewArrayClass.getComponentType();
 271 
 272         if (viewComponentType == long.class) {
 273             return new VarHandleByteArrayAsLongs.ByteBufferHandle(be);
 274         }
 275         else if (viewComponentType == int.class) {
 276             return new VarHandleByteArrayAsInts.ByteBufferHandle(be);
 277         }
 278         else if (viewComponentType == short.class) {
 279             return new VarHandleByteArrayAsShorts.ByteBufferHandle(be);
 280         }
 281         else if (viewComponentType == char.class) {
 282             return new VarHandleByteArrayAsChars.ByteBufferHandle(be);
 283         }
 284         else if (viewComponentType == double.class) {
 285             return new VarHandleByteArrayAsDoubles.ByteBufferHandle(be);
 286         }
 287         else if (viewComponentType == float.class) {
 288             return new VarHandleByteArrayAsFloats.ByteBufferHandle(be);
 289         }
 290 
 291         throw new UnsupportedOperationException();
 292     }
 293 
 294 //    /**
 295 //     * A helper program to generate the VarHandleGuards class with a set of
 296 //     * static guard methods each of which corresponds to a particular shape and
 297 //     * performs a type check of the symbolic type descriptor with the VarHandle
 298 //     * type descriptor before linking/invoking to the underlying operation as
 299 //     * characterized by the operation member name on the VarForm of the
 300 //     * VarHandle.
 301 //     * <p>
 302 //     * The generated class essentially encapsulates pre-compiled LambdaForms,
 303 //     * one for each method, for the most set of common method signatures.
 304 //     * This reduces static initialization costs, footprint costs, and circular
 305 //     * dependencies that may arise if a class is generated per LambdaForm.
 306 //     * <p>
 307 //     * A maximum of L*T*S methods will be generated where L is the number of
 308 //     * access modes kinds (or unique operation signatures) and T is the number
 309 //     * of variable types and S is the number of shapes (such as instance field,
 310 //     * static field, or array access).
 311 //     * If there are 4 unique operation signatures, 5 basic types (Object, int,
 312 //     * long, float, double), and 3 shapes then a maximum of 60 methods will be
 313 //     * generated.  However, the number is likely to be less since there
 314 //     * be duplicate signatures.
 315 //     * <p>
 316 //     * Each method is annotated with @LambdaForm.Compiled to inform the runtime
 317 //     * that such methods should be treated as if a method of a class that is the
 318 //     * result of compiling a LambdaForm.  Annotation of such methods is
 319 //     * important for correct evaluation of certain assertions and method return
 320 //     * type profiling in HotSpot.
 321 //     */
 322 //    public static class GuardMethodGenerator {
 323 //
 324 //        static final String GUARD_METHOD_SIG_TEMPLATE = "<RETURN> <NAME>_<SIGNATURE>(<PARAMS>)";
 325 //
 326 //        static final String GUARD_METHOD_TEMPLATE =
 327 //                "@ForceInline\n" +
 328 //                "@LambdaForm.Compiled\n" +
 329 //                "final static <METHOD> throws Throwable {\n" +
 330 //                "    if (handle.vform.methodType_table[ad.type] == ad.symbolicMethodType) {\n" +
 331 //                "        <RESULT_ERASED>MethodHandle.linkToStatic(<LINK_TO_STATIC_ARGS>);<RETURN_ERASED>\n" +
 332 //                "    }\n" +
 333 //                "    else {\n" +
 334 //                "        MethodHandle mh = handle.getMethodHandle(ad.mode);\n" +
 335 //                "        <RETURN>mh.asType(ad.symbolicMethodTypeInvoker).invokeBasic(<LINK_TO_INVOKER_ARGS>);\n" +
 336 //                "    }\n" +
 337 //                "}";
 338 //
 339 //        static final String GUARD_METHOD_TEMPLATE_V =
 340 //                "@ForceInline\n" +
 341 //                "@LambdaForm.Compiled\n" +
 342 //                "final static <METHOD> throws Throwable {\n" +
 343 //                "    if (handle.vform.methodType_table[ad.type] == ad.symbolicMethodType) {\n" +
 344 //                "        MethodHandle.linkToStatic(<LINK_TO_STATIC_ARGS>);\n" +
 345 //                "    }\n" +
 346 //                "    else if (handle.vform.getMethodType_V(ad.type) == ad.symbolicMethodType) {\n" +
 347 //                "        MethodHandle.linkToStatic(<LINK_TO_STATIC_ARGS>);\n" +
 348 //                "    }\n" +
 349 //                "    else {\n" +
 350 //                "        MethodHandle mh = handle.getMethodHandle(ad.mode);\n" +
 351 //                "        mh.asType(ad.symbolicMethodTypeInvoker).invokeBasic(<LINK_TO_INVOKER_ARGS>);\n" +
 352 //                "    }\n" +
 353 //                "}";
 354 //
 355 //        // A template for deriving the operations
 356 //        // could be supported by annotating VarHandle directly with the
 357 //        // operation kind and shape
 358 //        interface VarHandleTemplate {
 359 //            Object get();
 360 //
 361 //            void set(Object value);
 362 //
 363 //            boolean compareAndSet(Object actualValue, Object expectedValue);
 364 //
 365 //            Object compareAndExchange(Object actualValue, Object expectedValue);
 366 //
 367 //            Object getAndUpdate(Object value);
 368 //        }
 369 //
 370 //        static class HandleType {
 371 //            final Class<?> receiver;
 372 //            final Class<?>[] intermediates;
 373 //            final Class<?> value;
 374 //
 375 //            HandleType(Class<?> receiver, Class<?> value, Class<?>... intermediates) {
 376 //                this.receiver = receiver;
 377 //                this.intermediates = intermediates;
 378 //                this.value = value;
 379 //            }
 380 //        }
 381 //
 382 //        /**
 383 //         * @param args parameters
 384 //         */
 385 //        public static void main(String[] args) {
 386 //            System.out.println("package java.lang.invoke;");
 387 //            System.out.println();
 388 //            System.out.println("import jdk.internal.vm.annotation.ForceInline;");
 389 //            System.out.println();
 390 //            System.out.println("// This class is auto-generated by " +
 391 //                               GuardMethodGenerator.class.getName() +
 392 //                               ". Do not edit.");
 393 //            System.out.println("final class VarHandleGuards {");
 394 //
 395 //            System.out.println();
 396 //
 397 //            // Declare the stream of shapes
 398 //            Stream<HandleType> hts = Stream.of(
 399 //                    // Object->Object
 400 //                    new HandleType(Object.class, Object.class),
 401 //                    // Object->int
 402 //                    new HandleType(Object.class, int.class),
 403 //                    // Object->long
 404 //                    new HandleType(Object.class, long.class),
 405 //                    // Object->float
 406 //                    new HandleType(Object.class, float.class),
 407 //                    // Object->double
 408 //                    new HandleType(Object.class, double.class),
 409 //
 410 //                    // <static>->Object
 411 //                    new HandleType(null, Object.class),
 412 //                    // <static>->int
 413 //                    new HandleType(null, int.class),
 414 //                    // <static>->long
 415 //                    new HandleType(null, long.class),
 416 //                    // <static>->float
 417 //                    new HandleType(null, float.class),
 418 //                    // <static>->double
 419 //                    new HandleType(null, double.class),
 420 //
 421 //                    // Array[int]->Object
 422 //                    new HandleType(Object.class, Object.class, int.class),
 423 //                    // Array[int]->int
 424 //                    new HandleType(Object.class, int.class, int.class),
 425 //                    // Array[int]->long
 426 //                    new HandleType(Object.class, long.class, int.class),
 427 //                    // Array[int]->float
 428 //                    new HandleType(Object.class, float.class, int.class),
 429 //                    // Array[int]->double
 430 //                    new HandleType(Object.class, double.class, int.class),
 431 //
 432 //                    // Array[long]->int
 433 //                    new HandleType(Object.class, int.class, long.class),
 434 //                    // Array[long]->long
 435 //                    new HandleType(Object.class, long.class, long.class)
 436 //            );
 437 //
 438 //            hts.flatMap(ht -> Stream.of(VarHandleTemplate.class.getMethods()).
 439 //                    map(m -> generateMethodType(m, ht.receiver, ht.value, ht.intermediates))).
 440 //                    distinct().
 441 //                    map(mt -> generateMethod(mt)).
 442 //                    forEach(s -> {
 443 //                        System.out.println(s);
 444 //                        System.out.println();
 445 //                    });
 446 //
 447 //            System.out.println("}");
 448 //        }
 449 //
 450 //        static MethodType generateMethodType(Method m, Class<?> receiver, Class<?> value, Class<?>... intermediates) {
 451 //            Class<?> returnType = m.getReturnType() == Object.class
 452 //                                  ? value : m.getReturnType();
 453 //
 454 //            List<Class<?>> params = new ArrayList<>();
 455 //            if (receiver != null)
 456 //                params.add(receiver);
 457 //            for (int i = 0; i < intermediates.length; i++) {
 458 //                params.add(intermediates[i]);
 459 //            }
 460 //            for (Parameter p : m.getParameters()) {
 461 //                params.add(value);
 462 //            }
 463 //            return MethodType.methodType(returnType, params);
 464 //        }
 465 //
 466 //        static String generateMethod(MethodType mt) {
 467 //            Class<?> returnType = mt.returnType();
 468 //
 469 //            LinkedHashMap<String, Class<?>> params = new LinkedHashMap<>();
 470 //            params.put("handle", VarHandle.class);
 471 //            for (int i = 0; i < mt.parameterCount(); i++) {
 472 //                params.put("arg" + i, mt.parameterType(i));
 473 //            }
 474 //            params.put("ad", VarHandle.AccessDescriptor.class);
 475 //
 476 //            // Generate method signature line
 477 //            String RETURN = className(returnType);
 478 //            String NAME = "guard";
 479 //            String SIGNATURE = getSignature(mt);
 480 //            String PARAMS = params.entrySet().stream().
 481 //                    map(e -> className(e.getValue()) + " " + e.getKey()).
 482 //                    collect(joining(", "));
 483 //            String METHOD = GUARD_METHOD_SIG_TEMPLATE.
 484 //                    replace("<RETURN>", RETURN).
 485 //                    replace("<NAME>", NAME).
 486 //                    replace("<SIGNATURE>", SIGNATURE).
 487 //                    replace("<PARAMS>", PARAMS);
 488 //
 489 //            // Generate method
 490 //            params.remove("ad");
 491 //
 492 //            List<String> LINK_TO_STATIC_ARGS = params.keySet().stream().
 493 //                    collect(toList());
 494 //            LINK_TO_STATIC_ARGS.add("handle.vform.getMemberName(ad.mode)");
 495 //            List<String> LINK_TO_STATIC_ARGS_V = params.keySet().stream().
 496 //                    collect(toList());
 497 //            LINK_TO_STATIC_ARGS_V.add("handle.vform.getMemberName_V(ad.mode)");
 498 //
 499 //            List<String> LINK_TO_INVOKER_ARGS = params.keySet().stream().
 500 //                    collect(toList());
 501 //
 502 //            RETURN = returnType == void.class
 503 //                     ? ""
 504 //                     : returnType == Object.class
 505 //                       ? "return "
 506 //                       : "return (" + returnType.getName() + ") ";
 507 //
 508 //            String RESULT_ERASED = returnType == void.class
 509 //                                   ? ""
 510 //                                   : returnType != Object.class
 511 //                                     ? "return (" + returnType.getName() + ") "
 512 //                                     : "Object r = ";
 513 //
 514 //            String RETURN_ERASED = returnType != Object.class
 515 //                                   ? ""
 516 //                                   : " return ad.returnType.cast(r);";
 517 //
 518 //            String template = returnType == void.class
 519 //                              ? GUARD_METHOD_TEMPLATE_V
 520 //                              : GUARD_METHOD_TEMPLATE;
 521 //            return template.
 522 //                    replace("<METHOD>", METHOD).
 523 //                    replace("<NAME>", NAME).
 524 //                    replaceAll("<RETURN>", RETURN).
 525 //                    replace("<RESULT_ERASED>", RESULT_ERASED).
 526 //                    replace("<RETURN_ERASED>", RETURN_ERASED).
 527 //                    replaceAll("<LINK_TO_STATIC_ARGS>", LINK_TO_STATIC_ARGS.stream().
 528 //                            collect(joining(", "))).
 529 //                    replaceAll("<LINK_TO_STATIC_ARGS_V>", LINK_TO_STATIC_ARGS_V.stream().
 530 //                            collect(joining(", "))).
 531 //                    replace("<LINK_TO_INVOKER_ARGS>", LINK_TO_INVOKER_ARGS.stream().
 532 //                            collect(joining(", ")))
 533 //                    ;
 534 //        }
 535 //
 536 //        static String className(Class<?> c) {
 537 //            String n = c.getName();
 538 //            if (n.startsWith("java.lang.")) {
 539 //                n = n.replace("java.lang.", "");
 540 //                if (n.startsWith("invoke.")) {
 541 //                    n = n.replace("invoke.", "");
 542 //                }
 543 //            }
 544 //            return n.replace('$', '.');
 545 //        }
 546 //
 547 //        static String getSignature(MethodType m) {
 548 //            StringBuilder sb = new StringBuilder(m.parameterCount() + 1);
 549 //
 550 //            for (int i = 0; i < m.parameterCount(); i++) {
 551 //                Class<?> pt = m.parameterType(i);
 552 //                sb.append(getCharType(pt));
 553 //            }
 554 //
 555 //            sb.append('_').append(getCharType(m.returnType()));
 556 //
 557 //            return sb.toString();
 558 //        }
 559 //
 560 //        static char getCharType(Class<?> pt) {
 561 //            if (pt == void.class) {
 562 //                return 'V';
 563 //            }
 564 //            else if (!pt.isPrimitive()) {
 565 //                return 'L';
 566 //            }
 567 //            else if (pt == boolean.class) {
 568 //                return 'Z';
 569 //            }
 570 //            else if (pt == int.class) {
 571 //                return 'I';
 572 //            }
 573 //            else if (pt == long.class) {
 574 //                return 'J';
 575 //            }
 576 //            else if (pt == float.class) {
 577 //                return 'F';
 578 //            }
 579 //            else if (pt == double.class) {
 580 //                return 'D';
 581 //            }
 582 //            else {
 583 //                throw new IllegalStateException(pt.getName());
 584 //            }
 585 //        }
 586 //    }
 587 }