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 Presence |
|
29 * @bug 6843077 |
|
30 * @summary test that all type annotations are present in the classfile |
|
31 */ |
|
32 |
|
33 public class Presence { |
|
34 public static void main(String[] args) throws Exception { |
|
35 new Presence().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("Test.java"); |
|
123 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); |
|
124 out.println("import java.util.*;"); |
|
125 out.println("class Test<@Test.A T extends @Test.A List<@Test.A String>> { "); |
|
126 out.println(" @interface A { }"); |
|
127 |
|
128 out.println(" Map<@A String, Map<@A String, @A String>> f1;"); |
|
129 |
|
130 out.println(" <@A T extends @A List<@A String>>"); |
|
131 out.println(" Map<@A String, @A List<@A String>>"); |
|
132 out.println(" method(List<@A String> @A [] param1, String @A [] @A ... param2) @A"); |
|
133 out.println(" throws @A Exception {"); |
|
134 out.println(" @A String lc1 = null;"); |
|
135 out.println(" @A List<@A String> lc2 = null;"); |
|
136 out.println(" @A String @A [] [] @A[] lc3 = null;"); |
|
137 out.println(" List<? extends @A List<@A String>> lc4 = null;"); |
|
138 out.println(" Object lc5 = (@A List<@A String>) null;"); |
|
139 out.println(" boolean lc6 = lc1 instanceof @A String;"); |
|
140 out.println(" boolean lc7 = lc5 instanceof @A String @A [] @A [];"); |
|
141 out.println(" new @A ArrayList<@A String>();"); |
|
142 out.println(" Object lc8 = new @A String @A [4];"); |
|
143 out.println(" Object lc9 = @A String.class;"); |
|
144 out.println(" Object lc10 = @A int.class;"); |
|
145 out.println(" return null;"); |
|
146 out.println(" }"); |
|
147 out.println(" void vararg1(String @A ... t) { } "); |
|
148 out.println("}"); |
|
149 out.close(); |
|
150 return f; |
|
151 } |
|
152 |
|
153 File compileTestFile(File f) { |
|
154 int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.7", "-g", f.getPath() }); |
|
155 if (rc != 0) |
|
156 throw new Error("compilation failed. rc=" + rc); |
|
157 String path = f.getPath(); |
|
158 return new File(path.substring(0, path.length() - 5) + ".class"); |
|
159 } |
|
160 |
|
161 void countAnnotations() { |
|
162 int expected_visibles = 0, expected_invisibles = 39; |
|
163 int expected_all = expected_visibles + expected_invisibles; |
|
164 |
|
165 if (expected_all != all) { |
|
166 errors++; |
|
167 System.err.println("expected " + expected_all |
|
168 + " annotations but found " + all); |
|
169 } |
|
170 |
|
171 if (expected_visibles != visibles) { |
|
172 errors++; |
|
173 System.err.println("expected " + expected_visibles |
|
174 + " visibles annotations but found " + visibles); |
|
175 } |
|
176 |
|
177 if (expected_invisibles != invisibles) { |
|
178 errors++; |
|
179 System.err.println("expected " + expected_invisibles |
|
180 + " invisibles annotations but found " + invisibles); |
|
181 } |
|
182 |
|
183 } |
|
184 |
|
185 int errors; |
|
186 int all; |
|
187 int visibles; |
|
188 int invisibles; |
|
189 } |
|