1 /* 2 * Copyright (c) 2011, 2015, 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 7192246 27 * @summary check that code attributed for default methods is correctly generated 28 * @modules jdk.jdeps/com.sun.tools.classfile 29 */ 30 31 import com.sun.tools.classfile.AccessFlags; 32 import com.sun.tools.classfile.Attribute; 33 import com.sun.tools.classfile.ClassFile; 34 import com.sun.tools.classfile.ConstantPool.*; 35 import com.sun.tools.classfile.Code_attribute; 36 import com.sun.tools.classfile.Instruction; 37 import com.sun.tools.classfile.Method; 38 39 import com.sun.tools.classfile.Opcode; 40 import java.io.*; 41 42 public class TestDefaultBody { 43 44 interface TestInterface { 45 int no_default(int i); 46 default int yes_default(int i) { return impl(this, i); } 47 } 48 49 static int impl(TestInterface ti, int i) { return 0; } 50 51 static final String TARGET_CLASS_NAME = "TestDefaultBody"; 52 static final String TARGET_NAME = "impl"; 53 static final String TARGET_TYPE = "(LTestDefaultBody$TestInterface;I)I"; 54 static final String SUBTEST_NAME = TestInterface.class.getName() + ".class"; 55 static final String TEST_METHOD_NAME = "yes_default"; 56 57 public static void main(String... args) throws Exception { 58 new TestDefaultBody().run(); 59 } 60 61 public void run() throws Exception { 62 String workDir = System.getProperty("test.classes"); 63 File compiledTest = new File(workDir, SUBTEST_NAME); 64 verifyDefaultBody(compiledTest); 65 } 66 67 void verifyDefaultBody(File f) { 68 System.err.println("verify: " + f); 69 try { 70 ClassFile cf = ClassFile.read(f); 71 Method testMethod = null; 72 Code_attribute codeAttr = null; 73 for (Method m : cf.methods) { 74 codeAttr = (Code_attribute)m.attributes.get(Attribute.Code); 75 String mname = m.getName(cf.constant_pool); 76 if (mname.equals(TEST_METHOD_NAME)) { 77 testMethod = m; 78 break; 79 } else { 80 codeAttr = null; 81 } 82 } 83 if (testMethod == null) { 84 throw new Error("Test method not found"); 85 } 86 if (testMethod.access_flags.is(AccessFlags.ACC_ABSTRACT)) { 87 throw new Error("Test method is abstract"); 88 } 89 if (codeAttr == null) { 90 throw new Error("Code attribute in test method not found"); 91 } 92 93 boolean found = false; 94 for (Instruction instr : codeAttr.getInstructions()) { 95 if (instr.getOpcode() == Opcode.INVOKESTATIC) { 96 found = true; 97 int pc_index = instr.getShort(1); 98 CONSTANT_Methodref_info mref = (CONSTANT_Methodref_info)cf.constant_pool.get(pc_index); 99 String className = mref.getClassName(); 100 String targetName = mref.getNameAndTypeInfo().getName(); 101 String targetType = mref.getNameAndTypeInfo().getType(); 102 103 if (!className.equals(TARGET_CLASS_NAME)) { 104 throw new Error("unexpected class in default method body " + className); 105 } 106 if (!targetName.equals(TARGET_NAME)) { 107 throw new Error("unexpected method name in default method body " + targetName); 108 } 109 if (!targetType.equals(TARGET_TYPE)) { 110 throw new Error("unexpected method type in default method body " + targetType); 111 } 112 break; 113 } 114 } 115 116 if (!found) { 117 throw new Error("no invokestatic found in default method body"); 118 } 119 } catch (Exception e) { 120 e.printStackTrace(); 121 throw new Error("error reading " + f +": " + e); 122 } 123 } 124 }