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