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