|
1 /* |
|
2 * Copyright 2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 import java.io.*; |
|
25 import com.sun.tools.classfile.*; |
|
26 |
|
27 /* |
|
28 * @test ArrayClassLiterals |
|
29 * @summary test that class literals array doesn't crash javap |
|
30 */ |
|
31 |
|
32 public class ArrayClassLiterals { |
|
33 public static void main(String[] args) throws Exception { |
|
34 new ArrayClassLiterals().run(); |
|
35 } |
|
36 |
|
37 public void run() throws Exception { |
|
38 File javaFile = writeTestFile(); |
|
39 File classFile = compileTestFile(javaFile); |
|
40 |
|
41 ClassFile cf = ClassFile.read(classFile); |
|
42 test(cf); |
|
43 for (Field f : cf.fields) { |
|
44 test(cf, f); |
|
45 } |
|
46 for (Method m: cf.methods) { |
|
47 test(cf, m); |
|
48 } |
|
49 |
|
50 countAnnotations(); |
|
51 |
|
52 if (errors > 0) |
|
53 throw new Exception(errors + " errors found"); |
|
54 System.out.println("PASSED"); |
|
55 } |
|
56 |
|
57 void test(ClassFile cf) { |
|
58 test(cf, Attribute.RuntimeVisibleTypeAnnotations, true); |
|
59 test(cf, Attribute.RuntimeInvisibleTypeAnnotations, false); |
|
60 } |
|
61 |
|
62 void test(ClassFile cf, Method m) { |
|
63 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); |
|
64 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); |
|
65 } |
|
66 |
|
67 void test(ClassFile cf, Field m) { |
|
68 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true); |
|
69 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false); |
|
70 } |
|
71 |
|
72 // test the result of Attributes.getIndex according to expectations |
|
73 // encoded in the method's name |
|
74 void test(ClassFile cf, String name, boolean visible) { |
|
75 int index = cf.attributes.getIndex(cf.constant_pool, name); |
|
76 if (index != -1) { |
|
77 Attribute attr = cf.attributes.get(index); |
|
78 assert attr instanceof RuntimeTypeAnnotations_attribute; |
|
79 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; |
|
80 all += tAttr.annotations.length; |
|
81 if (visible) |
|
82 visibles += tAttr.annotations.length; |
|
83 else |
|
84 invisibles += tAttr.annotations.length; |
|
85 |
|
86 for (ExtendedAnnotation anno : tAttr.annotations) |
|
87 anno.position.toString(); |
|
88 } |
|
89 } |
|
90 |
|
91 // test the result of Attributes.getIndex according to expectations |
|
92 // encoded in the method's name |
|
93 void test(ClassFile cf, Method m, String name, boolean visible) { |
|
94 int index = m.attributes.getIndex(cf.constant_pool, name); |
|
95 if (index != -1) { |
|
96 Attribute attr = m.attributes.get(index); |
|
97 assert attr instanceof RuntimeTypeAnnotations_attribute; |
|
98 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; |
|
99 all += tAttr.annotations.length; |
|
100 if (visible) |
|
101 visibles += tAttr.annotations.length; |
|
102 else |
|
103 invisibles += tAttr.annotations.length; |
|
104 |
|
105 for (ExtendedAnnotation anno : tAttr.annotations) |
|
106 anno.position.toString(); |
|
107 } |
|
108 } |
|
109 |
|
110 // test the result of Attributes.getIndex according to expectations |
|
111 // encoded in the method's name |
|
112 void test(ClassFile cf, Field m, String name, boolean visible) { |
|
113 int index = m.attributes.getIndex(cf.constant_pool, name); |
|
114 if (index != -1) { |
|
115 Attribute attr = m.attributes.get(index); |
|
116 assert attr instanceof RuntimeTypeAnnotations_attribute; |
|
117 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr; |
|
118 all += tAttr.annotations.length; |
|
119 if (visible) |
|
120 visibles += tAttr.annotations.length; |
|
121 else |
|
122 invisibles += tAttr.annotations.length; |
|
123 |
|
124 for (ExtendedAnnotation anno : tAttr.annotations) |
|
125 anno.position.toString(); |
|
126 } |
|
127 } |
|
128 |
|
129 File writeTestFile() throws IOException { |
|
130 File f = new File("Testa.java"); |
|
131 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); |
|
132 out.println("import java.util.*;"); |
|
133 out.println("class Testa { "); |
|
134 out.println(" @interface A { }"); |
|
135 |
|
136 out.println(" void test() {"); |
|
137 out.println(" Object a = @A String.class;"); |
|
138 out.println(" Object b = @A String @A [] @A [].class;"); |
|
139 out.println(" }"); |
|
140 out.println("}"); |
|
141 |
|
142 out.close(); |
|
143 return f; |
|
144 } |
|
145 |
|
146 File compileTestFile(File f) { |
|
147 int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.7", "-g", f.getPath() }); |
|
148 if (rc != 0) |
|
149 throw new Error("compilation failed. rc=" + rc); |
|
150 String path = f.getPath(); |
|
151 return new File(path.substring(0, path.length() - 5) + ".class"); |
|
152 } |
|
153 |
|
154 void countAnnotations() { |
|
155 int expected_visibles = 0, expected_invisibles = 4; |
|
156 int expected_all = expected_visibles + expected_invisibles; |
|
157 |
|
158 if (expected_all != all) { |
|
159 errors++; |
|
160 System.err.println("expected " + expected_all |
|
161 + " annotations but found " + all); |
|
162 } |
|
163 |
|
164 if (expected_visibles != visibles) { |
|
165 errors++; |
|
166 System.err.println("expected " + expected_visibles |
|
167 + " visibles annotations but found " + visibles); |
|
168 } |
|
169 |
|
170 if (expected_invisibles != invisibles) { |
|
171 errors++; |
|
172 System.err.println("expected " + expected_invisibles |
|
173 + " invisibles annotations but found " + invisibles); |
|
174 } |
|
175 |
|
176 } |
|
177 |
|
178 int errors; |
|
179 int all; |
|
180 int visibles; |
|
181 int invisibles; |
|
182 } |