15385
|
1 |
/*
|
|
2 |
* Copyright (c) 2008, 2013, 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 java.lang.annotation.ElementType;
|
|
26 |
|
|
27 |
import com.sun.tools.classfile.*;
|
|
28 |
|
|
29 |
/*
|
|
30 |
* @test Presence
|
|
31 |
* @bug 6843077
|
|
32 |
* @summary test that all type annotations are present in the classfile
|
|
33 |
*/
|
|
34 |
|
|
35 |
public class Presence {
|
|
36 |
public static void main(String[] args) throws Exception {
|
|
37 |
new Presence().run();
|
|
38 |
}
|
|
39 |
|
|
40 |
public void run() throws Exception {
|
|
41 |
File javaFile = writeTestFile();
|
|
42 |
File classFile = compileTestFile(javaFile);
|
|
43 |
|
|
44 |
ClassFile cf = ClassFile.read(classFile);
|
|
45 |
test(cf);
|
|
46 |
for (Field f : cf.fields) {
|
|
47 |
test(cf, f);
|
|
48 |
}
|
|
49 |
for (Method m: cf.methods) {
|
|
50 |
test(cf, m);
|
|
51 |
}
|
|
52 |
|
|
53 |
countAnnotations();
|
|
54 |
|
|
55 |
if (errors > 0)
|
|
56 |
throw new Exception(errors + " errors found");
|
|
57 |
System.out.println("PASSED");
|
|
58 |
}
|
|
59 |
|
|
60 |
void test(ClassFile cf) {
|
|
61 |
test(cf, Attribute.RuntimeVisibleTypeAnnotations, true);
|
|
62 |
test(cf, Attribute.RuntimeInvisibleTypeAnnotations, false);
|
|
63 |
}
|
|
64 |
|
|
65 |
void test(ClassFile cf, Method m) {
|
|
66 |
test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
|
|
67 |
test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
|
|
68 |
}
|
|
69 |
|
|
70 |
void test(ClassFile cf, Field m) {
|
|
71 |
test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
|
|
72 |
test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
|
|
73 |
}
|
|
74 |
|
|
75 |
// test the result of Attributes.getIndex according to expectations
|
|
76 |
// encoded in the method's name
|
|
77 |
void test(ClassFile cf, String name, boolean visible) {
|
|
78 |
int index = cf.attributes.getIndex(cf.constant_pool, name);
|
|
79 |
if (index != -1) {
|
|
80 |
Attribute attr = cf.attributes.get(index);
|
|
81 |
assert attr instanceof RuntimeTypeAnnotations_attribute;
|
|
82 |
RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
|
|
83 |
all += tAttr.annotations.length;
|
|
84 |
if (visible)
|
|
85 |
visibles += tAttr.annotations.length;
|
|
86 |
else
|
|
87 |
invisibles += tAttr.annotations.length;
|
|
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 |
}
|
|
106 |
|
|
107 |
// test the result of Attributes.getIndex according to expectations
|
|
108 |
// encoded in the method's name
|
|
109 |
void test(ClassFile cf, Field m, String name, boolean visible) {
|
|
110 |
int index = m.attributes.getIndex(cf.constant_pool, name);
|
|
111 |
if (index != -1) {
|
|
112 |
Attribute attr = m.attributes.get(index);
|
|
113 |
assert attr instanceof RuntimeTypeAnnotations_attribute;
|
|
114 |
RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
|
|
115 |
all += tAttr.annotations.length;
|
|
116 |
if (visible)
|
|
117 |
visibles += tAttr.annotations.length;
|
|
118 |
else
|
|
119 |
invisibles += tAttr.annotations.length;
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
File writeTestFile() throws IOException {
|
|
124 |
File f = new File("Test.java");
|
|
125 |
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
|
|
126 |
out.println("import java.util.*;");
|
|
127 |
out.println("import java.lang.annotation.*;");
|
|
128 |
|
|
129 |
out.println("class Test<@Test.A T extends @Test.A List<@Test.A String>> { ");
|
|
130 |
out.println(" @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})");
|
|
131 |
out.println(" @interface A { }");
|
|
132 |
|
|
133 |
out.println(" Map<@A String, Map<@A String, @A String>> f1;");
|
|
134 |
|
|
135 |
out.println(" <@A TM extends @A List<@A String>>");
|
|
136 |
out.println(" Map<@A String, @A List<@A String>>");
|
|
137 |
out.println(" method(@A Test<T> this, List<@A String> @A [] param1, String @A [] @A ... param2)");
|
|
138 |
out.println(" throws @A Exception {");
|
|
139 |
out.println(" @A String lc1 = null;");
|
|
140 |
out.println(" @A List<@A String> lc2 = null;");
|
|
141 |
out.println(" @A String @A [] [] @A[] lc3 = null;");
|
|
142 |
out.println(" List<? extends @A List<@A String>> lc4 = null;");
|
|
143 |
out.println(" Object lc5 = (@A List<@A String>) null;");
|
|
144 |
out.println(" boolean lc6 = lc1 instanceof @A String;");
|
|
145 |
out.println(" boolean lc7 = lc5 instanceof @A String @A [] @A [];");
|
|
146 |
out.println(" new @A ArrayList<@A String>();");
|
|
147 |
out.println(" Object lc8 = new @A String @A [4];");
|
|
148 |
out.println(" try {");
|
|
149 |
out.println(" Object lc10 = int.class;");
|
|
150 |
out.println(" } catch (@A Exception e) { e.toString(); }");
|
|
151 |
out.println(" return null;");
|
|
152 |
out.println(" }");
|
|
153 |
out.println(" void vararg1(String @A ... t) { } ");
|
|
154 |
out.println("}");
|
|
155 |
out.close();
|
|
156 |
return f;
|
|
157 |
}
|
|
158 |
|
|
159 |
File compileTestFile(File f) {
|
|
160 |
int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.8", "-g", f.getPath() });
|
|
161 |
if (rc != 0)
|
|
162 |
throw new Error("compilation failed. rc=" + rc);
|
|
163 |
String path = f.getPath();
|
|
164 |
return new File(path.substring(0, path.length() - 5) + ".class");
|
|
165 |
}
|
|
166 |
|
|
167 |
void countAnnotations() {
|
|
168 |
int expected_visibles = 0, expected_invisibles = 38;
|
|
169 |
int expected_all = expected_visibles + expected_invisibles;
|
|
170 |
|
|
171 |
if (expected_all != all) {
|
|
172 |
errors++;
|
|
173 |
System.err.println("expected " + expected_all
|
|
174 |
+ " annotations but found " + all);
|
|
175 |
}
|
|
176 |
|
|
177 |
if (expected_visibles != visibles) {
|
|
178 |
errors++;
|
|
179 |
System.err.println("expected " + expected_visibles
|
|
180 |
+ " visibles annotations but found " + visibles);
|
|
181 |
}
|
|
182 |
|
|
183 |
if (expected_invisibles != invisibles) {
|
|
184 |
errors++;
|
|
185 |
System.err.println("expected " + expected_invisibles
|
|
186 |
+ " invisibles annotations but found " + invisibles);
|
|
187 |
}
|
|
188 |
|
|
189 |
}
|
|
190 |
|
|
191 |
int errors;
|
|
192 |
int all;
|
|
193 |
int visibles;
|
|
194 |
int invisibles;
|
|
195 |
}
|