author | goetz |
Wed, 27 Jan 2016 10:35:49 +0100 | |
changeset 35775 | 00710c1b504a |
parent 30846 | 2b3f379840f0 |
permissions | -rw-r--r-- |
861
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
1 |
/* |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
5520
diff
changeset
|
2 |
* Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved. |
861
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
4 |
* |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
8 |
* |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
13 |
* accompanied this code). |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
14 |
* |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
18 |
* |
5520 | 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. |
|
861
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
22 |
*/ |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
23 |
|
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
24 |
/* |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
25 |
* @test 6716452 |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
26 |
* @summary need a method to get an index of an attribute |
30846 | 27 |
* @modules jdk.jdeps/com.sun.tools.classfile |
861
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
28 |
*/ |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
29 |
|
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
30 |
import java.io.*; |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
31 |
import com.sun.tools.classfile.*; |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
32 |
|
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
33 |
public class T6716452 { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
34 |
public static void main(String[] args) throws Exception { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
35 |
new T6716452().run(); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
36 |
} |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
37 |
|
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
38 |
public void run() throws Exception { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
39 |
File javaFile = writeTestFile(); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
40 |
File classFile = compileTestFile(javaFile); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
41 |
|
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
42 |
ClassFile cf = ClassFile.read(classFile); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
43 |
for (Method m: cf.methods) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
44 |
test(cf, m); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
45 |
} |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
46 |
|
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
47 |
if (errors > 0) |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
48 |
throw new Exception(errors + " errors found"); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
49 |
} |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
50 |
|
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
51 |
void test(ClassFile cf, Method m) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
52 |
test(cf, m, Attribute.Code, Code_attribute.class); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
53 |
test(cf, m, Attribute.Exceptions, Exceptions_attribute.class); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
54 |
} |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
55 |
|
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
56 |
// test the result of Attributes.getIndex according to expectations |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
57 |
// encoded in the method's name |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
58 |
void test(ClassFile cf, Method m, String name, Class<?> c) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
59 |
int index = m.attributes.getIndex(cf.constant_pool, name); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
60 |
try { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
61 |
String m_name = m.getName(cf.constant_pool); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
62 |
System.err.println("Method " + m_name + " name:" + name + " index:" + index + " class: " + c); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
63 |
boolean expect = (m_name.equals("<init>") && name.equals("Code")) |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
64 |
|| (m_name.indexOf(name) != -1); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
65 |
boolean found = (index != -1); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
66 |
if (expect) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
67 |
if (found) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
68 |
Attribute attr = m.attributes.get(index); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
69 |
if (!c.isAssignableFrom(attr.getClass())) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
70 |
error(m + ": unexpected attribute found," |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
71 |
+ " expected " + c.getName() |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
72 |
+ " found " + attr.getClass().getName()); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
73 |
} |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
74 |
} else { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
75 |
error(m + ": expected attribute " + name + " not found"); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
76 |
} |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
77 |
} else { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
78 |
if (found) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
79 |
error(m + ": unexpected attribute " + name); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
80 |
} |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
81 |
} |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
82 |
} catch (ConstantPoolException e) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
83 |
error(m + ": " + e); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
84 |
} |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
85 |
} |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
86 |
|
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
87 |
File writeTestFile() throws IOException { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
88 |
File f = new File("Test.java"); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
89 |
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
90 |
out.println("abstract class Test { "); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
91 |
out.println(" abstract void m();"); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
92 |
out.println(" void m_Code() { }"); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
93 |
out.println(" abstract void m_Exceptions() throws Exception;"); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
94 |
out.println(" void m_Code_Exceptions() throws Exception { }"); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
95 |
out.println("}"); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
96 |
out.close(); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
97 |
return f; |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
98 |
} |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
99 |
|
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
100 |
File compileTestFile(File f) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
101 |
int rc = com.sun.tools.javac.Main.compile(new String[] { "-g", f.getPath() }); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
102 |
if (rc != 0) |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
103 |
throw new Error("compilation failed. rc=" + rc); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
104 |
String path = f.getPath(); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
105 |
return new File(path.substring(0, path.length() - 5) + ".class"); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
106 |
} |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
107 |
|
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
108 |
void error(String msg) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
109 |
System.err.println("error: " + msg); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
110 |
errors++; |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
111 |
} |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
112 |
|
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
113 |
int errors; |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
114 |
} |