15385
|
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 java.lang.annotation.Retention;
|
|
26 |
import java.lang.annotation.RetentionPolicy;
|
|
27 |
import java.net.URL;
|
|
28 |
import java.util.List;
|
|
29 |
|
|
30 |
import com.sun.tools.classfile.*;
|
|
31 |
|
|
32 |
/*
|
|
33 |
* @test NoTargetAnnotations
|
|
34 |
* @summary test that annotations with no Target meta type is emitted
|
|
35 |
* only once as declaration annotation
|
|
36 |
*/
|
|
37 |
public class NoTargetAnnotations {
|
|
38 |
|
|
39 |
public static void main(String[] args) throws Exception {
|
|
40 |
new NoTargetAnnotations().run();
|
|
41 |
}
|
|
42 |
|
|
43 |
public void run() throws Exception {
|
|
44 |
ClassFile cf = getClassFile("NoTargetAnnotations$Test.class");
|
|
45 |
for (Field f : cf.fields) {
|
|
46 |
test(cf, f);
|
|
47 |
testDeclaration(cf, f);
|
|
48 |
}
|
|
49 |
for (Method m: cf.methods) {
|
|
50 |
test(cf, m);
|
|
51 |
testDeclaration(cf, m);
|
|
52 |
}
|
|
53 |
|
|
54 |
countAnnotations();
|
|
55 |
|
|
56 |
if (errors > 0)
|
|
57 |
throw new Exception(errors + " errors found");
|
|
58 |
System.out.println("PASSED");
|
|
59 |
}
|
|
60 |
|
|
61 |
ClassFile getClassFile(String name) throws IOException, ConstantPoolException {
|
|
62 |
URL url = getClass().getResource(name);
|
|
63 |
InputStream in = url.openStream();
|
|
64 |
try {
|
|
65 |
return ClassFile.read(in);
|
|
66 |
} finally {
|
|
67 |
in.close();
|
|
68 |
}
|
|
69 |
}
|
|
70 |
|
|
71 |
/************ Helper annotations counting methods ******************/
|
|
72 |
void test(ClassFile cf, Method m) {
|
|
73 |
test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
|
|
74 |
test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
|
|
75 |
}
|
|
76 |
|
|
77 |
void test(ClassFile cf, Field m) {
|
|
78 |
test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
|
|
79 |
test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
|
|
80 |
}
|
|
81 |
|
|
82 |
void testDeclaration(ClassFile cf, Method m) {
|
|
83 |
testDecl(cf, m, Attribute.RuntimeVisibleAnnotations, true);
|
|
84 |
testDecl(cf, m, Attribute.RuntimeInvisibleAnnotations, false);
|
|
85 |
}
|
|
86 |
|
|
87 |
void testDeclaration(ClassFile cf, Field m) {
|
|
88 |
testDecl(cf, m, Attribute.RuntimeVisibleAnnotations, true);
|
|
89 |
testDecl(cf, m, Attribute.RuntimeInvisibleAnnotations, false);
|
|
90 |
}
|
|
91 |
|
|
92 |
// test the result of Attributes.getIndex according to expectations
|
|
93 |
// encoded in the method's name
|
|
94 |
void test(ClassFile cf, Method m, String name, boolean visible) {
|
|
95 |
int index = m.attributes.getIndex(cf.constant_pool, name);
|
|
96 |
if (index != -1) {
|
|
97 |
Attribute attr = m.attributes.get(index);
|
|
98 |
assert attr instanceof RuntimeTypeAnnotations_attribute;
|
|
99 |
RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
|
|
100 |
all += tAttr.annotations.length;
|
|
101 |
if (visible)
|
|
102 |
visibles += tAttr.annotations.length;
|
|
103 |
else
|
|
104 |
invisibles += tAttr.annotations.length;
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
// test the result of Attributes.getIndex according to expectations
|
|
109 |
// encoded in the method's name
|
|
110 |
void test(ClassFile cf, Field m, String name, boolean visible) {
|
|
111 |
int index = m.attributes.getIndex(cf.constant_pool, name);
|
|
112 |
if (index != -1) {
|
|
113 |
Attribute attr = m.attributes.get(index);
|
|
114 |
assert attr instanceof RuntimeTypeAnnotations_attribute;
|
|
115 |
RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
|
|
116 |
all += tAttr.annotations.length;
|
|
117 |
if (visible)
|
|
118 |
visibles += tAttr.annotations.length;
|
|
119 |
else
|
|
120 |
invisibles += tAttr.annotations.length;
|
|
121 |
}
|
|
122 |
}
|
|
123 |
|
|
124 |
// test the result of Attributes.getIndex according to expectations
|
|
125 |
// encoded in the method's name
|
|
126 |
void testDecl(ClassFile cf, Method m, String name, boolean visible) {
|
|
127 |
int index = m.attributes.getIndex(cf.constant_pool, name);
|
|
128 |
if (index != -1) {
|
|
129 |
Attribute attr = m.attributes.get(index);
|
|
130 |
assert attr instanceof RuntimeAnnotations_attribute;
|
|
131 |
RuntimeAnnotations_attribute tAttr = (RuntimeAnnotations_attribute)attr;
|
|
132 |
this.declAnnotations += tAttr.annotations.length;
|
|
133 |
}
|
|
134 |
}
|
|
135 |
|
|
136 |
// test the result of Attributes.getIndex according to expectations
|
|
137 |
// encoded in the method's name
|
|
138 |
void testDecl(ClassFile cf, Field m, String name, boolean visible) {
|
|
139 |
int index = m.attributes.getIndex(cf.constant_pool, name);
|
|
140 |
if (index != -1) {
|
|
141 |
Attribute attr = m.attributes.get(index);
|
|
142 |
assert attr instanceof RuntimeAnnotations_attribute;
|
|
143 |
RuntimeAnnotations_attribute tAttr = (RuntimeAnnotations_attribute)attr;
|
|
144 |
this.declAnnotations += tAttr.annotations.length;
|
|
145 |
}
|
|
146 |
}
|
|
147 |
|
|
148 |
File compileTestFile(File f) {
|
|
149 |
int rc = com.sun.tools.javac.Main.compile(new String[] { "-XDTA:writer", "-source", "1.8", "-g", f.getPath() });
|
|
150 |
if (rc != 0)
|
|
151 |
throw new Error("compilation failed. rc=" + rc);
|
|
152 |
String path = f.getPath();
|
|
153 |
return new File(path.substring(0, path.length() - 5) + ".class");
|
|
154 |
}
|
|
155 |
|
|
156 |
void countAnnotations() {
|
|
157 |
int expected_all = expected_visibles + expected_invisibles;
|
|
158 |
|
|
159 |
if (expected_all != all) {
|
|
160 |
errors++;
|
|
161 |
System.err.println("expected " + expected_all
|
|
162 |
+ " annotations but found " + all);
|
|
163 |
}
|
|
164 |
|
|
165 |
if (expected_visibles != visibles) {
|
|
166 |
errors++;
|
|
167 |
System.err.println("expected " + expected_visibles
|
|
168 |
+ " visibles annotations but found " + visibles);
|
|
169 |
}
|
|
170 |
|
|
171 |
if (expected_invisibles != invisibles) {
|
|
172 |
errors++;
|
|
173 |
System.err.println("expected " + expected_invisibles
|
|
174 |
+ " invisibles annotations but found " + invisibles);
|
|
175 |
}
|
|
176 |
|
|
177 |
if (expected_decl != declAnnotations) {
|
|
178 |
errors++;
|
|
179 |
System.err.println("expected " + expected_decl
|
|
180 |
+ " declaration annotations but found " + declAnnotations);
|
|
181 |
}
|
|
182 |
}
|
|
183 |
|
|
184 |
int errors;
|
|
185 |
int all;
|
|
186 |
int visibles;
|
|
187 |
int invisibles;
|
|
188 |
|
|
189 |
int declAnnotations;
|
|
190 |
|
|
191 |
/*********************** Test class *************************/
|
|
192 |
static int expected_invisibles = 0;
|
|
193 |
static int expected_visibles = 0;
|
|
194 |
static int expected_decl = 1;
|
|
195 |
|
|
196 |
static class Test {
|
|
197 |
@Retention(RetentionPolicy.RUNTIME)
|
|
198 |
@interface A {}
|
|
199 |
|
|
200 |
@A String method() {
|
|
201 |
return null;
|
|
202 |
}
|
|
203 |
}
|
|
204 |
}
|