test/jdk/java/lang/invoke/condy/CondyInterfaceWithOverpassMethods.java
changeset 49380 74518f9ca4b4
parent 48965 be873d8c0114
child 49387 98e7a2c315a9
equal deleted inserted replaced
49379:2d1d0c66966b 49380:74518f9ca4b4
       
     1 /*
       
     2  * Copyright (c) 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.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8186046
       
    27  * @summary Test for an interface using condy with default overpass methods
       
    28  * @library /lib/testlibrary/bytecode
       
    29  * @build jdk.experimental.bytecode.BasicClassBuilder
       
    30  * @run testng CondyInterfaceWithOverpassMethods
       
    31  * @run testng/othervm -XX:+UnlockDiagnosticVMOptions -XX:UseBootstrapCallInfo=3 CondyInterfaceWithOverpassMethods
       
    32  */
       
    33 
       
    34 import jdk.experimental.bytecode.BasicClassBuilder;
       
    35 import jdk.experimental.bytecode.Flag;
       
    36 import jdk.experimental.bytecode.TypedCodeBuilder;
       
    37 import org.testng.Assert;
       
    38 import org.testng.annotations.BeforeClass;
       
    39 import org.testng.annotations.Test;
       
    40 
       
    41 import java.io.File;
       
    42 import java.io.FileOutputStream;
       
    43 import java.lang.invoke.MethodHandles;
       
    44 import java.lang.invoke.MethodType;
       
    45 import java.lang.reflect.Method;
       
    46 import java.util.concurrent.atomic.AtomicInteger;
       
    47 
       
    48 @Test
       
    49 public class CondyInterfaceWithOverpassMethods {
       
    50     interface A {
       
    51         int a();
       
    52 
       
    53         default int x() {
       
    54             return 1;
       
    55         }
       
    56     }
       
    57 
       
    58 
       
    59     // Generated class with methods containing condy ldc
       
    60     Class<?> gc;
       
    61 
       
    62     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type) {
       
    63         return name;
       
    64     }
       
    65 
       
    66     @BeforeClass
       
    67     public void generateClass() throws Exception {
       
    68 //        interface B extends A {
       
    69 //            // Overpass for method A.a
       
    70 //
       
    71 //            default void y() {
       
    72 //                // ldc to Dynamic
       
    73 //            }
       
    74 //        }
       
    75         Class<?> thisClass = CondyInterfaceWithOverpassMethods.class;
       
    76 
       
    77         String genClassName = thisClass.getSimpleName() + "$Code";
       
    78         String bsmClassName = thisClass.getCanonicalName().replace('.', '/');
       
    79         String bsmMethodName = "bsm";
       
    80         String bsmDescriptor = MethodType.methodType(Object.class, MethodHandles.Lookup.class,
       
    81                                                      String.class, Class.class).toMethodDescriptorString();
       
    82 
       
    83         byte[] byteArray = new BasicClassBuilder(genClassName, 55, 0)
       
    84                 .withFlags(Flag.ACC_INTERFACE, Flag.ACC_ABSTRACT)
       
    85                 .withSuperclass("java/lang/Object")
       
    86                 .withSuperinterface(thisClass.getCanonicalName().replace('.', '/') + "$" + A.class.getSimpleName())
       
    87                 .withMethod("y", "()Ljava/lang/String;", M ->
       
    88                         M.withFlags(Flag.ACC_PUBLIC)
       
    89                                 .withCode(TypedCodeBuilder::new, C ->
       
    90                                         C.ldc("String", "Ljava/lang/String;", bsmClassName, bsmMethodName, bsmDescriptor,
       
    91                                               S -> {})
       
    92                                                 .areturn()
       
    93                                 ))
       
    94                 .build();
       
    95 
       
    96         // For debugging purposes
       
    97         new FileOutputStream(new File(genClassName + ".class")).write(byteArray);
       
    98 
       
    99         gc = MethodHandles.lookup().defineClass(byteArray);
       
   100     }
       
   101 
       
   102     @Test
       
   103     public void testClass() throws Exception {
       
   104         // Trigger initialization
       
   105         Class.forName(gc.getName());
       
   106     }
       
   107 }