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