test/jdk/java/lang/invoke/condy/CondyRepeatFailedResolution.java
author lfoltan
Fri, 04 May 2018 12:14:09 -0400
changeset 49987 e4b3cc56e2b2
parent 49387 98e7a2c315a9
permissions -rw-r--r--
8189916: Dynamic Constant support for Sparc Summary: Change to the template interpreter to support dynamic constant on Sparc. Reviewed-by: coleenp, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     1
/*
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     4
 *
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     8
 *
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    13
 * accompanied this code).
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    14
 *
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    18
 *
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    21
 * questions.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    22
 */
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    23
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    24
/*
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    25
 * @test
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    26
 * @bug 8186211
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    27
 * @summary Test basic invocation of multiple ldc's of the same dynamic constant that fail resolution
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    28
 * @library /lib/testlibrary/bytecode /java/lang/invoke/common
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    29
 * @build jdk.experimental.bytecode.BasicClassBuilder
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    30
 * @run testng CondyRepeatFailedResolution
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    31
 * @run testng/othervm -XX:+UnlockDiagnosticVMOptions -XX:UseBootstrapCallInfo=3 CondyRepeatFailedResolution
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    32
 */
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    33
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    34
import jdk.experimental.bytecode.BasicClassBuilder;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    35
import jdk.experimental.bytecode.Flag;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    36
import jdk.experimental.bytecode.TypedCodeBuilder;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    37
import org.testng.Assert;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    38
import org.testng.annotations.BeforeClass;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    39
import org.testng.annotations.Test;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    40
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    41
import java.lang.invoke.MethodHandles;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    42
import java.lang.invoke.MethodType;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    43
import java.lang.reflect.InvocationTargetException;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    44
import java.lang.reflect.Method;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    45
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    46
@Test
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    47
public class CondyRepeatFailedResolution {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    48
    // Counter used to determine if a given BSM is invoked more than once
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    49
    static int bsm_called = 0;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    50
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    51
    // Generated class with methods containing condy ldc
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    52
    Class<?> gc;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    53
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    54
    // Bootstrap method used to represent primitive values
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    55
    // that cannot be represented directly in the constant pool,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    56
    // such as byte, and for completeness of testing primitive values
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    57
    // that can be represented directly, such as double or long that
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    58
    // take two slots
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    59
    public static Object intConversion(MethodHandles.Lookup l,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    60
                                       String constantName,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    61
                                       Class<?> constantType,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    62
                                       int value) throws Throwable {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    63
        ++bsm_called;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    64
        // replace constantName with a bogus value to trigger failed resolution
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    65
        constantName = "Foo";
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    66
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    67
        switch (constantName) {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    68
            case "B":
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    69
                return (byte) value;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    70
            case "C":
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    71
                return (char) value;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    72
            case "D":
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    73
                return (double) value;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    74
            case "F":
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    75
                return (float) value;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    76
            case "I":
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    77
                return value;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    78
            case "J":
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    79
                return (long) value;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    80
            case "S":
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    81
                return (short) value;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    82
            case "Z":
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    83
                return value > 0;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    84
            case "nullRef":
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    85
                return null;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    86
            case "string":
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    87
                return "string";
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    88
            case "stringArray":
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    89
                return new String[]{"string", "string"};
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    90
            default:
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    91
                throw new BootstrapMethodError("Failure to generate a dynamic constant");
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    92
        }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    93
    }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    94
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    95
    @BeforeClass
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    96
    public void generateClass() throws Exception {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    97
        String genClassName = CondyRepeatFailedResolution.class.getSimpleName() + "$Code";
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    98
        String bsmClassName = CondyRepeatFailedResolution.class.getCanonicalName().replace('.', '/');
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    99
        String bsmMethodName = "intConversion";
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   100
        String bsmDescriptor = MethodType.methodType(Object.class, MethodHandles.Lookup.class,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   101
                                                     String.class, Class.class, int.class).toMethodDescriptorString();
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   102
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   103
        byte[] byteArray = new BasicClassBuilder(genClassName, 55, 0)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   104
                .withSuperclass("java/lang/Object")
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   105
                .withMethod("<init>", "()V", M ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   106
                        M.withFlags(Flag.ACC_PUBLIC)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   107
                                .withCode(TypedCodeBuilder::new, C ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   108
                                        C.aload_0().invokespecial("java/lang/Object", "<init>", "()V", false).return_()
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   109
                                ))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   110
                .withMethod("B", "()B", M ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   111
                        M.withFlags(Flag.ACC_PUBLIC, Flag.ACC_STATIC)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   112
                                .withCode(TypedCodeBuilder::new, C ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   113
                                        C.ldc("B", "B", bsmClassName, bsmMethodName, bsmDescriptor,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   114
                                              S -> S.add(Byte.MAX_VALUE))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   115
                                                .ireturn()
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   116
                                ))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   117
                .withMethod("C", "()C", M ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   118
                        M.withFlags(Flag.ACC_PUBLIC, Flag.ACC_STATIC)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   119
                                .withCode(TypedCodeBuilder::new, C ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   120
                                        C.ldc("C", "C", bsmClassName, bsmMethodName, bsmDescriptor,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   121
                                              S -> S.add(Character.MAX_VALUE))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   122
                                                .ireturn()
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   123
                                ))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   124
                .withMethod("D", "()D", M ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   125
                        M.withFlags(Flag.ACC_PUBLIC, Flag.ACC_STATIC)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   126
                                .withCode(TypedCodeBuilder::new, C ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   127
                                        C.ldc("D", "D", bsmClassName, bsmMethodName, bsmDescriptor,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   128
                                              S -> S.add(Integer.MAX_VALUE))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   129
                                                .dreturn()
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   130
                                ))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   131
                .withMethod("D_AsType", "()D", M ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   132
                        M.withFlags(Flag.ACC_PUBLIC, Flag.ACC_STATIC)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   133
                                .withCode(TypedCodeBuilder::new, C ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   134
                                        C.ldc("I", "D", bsmClassName, bsmMethodName, bsmDescriptor,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   135
                                              S -> S.add(Integer.MAX_VALUE))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   136
                                                .dreturn()
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   137
                                ))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   138
                .withMethod("F", "()F", M ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   139
                        M.withFlags(Flag.ACC_PUBLIC, Flag.ACC_STATIC)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   140
                                .withCode(TypedCodeBuilder::new, C ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   141
                                        C.ldc("F", "F", bsmClassName, bsmMethodName, bsmDescriptor,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   142
                                              S -> S.add(Integer.MAX_VALUE))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   143
                                                .freturn()
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   144
                                ))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   145
                .withMethod("F_AsType", "()F", M ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   146
                        M.withFlags(Flag.ACC_PUBLIC, Flag.ACC_STATIC)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   147
                                .withCode(TypedCodeBuilder::new, C ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   148
                                        C.ldc("I", "F", bsmClassName, bsmMethodName, bsmDescriptor,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   149
                                              S -> S.add(Integer.MAX_VALUE))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   150
                                                .freturn()
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   151
                                ))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   152
                .withMethod("I", "()I", M ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   153
                        M.withFlags(Flag.ACC_PUBLIC, Flag.ACC_STATIC)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   154
                                .withCode(TypedCodeBuilder::new, C ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   155
                                        C.ldc("I", "I", bsmClassName, bsmMethodName, bsmDescriptor,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   156
                                              S -> S.add(Integer.MAX_VALUE))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   157
                                                .ireturn()
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   158
                                ))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   159
                .withMethod("J", "()J", M ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   160
                        M.withFlags(Flag.ACC_PUBLIC, Flag.ACC_STATIC)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   161
                                .withCode(TypedCodeBuilder::new, C ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   162
                                        C.ldc("J", "J", bsmClassName, bsmMethodName, bsmDescriptor,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   163
                                              S -> S.add(Integer.MAX_VALUE))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   164
                                                .lreturn()
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   165
                                ))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   166
                .withMethod("J_AsType", "()J", M ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   167
                        M.withFlags(Flag.ACC_PUBLIC, Flag.ACC_STATIC)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   168
                                .withCode(TypedCodeBuilder::new, C ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   169
                                        C.ldc("I", "J", bsmClassName, bsmMethodName, bsmDescriptor,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   170
                                              S -> S.add(Integer.MAX_VALUE))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   171
                                                .lreturn()
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   172
                                ))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   173
                .withMethod("S", "()S", M ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   174
                        M.withFlags(Flag.ACC_PUBLIC, Flag.ACC_STATIC)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   175
                                .withCode(TypedCodeBuilder::new, C ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   176
                                        C.ldc("S", "S", bsmClassName, bsmMethodName, bsmDescriptor,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   177
                                              S -> S.add(Short.MAX_VALUE))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   178
                                                .ireturn()
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   179
                                ))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   180
                .withMethod("Z_F", "()Z", M ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   181
                        M.withFlags(Flag.ACC_PUBLIC, Flag.ACC_STATIC)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   182
                                .withCode(TypedCodeBuilder::new, C ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   183
                                        C.ldc("Z", "Z", bsmClassName, bsmMethodName, bsmDescriptor,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   184
                                              S -> S.add(0))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   185
                                                .ireturn()
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   186
                                ))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   187
                .withMethod("Z_T", "()Z", M ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   188
                        M.withFlags(Flag.ACC_PUBLIC, Flag.ACC_STATIC)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   189
                                .withCode(TypedCodeBuilder::new, C ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   190
                                        C.ldc("Z", "Z", bsmClassName, bsmMethodName, bsmDescriptor,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   191
                                              S -> S.add(1))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   192
                                                .ireturn()
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   193
                                ))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   194
                .withMethod("null", "()Ljava/lang/Object;", M ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   195
                        M.withFlags(Flag.ACC_PUBLIC, Flag.ACC_STATIC)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   196
                                .withCode(TypedCodeBuilder::new, C ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   197
                                        C.ldc("nullRef", "Ljava/lang/Object;", bsmClassName, bsmMethodName, bsmDescriptor,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   198
                                              S -> S.add(Integer.MAX_VALUE))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   199
                                                .areturn()
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   200
                                ))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   201
                .withMethod("string", "()Ljava/lang/String;", M ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   202
                        M.withFlags(Flag.ACC_PUBLIC, Flag.ACC_STATIC)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   203
                                .withCode(TypedCodeBuilder::new, C ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   204
                                        C.ldc("string", "Ljava/lang/String;", bsmClassName, bsmMethodName, bsmDescriptor,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   205
                                              S -> S.add(Integer.MAX_VALUE))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   206
                                                .areturn()
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   207
                                ))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   208
                .withMethod("stringArray", "()[Ljava/lang/String;", M ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   209
                        M.withFlags(Flag.ACC_PUBLIC, Flag.ACC_STATIC)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   210
                                .withCode(TypedCodeBuilder::new, C ->
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   211
                                        C.ldc("stringArray", "[Ljava/lang/String;", bsmClassName, bsmMethodName, bsmDescriptor,
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   212
                                              S -> S.add(Integer.MAX_VALUE))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   213
                                                .areturn()
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   214
                                ))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   215
                .build();
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   216
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   217
        gc = MethodHandles.lookup().defineClass(byteArray);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   218
    }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   219
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   220
    @Test
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   221
    public void testPrimitives() throws Exception {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   222
        testConstants();
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   223
    }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   224
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   225
    @Test
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   226
    public void testRefs() throws Exception {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   227
        testConstant("string", "string");
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   228
        testConstant("stringArray", new String[]{"string", "string"});
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   229
    }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   230
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   231
    void testConstants() throws Exception {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   232
        // Note: for the _asType methods the BSM returns an int which is
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   233
        // then converted by an asType transformation
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   234
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   235
        testConstant("B", Byte.MAX_VALUE);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   236
        testConstant("C", Character.MAX_VALUE);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   237
        testConstant("D", (double) Integer.MAX_VALUE);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   238
        testConstant("D_AsType", (double) Integer.MAX_VALUE);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   239
        testConstant("F", (float) Integer.MAX_VALUE);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   240
        testConstant("F_AsType", (float) Integer.MAX_VALUE);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   241
        testConstant("I", Integer.MAX_VALUE);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   242
        testConstant("J", (long) Integer.MAX_VALUE);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   243
        testConstant("J_AsType", (long) Integer.MAX_VALUE);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   244
        testConstant("S", Short.MAX_VALUE);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   245
        testConstant("Z_F", false);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   246
        testConstant("Z_T", true);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   247
        testConstant("null", null);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   248
    }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   249
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   250
    void testConstant(String name, Object expected) throws Exception {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   251
        Method m = gc.getDeclaredMethod(name);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   252
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   253
        bsm_called = 0;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   254
        try {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   255
            Object r1 = m.invoke(null);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   256
            Assert.fail("InvocationTargetException expected to be thrown after first invocation");
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   257
        } catch (InvocationTargetException e1) {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   258
            // bsm_called should have been incremented prior to the exception
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   259
            Assert.assertEquals(bsm_called, 1);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   260
            Assert.assertTrue(e1.getCause() instanceof BootstrapMethodError);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   261
            // Try invoking method again to ensure that the bootstrap
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   262
            // method is not invoked twice and a resolution failure
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   263
            // results.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   264
            try {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   265
                Object r2 = m.invoke(null);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   266
                Assert.fail("InvocationTargetException expected to be thrown after second invocation");
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   267
            } catch (InvocationTargetException e2) {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   268
                // bsm_called should remain at 1 since the bootstrap
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   269
                // method should not have been invoked.
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   270
                Assert.assertEquals(bsm_called, 1);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   271
                Assert.assertTrue(e2.getCause() instanceof BootstrapMethodError);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   272
            } catch (Throwable t2) {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   273
                Assert.fail("InvocationTargetException expected to be thrown");
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   274
            }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   275
        } catch (Throwable t1) {
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   276
                Assert.fail("InvocationTargetException expected to be thrown");
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   277
        }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   278
    }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   279
}