Mon, 03 May 2010 17:12:59 -0700
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
Reviewed-by: jjg, darcy
1 /*
2 * Copyright 2008 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 */
24 import java.io.*;
25 import com.sun.tools.classfile.*;
27 /*
28 * @test Presence
29 * @bug 6843077
30 * @summary test that all type annotations are present in the classfile
31 */
33 public class Presence {
34 public static void main(String[] args) throws Exception {
35 new Presence().run();
36 }
38 public void run() throws Exception {
39 File javaFile = writeTestFile();
40 File classFile = compileTestFile(javaFile);
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 }
51 countAnnotations();
53 if (errors > 0)
54 throw new Exception(errors + " errors found");
55 System.out.println("PASSED");
56 }
58 void test(ClassFile cf) {
59 test(cf, Attribute.RuntimeVisibleTypeAnnotations, true);
60 test(cf, Attribute.RuntimeInvisibleTypeAnnotations, false);
61 }
63 void test(ClassFile cf, Method m) {
64 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
65 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
66 }
68 void test(ClassFile cf, Field m) {
69 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
70 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
71 }
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 }
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 }
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 }
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 { }");
128 out.println(" Map<@A String, Map<@A String, @A String>> f1;");
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 }
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 }
161 void countAnnotations() {
162 int expected_visibles = 0, expected_invisibles = 39;
163 int expected_all = expected_visibles + expected_invisibles;
165 if (expected_all != all) {
166 errors++;
167 System.err.println("expected " + expected_all
168 + " annotations but found " + all);
169 }
171 if (expected_visibles != visibles) {
172 errors++;
173 System.err.println("expected " + expected_visibles
174 + " visibles annotations but found " + visibles);
175 }
177 if (expected_invisibles != invisibles) {
178 errors++;
179 System.err.println("expected " + expected_invisibles
180 + " invisibles annotations but found " + invisibles);
181 }
183 }
185 int errors;
186 int all;
187 int visibles;
188 int invisibles;
189 }