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