author | katleman |
Thu, 21 Aug 2014 14:16:14 -0700 | |
changeset 25878 | 6d561031123e |
parent 5520 | 86e4b9a9da40 |
child 30730 | d3ce7619db2c |
permissions | -rw-r--r-- |
861
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
1 |
/* |
5520 | 2 |
* Copyright (c) 2008, 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 |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
27 |
*/ |
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 |
import java.io.*; |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
30 |
import com.sun.tools.classfile.*; |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
31 |
|
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
32 |
public class T6716452 { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
33 |
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
|
34 |
new T6716452().run(); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
35 |
} |
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 |
public void run() throws Exception { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
38 |
File javaFile = writeTestFile(); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
39 |
File classFile = compileTestFile(javaFile); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
40 |
|
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
41 |
ClassFile cf = ClassFile.read(classFile); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
42 |
for (Method m: cf.methods) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
43 |
test(cf, m); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
44 |
} |
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 |
if (errors > 0) |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
47 |
throw new Exception(errors + " errors found"); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
48 |
} |
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 |
void test(ClassFile cf, Method m) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
51 |
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
|
52 |
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
|
53 |
} |
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 |
// 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
|
56 |
// encoded in the method's name |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
57 |
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
|
58 |
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
|
59 |
try { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
60 |
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
|
61 |
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
|
62 |
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
|
63 |
|| (m_name.indexOf(name) != -1); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
64 |
boolean found = (index != -1); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
65 |
if (expect) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
66 |
if (found) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
67 |
Attribute attr = m.attributes.get(index); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
68 |
if (!c.isAssignableFrom(attr.getClass())) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
69 |
error(m + ": unexpected attribute found," |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
70 |
+ " expected " + c.getName() |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
71 |
+ " found " + attr.getClass().getName()); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
72 |
} |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
73 |
} else { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
74 |
error(m + ": expected attribute " + name + " not found"); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
75 |
} |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
76 |
} else { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
77 |
if (found) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
78 |
error(m + ": unexpected attribute " + name); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
79 |
} |
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 |
} catch (ConstantPoolException e) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
82 |
error(m + ": " + e); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
83 |
} |
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 |
File writeTestFile() throws IOException { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
87 |
File f = new File("Test.java"); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
88 |
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
|
89 |
out.println("abstract class Test { "); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
90 |
out.println(" abstract void m();"); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
91 |
out.println(" void m_Code() { }"); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
92 |
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
|
93 |
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
|
94 |
out.println("}"); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
95 |
out.close(); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
96 |
return f; |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
97 |
} |
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 |
File compileTestFile(File f) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
100 |
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
|
101 |
if (rc != 0) |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
102 |
throw new Error("compilation failed. rc=" + rc); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
103 |
String path = f.getPath(); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
104 |
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
|
105 |
} |
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 |
void error(String msg) { |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
108 |
System.err.println("error: " + msg); |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
109 |
errors++; |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
110 |
} |
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 |
int errors; |
fb540143ce11
6716452: (classfile) need a method to get the index of an attribute
jjg
parents:
diff
changeset
|
113 |
} |