author | darcy |
Thu, 31 Jan 2013 18:58:17 -0800 | |
changeset 15557 | 3e211bbf4094 |
parent 15385 | ee1eebe7e210 |
child 17578 | 46ac954e4a84 |
permissions | -rw-r--r-- |
15385 | 1 |
/* |
15557
3e211bbf4094
8007351: Malformed copyright statements in typeAnnotations test directory
darcy
parents:
15385
diff
changeset
|
2 |
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
15385 | 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.lang.annotation.*; |
|
25 |
import java.io.*; |
|
26 |
import java.net.URL; |
|
27 |
import java.util.List; |
|
28 |
||
29 |
import com.sun.tools.classfile.*; |
|
30 |
||
31 |
public class ClassfileTestHelper { |
|
32 |
int expected_tinvisibles = 0; |
|
33 |
int expected_tvisibles = 0; |
|
34 |
int expected_invisibles = 0; |
|
35 |
int expected_visibles = 0; |
|
36 |
||
37 |
//Makes debugging much easier. Set to 'false' for less output. |
|
38 |
public Boolean verbose = true; |
|
39 |
void println(String msg) { if(verbose) System.out.println(msg); } |
|
40 |
||
41 |
File writeTestFile(String fname, String source) throws IOException { |
|
42 |
File f = new File(fname); |
|
43 |
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); |
|
44 |
out.println(source); |
|
45 |
out.close(); |
|
46 |
return f; |
|
47 |
} |
|
48 |
||
49 |
File compile(File f) { |
|
50 |
int rc = com.sun.tools.javac.Main.compile(new String[] { |
|
51 |
"-source", "1.8", "-g", f.getPath() }); |
|
52 |
if (rc != 0) |
|
53 |
throw new Error("compilation failed. rc=" + rc); |
|
54 |
String path = f.getPath(); |
|
55 |
return new File(path.substring(0, path.length() - 5) + ".class"); |
|
56 |
} |
|
57 |
||
58 |
ClassFile getClassFile(String name) throws IOException, ConstantPoolException { |
|
59 |
URL url = getClass().getResource(name); |
|
60 |
InputStream in = url.openStream(); |
|
61 |
try { |
|
62 |
return ClassFile.read(in); |
|
63 |
} finally { |
|
64 |
in.close(); |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
ClassFile getClassFile(URL url) throws IOException, ConstantPoolException { |
|
69 |
InputStream in = url.openStream(); |
|
70 |
try { |
|
71 |
return ClassFile.read(in); |
|
72 |
} finally { |
|
73 |
in.close(); |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
/************ Helper annotations counting methods ******************/ |
|
78 |
void test(ClassFile cf) { |
|
79 |
test("CLASS",cf, null, null, Attribute.RuntimeVisibleTypeAnnotations, true); |
|
80 |
test("CLASS",cf, null, null, Attribute.RuntimeInvisibleTypeAnnotations, false); |
|
81 |
//RuntimeAnnotations since one annotation can result in two attributes. |
|
82 |
test("CLASS",cf, null, null, Attribute.RuntimeVisibleAnnotations, true); |
|
83 |
test("CLASS",cf, null, null, Attribute.RuntimeInvisibleAnnotations, false); |
|
84 |
} |
|
85 |
||
86 |
void test(ClassFile cf, Method m) { |
|
87 |
test("METHOD",cf, null, m, Attribute.RuntimeVisibleTypeAnnotations, true); |
|
88 |
test("METHOD",cf, null, m, Attribute.RuntimeInvisibleTypeAnnotations, false); |
|
89 |
test("METHOD",cf, null, m, Attribute.RuntimeVisibleAnnotations, true); |
|
90 |
test("METHOD",cf, null, m, Attribute.RuntimeInvisibleAnnotations, false); |
|
91 |
} |
|
92 |
||
93 |
void test(ClassFile cf, Field f) { |
|
94 |
test("FIELD",cf, f, null, Attribute.RuntimeVisibleTypeAnnotations, true); |
|
95 |
test("FIELD",cf, f, null, Attribute.RuntimeInvisibleTypeAnnotations, false); |
|
96 |
test("FIELD",cf, f, null, Attribute.RuntimeVisibleAnnotations, true); |
|
97 |
test("FIELD",cf, f, null, Attribute.RuntimeInvisibleAnnotations, false); |
|
98 |
} |
|
99 |
||
100 |
||
101 |
// Test the result of Attributes.getIndex according to expectations |
|
102 |
// encoded in the class/field/method name; increment annotations counts. |
|
103 |
void test(String ttype, ClassFile cf, Field f, Method m, String annName, boolean visible) { |
|
104 |
String testtype = ttype; |
|
105 |
String name = null; |
|
106 |
int index = -1; |
|
107 |
Attribute attr = null; |
|
108 |
boolean isTAattr = annName.contains("TypeAnnotations"); |
|
109 |
try { |
|
110 |
switch(testtype) { |
|
111 |
case "FIELD": |
|
112 |
name = f.getName(cf.constant_pool); |
|
113 |
index = f.attributes.getIndex(cf.constant_pool, annName); |
|
114 |
if(index!= -1) attr = f.attributes.get(index); |
|
115 |
break; |
|
116 |
case "METHOD": |
|
117 |
name = m.getName(cf.constant_pool); |
|
118 |
index = m.attributes.getIndex(cf.constant_pool, annName); |
|
119 |
if(index!= -1) attr = m.attributes.get(index); |
|
120 |
break; |
|
121 |
default: |
|
122 |
name = cf.getName(); |
|
123 |
index = cf.attributes.getIndex(cf.constant_pool, annName); |
|
124 |
if(index!= -1) attr = cf.attributes.get(index); |
|
125 |
} |
|
126 |
} catch(ConstantPoolException cpe) { cpe.printStackTrace(); } |
|
127 |
||
128 |
if (index != -1) { |
|
129 |
assert attr instanceof RuntimeTypeAnnotations_attribute; |
|
130 |
if(isTAattr) { //count RuntimeTypeAnnotations |
|
131 |
RuntimeTypeAnnotations_attribute tAttr = |
|
132 |
(RuntimeTypeAnnotations_attribute)attr; |
|
133 |
println(testtype + ": " + name + ", " + annName + ": " + |
|
134 |
tAttr.annotations.length ); |
|
135 |
allt += tAttr.annotations.length; |
|
136 |
if (visible) |
|
137 |
tvisibles += tAttr.annotations.length; |
|
138 |
else |
|
139 |
tinvisibles += tAttr.annotations.length; |
|
140 |
} else { |
|
141 |
RuntimeAnnotations_attribute tAttr = |
|
142 |
(RuntimeAnnotations_attribute)attr; |
|
143 |
println(testtype + ": " + name + ", " + annName + ": " + |
|
144 |
tAttr.annotations.length ); |
|
145 |
all += tAttr.annotations.length; |
|
146 |
if (visible) |
|
147 |
visibles += tAttr.annotations.length; |
|
148 |
else |
|
149 |
invisibles += tAttr.annotations.length; |
|
150 |
} |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
void countAnnotations() { |
|
155 |
errors=0; |
|
156 |
int expected_allt = expected_tvisibles + expected_tinvisibles; |
|
157 |
int expected_all = expected_visibles + expected_invisibles; |
|
158 |
||
159 |
if (expected_allt != allt) { |
|
160 |
errors++; |
|
161 |
System.err.println("Failure: expected " + expected_allt + |
|
162 |
" type annotations but found " + allt); |
|
163 |
} |
|
164 |
if (expected_all != all) { |
|
165 |
errors++; |
|
166 |
System.err.println("Failure: expected " + expected_all + |
|
167 |
" annotations but found " + all); |
|
168 |
} |
|
169 |
if (expected_tvisibles != tvisibles) { |
|
170 |
errors++; |
|
171 |
System.err.println("Failure: expected " + expected_tvisibles + |
|
172 |
" typevisible annotations but found " + tvisibles); |
|
173 |
} |
|
174 |
||
175 |
if (expected_tinvisibles != tinvisibles) { |
|
176 |
errors++; |
|
177 |
System.err.println("Failure: expected " + expected_tinvisibles + |
|
178 |
" typeinvisible annotations but found " + tinvisibles); |
|
179 |
} |
|
180 |
allt=0; |
|
181 |
tvisibles=0; |
|
182 |
tinvisibles=0; |
|
183 |
all=0; |
|
184 |
visibles=0; |
|
185 |
invisibles=0; |
|
186 |
} |
|
187 |
||
188 |
int errors; |
|
189 |
int allt; |
|
190 |
int tvisibles; |
|
191 |
int tinvisibles; |
|
192 |
int all; |
|
193 |
int visibles; |
|
194 |
int invisibles; |
|
195 |
} |