test/tools/javac/defaultMethods/TestDefaultBody.java

Tue, 10 Sep 2013 13:47:51 +0200

author
jfranck
date
Tue, 10 Sep 2013 13:47:51 +0200
changeset 2020
bb7271e64ef6
parent 1415
01c9d4161882
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8005222: Fixed bugs should have tests with bugid in @bug tag
Reviewed-by: jfranck, jjg
Contributed-by: Andreas Lundblad <andreas.lundblad@oracle.com>

mcimadamore@1393 1 /*
jfranck@2020 2 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
mcimadamore@1393 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
mcimadamore@1393 4 *
mcimadamore@1393 5 * This code is free software; you can redistribute it and/or modify it
mcimadamore@1393 6 * under the terms of the GNU General Public License version 2 only, as
mcimadamore@1393 7 * published by the Free Software Foundation.
mcimadamore@1393 8 *
mcimadamore@1393 9 * This code is distributed in the hope that it will be useful, but WITHOUT
mcimadamore@1393 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
mcimadamore@1393 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
mcimadamore@1393 12 * version 2 for more details (a copy is included in the LICENSE file that
mcimadamore@1393 13 * accompanied this code).
mcimadamore@1393 14 *
mcimadamore@1393 15 * You should have received a copy of the GNU General Public License version
mcimadamore@1393 16 * 2 along with this work; if not, write to the Free Software Foundation,
mcimadamore@1393 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
mcimadamore@1393 18 *
mcimadamore@1393 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
mcimadamore@1393 20 * or visit www.oracle.com if you need additional information or have any
mcimadamore@1393 21 * questions.
mcimadamore@1393 22 */
mcimadamore@1393 23
mcimadamore@1393 24 /*
mcimadamore@1393 25 * @test
jfranck@2020 26 * @bug 7192246
mcimadamore@1393 27 * @summary check that code attributed for default methods is correctly generated
mcimadamore@1393 28 */
mcimadamore@1393 29
mcimadamore@1393 30 import com.sun.tools.classfile.AccessFlags;
mcimadamore@1393 31 import com.sun.tools.classfile.Attribute;
mcimadamore@1393 32 import com.sun.tools.classfile.ClassFile;
mcimadamore@1393 33 import com.sun.tools.classfile.ConstantPool.*;
mcimadamore@1393 34 import com.sun.tools.classfile.Code_attribute;
mcimadamore@1393 35 import com.sun.tools.classfile.Instruction;
mcimadamore@1393 36 import com.sun.tools.classfile.Method;
mcimadamore@1393 37
mcimadamore@1393 38 import com.sun.tools.classfile.Opcode;
mcimadamore@1393 39 import java.io.*;
mcimadamore@1393 40
mcimadamore@1393 41 public class TestDefaultBody {
mcimadamore@1393 42
mcimadamore@1393 43 interface TestInterface {
mcimadamore@1393 44 int no_default(int i);
mcimadamore@1393 45 default int yes_default(int i) { return impl(this, i); }
mcimadamore@1393 46 }
mcimadamore@1393 47
mcimadamore@1393 48 static int impl(TestInterface ti, int i) { return 0; }
mcimadamore@1393 49
mcimadamore@1393 50 static final String TARGET_CLASS_NAME = "TestDefaultBody";
mcimadamore@1393 51 static final String TARGET_NAME = "impl";
mcimadamore@1393 52 static final String TARGET_TYPE = "(LTestDefaultBody$TestInterface;I)I";
mcimadamore@1393 53 static final String SUBTEST_NAME = TestInterface.class.getName() + ".class";
mcimadamore@1393 54 static final String TEST_METHOD_NAME = "yes_default";
mcimadamore@1393 55
mcimadamore@1393 56 public static void main(String... args) throws Exception {
mcimadamore@1393 57 new TestDefaultBody().run();
mcimadamore@1393 58 }
mcimadamore@1393 59
mcimadamore@1393 60 public void run() throws Exception {
mcimadamore@1393 61 String workDir = System.getProperty("test.classes");
mcimadamore@1393 62 File compiledTest = new File(workDir, SUBTEST_NAME);
mcimadamore@1393 63 verifyDefaultBody(compiledTest);
mcimadamore@1393 64 }
mcimadamore@1393 65
mcimadamore@1393 66 void verifyDefaultBody(File f) {
mcimadamore@1393 67 System.err.println("verify: " + f);
mcimadamore@1393 68 try {
mcimadamore@1393 69 ClassFile cf = ClassFile.read(f);
mcimadamore@1393 70 Method testMethod = null;
mcimadamore@1393 71 Code_attribute codeAttr = null;
mcimadamore@1393 72 for (Method m : cf.methods) {
mcimadamore@1393 73 codeAttr = (Code_attribute)m.attributes.get(Attribute.Code);
mcimadamore@1393 74 String mname = m.getName(cf.constant_pool);
mcimadamore@1393 75 if (mname.equals(TEST_METHOD_NAME)) {
mcimadamore@1393 76 testMethod = m;
mcimadamore@1393 77 break;
mcimadamore@1393 78 } else {
mcimadamore@1393 79 codeAttr = null;
mcimadamore@1393 80 }
mcimadamore@1393 81 }
mcimadamore@1393 82 if (testMethod == null) {
mcimadamore@1393 83 throw new Error("Test method not found");
mcimadamore@1393 84 }
mcimadamore@1393 85 if (testMethod.access_flags.is(AccessFlags.ACC_ABSTRACT)) {
mcimadamore@1393 86 throw new Error("Test method is abstract");
mcimadamore@1393 87 }
mcimadamore@1393 88 if (codeAttr == null) {
mcimadamore@1393 89 throw new Error("Code attribute in test method not found");
mcimadamore@1393 90 }
mcimadamore@1393 91
mcimadamore@1393 92 boolean found = false;
mcimadamore@1393 93 for (Instruction instr : codeAttr.getInstructions()) {
mcimadamore@1393 94 if (instr.getOpcode() == Opcode.INVOKESTATIC) {
mcimadamore@1393 95 found = true;
mcimadamore@1393 96 int pc_index = instr.getShort(1);
mcimadamore@1393 97 CONSTANT_Methodref_info mref = (CONSTANT_Methodref_info)cf.constant_pool.get(pc_index);
mcimadamore@1393 98 String className = mref.getClassName();
mcimadamore@1393 99 String targetName = mref.getNameAndTypeInfo().getName();
mcimadamore@1393 100 String targetType = mref.getNameAndTypeInfo().getType();
mcimadamore@1393 101
mcimadamore@1393 102 if (!className.equals(TARGET_CLASS_NAME)) {
mcimadamore@1393 103 throw new Error("unexpected class in default method body " + className);
mcimadamore@1393 104 }
mcimadamore@1393 105 if (!targetName.equals(TARGET_NAME)) {
mcimadamore@1393 106 throw new Error("unexpected method name in default method body " + targetName);
mcimadamore@1393 107 }
mcimadamore@1393 108 if (!targetType.equals(TARGET_TYPE)) {
mcimadamore@1393 109 throw new Error("unexpected method type in default method body " + targetType);
mcimadamore@1393 110 }
mcimadamore@1393 111 break;
mcimadamore@1393 112 }
mcimadamore@1393 113 }
mcimadamore@1393 114
mcimadamore@1393 115 if (!found) {
mcimadamore@1393 116 throw new Error("no invokestatic found in default method body");
mcimadamore@1393 117 }
mcimadamore@1393 118 } catch (Exception e) {
mcimadamore@1393 119 e.printStackTrace();
mcimadamore@1393 120 throw new Error("error reading " + f +": " + e);
mcimadamore@1393 121 }
mcimadamore@1393 122 }
mcimadamore@1393 123 }

mercurial