test/tools/javac/defaultMethods/TestDefaultBody.java

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

mercurial