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  */
  32 
  33 import jdk.experimental.bytecode.BasicClassBuilder;
  34 import jdk.experimental.bytecode.Flag;
  35 import jdk.experimental.bytecode.TypedCodeBuilder;
  36 import org.testng.annotations.BeforeClass;
  37 import org.testng.annotations.Test;
  38 
  39 import java.lang.invoke.MethodHandles;
  40 import java.lang.invoke.MethodType;
  41 
  42 @Test
  43 public class CondyInterfaceWithOverpassMethods {
  44     interface A {
  45         int a();
  46 
  47         default int x() {
  48             return 1;
  49         }
  50     }
  51 
  52 
  53     // Generated class with methods containing condy ldc
  54     Class<?> gc;
  55 
  56     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type) {
  57         return name;
  58     }
  59 
  60     @BeforeClass
  61     public void generateClass() throws Exception {
  62 //        interface B extends A {
  63 //            // Overpass for method A.a
  64 //
  65 //            default void y() {
  66 //                // ldc to Dynamic
  67 //            }
  68 //        }
  69         Class<?> thisClass = CondyInterfaceWithOverpassMethods.class;
  70 
  71         String genClassName = thisClass.getSimpleName() + "$Code";
  72         String bsmClassName = thisClass.getCanonicalName().replace('.', '/');
  73         String bsmMethodName = "bsm";
  74         String bsmDescriptor = MethodType.methodType(Object.class, MethodHandles.Lookup.class,
  75                                                      String.class, Class.class).toMethodDescriptorString();
  76 
  77         byte[] byteArray = new BasicClassBuilder(genClassName, 55, 0)
  78                 .withFlags(Flag.ACC_INTERFACE, Flag.ACC_ABSTRACT)
  79                 .withSuperclass("java/lang/Object")
  80                 .withSuperinterface(thisClass.getCanonicalName().replace('.', '/') + "$" + A.class.getSimpleName())
  81                 .withMethod("y", "()Ljava/lang/String;", M ->
  82                         M.withFlags(Flag.ACC_PUBLIC)
  83                                 .withCode(TypedCodeBuilder::new, C ->
  84                                         C.ldc("String", "Ljava/lang/String;", bsmClassName, bsmMethodName, bsmDescriptor,
  85                                               S -> {})
  86                                                 .areturn()
  87                                 ))
  88                 .build();
  89 
  90         gc = MethodHandles.lookup().defineClass(byteArray);
  91     }
  92 
  93     @Test
  94     public void testClass() throws Exception {
  95         // Trigger initialization
  96         Class.forName(gc.getName());
  97     }
  98 }