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 /java/lang/invoke/common
 29  * @build test.java.lang.invoke.lib.InstructionHelper
 30  * @enablePreview
 31  * @run testng CondyInterfaceWithOverpassMethods
 32  * @run testng/othervm -XX:+UnlockDiagnosticVMOptions -XX:UseBootstrapCallInfo=3 CondyInterfaceWithOverpassMethods
 33  */
 34 
 35 import java.lang.classfile.ClassFile;
 36 import org.testng.annotations.BeforeClass;
 37 import org.testng.annotations.Test;
 38 import test.java.lang.invoke.lib.InstructionHelper;
 39 
 40 import java.lang.constant.*;
 41 import java.lang.invoke.MethodHandles;
 42 import java.lang.invoke.MethodType;
 43 
 44 @Test
 45 public class CondyInterfaceWithOverpassMethods {
 46     interface A {
 47         int a();
 48 
 49         default int x() {
 50             return 1;
 51         }
 52     }
 53 
 54 
 55     // Generated class with methods containing condy ldc
 56     Class<?> gc;
 57 
 58     public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type) {
 59         return name;
 60     }
 61 
 62     @BeforeClass
 63     public void generateClass() throws Exception {
 64 //        interface B extends A {
 65 //            // Overpass for method A.a
 66 //
 67 //            default void y() {
 68 //                // ldc to Dynamic
 69 //            }
 70 //        }
 71         Class<?> thisClass = CondyInterfaceWithOverpassMethods.class;
 72 
 73         String genClassName = thisClass.getSimpleName() + "$Code";
 74         String bsmClassName = thisClass.descriptorString();
 75         String bsmMethodName = "bsm";
 76         String bsmDescriptor = MethodType.methodType(Object.class, MethodHandles.Lookup.class,
 77                 String.class, Class.class).toMethodDescriptorString();
 78 
 79         byte[] byteArray = ClassFile.of().build(ClassDesc.of(genClassName), classBuilder -> classBuilder
 80                 .withFlags(ClassFile.ACC_INTERFACE + ClassFile.ACC_ABSTRACT)
 81                 .withSuperclass(ConstantDescs.CD_Object)
 82                 .withInterfaceSymbols(InstructionHelper.classDesc(A.class))
 83                 .withMethod("y", MethodTypeDesc.of(ConstantDescs.CD_String), ClassFile.ACC_PUBLIC, methodBuilder -> methodBuilder
 84                         .withCode(codeBuilder -> codeBuilder
 85                                 .ldc(DynamicConstantDesc.ofNamed(
 86                                                 MethodHandleDesc.of(
 87                                                         DirectMethodHandleDesc.Kind.STATIC,
 88                                                         ClassDesc.ofDescriptor(bsmClassName),
 89                                                         bsmMethodName,
 90                                                         bsmDescriptor
 91                                                 ),
 92                                                 "String",
 93                                                 ConstantDescs.CD_String
 94                                         )
 95                                 )
 96                                 .areturn()
 97                         )
 98                 )
 99         );
100         gc = MethodHandles.lookup().defineClass(byteArray);
101     }
102 
103     @Test
104     public void testClass() throws Exception {
105         // Trigger initialization
106         Class.forName(gc.getName());
107     }
108 }