test/jdk/java/lang/invoke/condy/CondyInterfaceWithOverpassMethods.java
author psandoz
Thu, 08 Mar 2018 14:33:57 -0800
changeset 49380 74518f9ca4b4
parent 48965 test/jdk/java/lang/invoke/condy/CondyReturnPrimitiveTest.java@be873d8c0114
child 49387 98e7a2c315a9
permissions -rw-r--r--
8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods Reviewed-by: lfoltan, acorn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
     1
/*
49380
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
48826
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 8186046
49380
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    27
 * @summary Test for an interface using condy with default overpass methods
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    28
 * @library /lib/testlibrary/bytecode
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    29
 * @build jdk.experimental.bytecode.BasicClassBuilder
49380
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    30
 * @run testng CondyInterfaceWithOverpassMethods
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    31
 * @run testng/othervm -XX:+UnlockDiagnosticVMOptions -XX:UseBootstrapCallInfo=3 CondyInterfaceWithOverpassMethods
48826
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.io.File;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    42
import java.io.FileOutputStream;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    43
import java.lang.invoke.MethodHandles;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    44
import java.lang.invoke.MethodType;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    45
import java.lang.reflect.Method;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    46
import java.util.concurrent.atomic.AtomicInteger;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    47
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    48
@Test
49380
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    49
public class CondyInterfaceWithOverpassMethods {
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    50
    interface A {
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    51
        int a();
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    52
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    53
        default int x() {
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    54
            return 1;
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    55
        }
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    56
    }
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    57
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    58
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    59
    // Generated class with methods containing condy ldc
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    60
    Class<?> gc;
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    61
49380
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    62
    public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type) {
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    63
        return name;
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    64
    }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    65
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    66
    @BeforeClass
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    67
    public void generateClass() throws Exception {
49380
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    68
//        interface B extends A {
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    69
//            // Overpass for method A.a
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    70
//
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    71
//            default void y() {
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    72
//                // ldc to Dynamic
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    73
//            }
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    74
//        }
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    75
        Class<?> thisClass = CondyInterfaceWithOverpassMethods.class;
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    76
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    77
        String genClassName = thisClass.getSimpleName() + "$Code";
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    78
        String bsmClassName = thisClass.getCanonicalName().replace('.', '/');
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    79
        String bsmMethodName = "bsm";
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    80
        String bsmDescriptor = MethodType.methodType(Object.class, MethodHandles.Lookup.class,
49380
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    81
                                                     String.class, Class.class).toMethodDescriptorString();
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    82
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    83
        byte[] byteArray = new BasicClassBuilder(genClassName, 55, 0)
49380
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    84
                .withFlags(Flag.ACC_INTERFACE, Flag.ACC_ABSTRACT)
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    85
                .withSuperclass("java/lang/Object")
49380
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    86
                .withSuperinterface(thisClass.getCanonicalName().replace('.', '/') + "$" + A.class.getSimpleName())
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    87
                .withMethod("y", "()Ljava/lang/String;", M ->
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    88
                        M.withFlags(Flag.ACC_PUBLIC)
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    89
                                .withCode(TypedCodeBuilder::new, C ->
49380
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    90
                                        C.ldc("String", "Ljava/lang/String;", bsmClassName, bsmMethodName, bsmDescriptor,
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
    91
                                              S -> {})
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    92
                                                .areturn()
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    93
                                ))
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    94
                .build();
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    95
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    96
        // For debugging purposes
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    97
        new FileOutputStream(new File(genClassName + ".class")).write(byteArray);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    98
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
    99
        gc = MethodHandles.lookup().defineClass(byteArray);
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   100
    }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   101
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   102
    @Test
49380
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
   103
    public void testClass() throws Exception {
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
   104
        // Trigger initialization
74518f9ca4b4 8199342: The constant pool forgets it has a Dynamic entry if there are overpass methods
psandoz
parents: 48965
diff changeset
   105
        Class.forName(gc.getName());
48826
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   106
    }
c4d9d1b08e2e 8186209: Tool support for ConstantDynamic
psandoz
parents:
diff changeset
   107
}