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