test/tools/javac/typeAnnotations/classfile/DeadCode.java

changeset 480
59167312ed4e
child 554
9d9f26857129
equal deleted inserted replaced
479:da0e3e2dd3ef 480:59167312ed4e
1 /*
2 * Copyright 2009-2010 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24 import java.io.*;
25 import java.net.URL;
26 import java.util.List;
27
28 import com.sun.tools.classfile.*;
29
30 /*
31 * @test
32 * @bug 6917130
33 * @summary test that optimized away annotations are not emited to classfile
34 */
35
36 public class DeadCode {
37 public static void main(String[] args) throws Exception {
38 new DeadCode().run();
39 }
40
41 public void run() throws Exception {
42 ClassFile cf = getClassFile("DeadCode$Test.class");
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 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 /************ Helper annotations counting methods ******************/
69 void test(ClassFile cf) {
70 test(cf, Attribute.RuntimeVisibleTypeAnnotations, true);
71 test(cf, Attribute.RuntimeInvisibleTypeAnnotations, false);
72 }
73
74 void test(ClassFile cf, Method m) {
75 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
76 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
77 }
78
79 void test(ClassFile cf, Field m) {
80 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
81 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
82 }
83
84 // test the result of Attributes.getIndex according to expectations
85 // encoded in the method's name
86 void test(ClassFile cf, String name, boolean visible) {
87 int index = cf.attributes.getIndex(cf.constant_pool, name);
88 if (index != -1) {
89 Attribute attr = cf.attributes.get(index);
90 assert attr instanceof RuntimeTypeAnnotations_attribute;
91 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
92 all += tAttr.annotations.length;
93 if (visible)
94 visibles += tAttr.annotations.length;
95 else
96 invisibles += tAttr.annotations.length;
97 }
98 }
99
100 // test the result of Attributes.getIndex according to expectations
101 // encoded in the method's name
102 void test(ClassFile cf, Method m, String name, boolean visible) {
103 int index = m.attributes.getIndex(cf.constant_pool, name);
104 if (index != -1) {
105 Attribute attr = m.attributes.get(index);
106 assert attr instanceof RuntimeTypeAnnotations_attribute;
107 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
108 all += tAttr.annotations.length;
109 if (visible)
110 visibles += tAttr.annotations.length;
111 else
112 invisibles += tAttr.annotations.length;
113 }
114 }
115
116 // test the result of Attributes.getIndex according to expectations
117 // encoded in the method's name
118 void test(ClassFile cf, Field m, String name, boolean visible) {
119 int index = m.attributes.getIndex(cf.constant_pool, name);
120 if (index != -1) {
121 Attribute attr = m.attributes.get(index);
122 assert attr instanceof RuntimeTypeAnnotations_attribute;
123 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
124 all += tAttr.annotations.length;
125 if (visible)
126 visibles += tAttr.annotations.length;
127 else
128 invisibles += tAttr.annotations.length;
129 }
130 }
131
132 void countAnnotations() {
133 int expected_all = expected_visibles + expected_invisibles;
134
135 if (expected_all != all) {
136 errors++;
137 System.err.println("expected " + expected_all
138 + " annotations but found " + all);
139 }
140
141 if (expected_visibles != visibles) {
142 errors++;
143 System.err.println("expected " + expected_visibles
144 + " visibles annotations but found " + visibles);
145 }
146
147 if (expected_invisibles != invisibles) {
148 errors++;
149 System.err.println("expected " + expected_invisibles
150 + " invisibles annotations but found " + invisibles);
151 }
152
153 }
154
155 int errors;
156 int all;
157 int visibles;
158 int invisibles;
159
160 /*********************** Test class *************************/
161 static int expected_invisibles = 1;
162 static int expected_visibles = 0;
163 static class Test {
164 @interface A {}
165
166 void test() {
167 List<? extends @A Object> o = null;
168 o.toString();
169
170 @A String m;
171 if (false) {
172 @A String a;
173 @A String b = "m";
174 b.toString();
175 List<? extends @A Object> c = null;
176 c.toString();
177 }
178 }
179 }
180
181 }

mercurial