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 NewArray
29 * @bug 6843077
30 * @summary test that all type annotations are present in the classfile
31 */
33 public class NewArray {
34 public static void main(String[] args) throws Exception {
35 new NewArray().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 { ");
126 out.println(" @interface A { }");
128 out.println(" void test() {");
129 out.println(" Object a = new @A String @A [5] @A [];");
130 out.println(" Object b = new @A String @A [5] @A [3];");
131 out.println(" Object c = new @A String @A [] @A [] {};");
132 out.println(" }");
133 out.println("}");
135 out.close();
136 return f;
137 }
139 File compileTestFile(File f) {
140 int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.7", "-g", f.getPath() });
141 if (rc != 0)
142 throw new Error("compilation failed. rc=" + rc);
143 String path = f.getPath();
144 return new File(path.substring(0, path.length() - 5) + ".class");
145 }
147 void countAnnotations() {
148 int expected_visibles = 0, expected_invisibles = 9;
149 int expected_all = expected_visibles + expected_invisibles;
151 if (expected_all != all) {
152 errors++;
153 System.err.println("expected " + expected_all
154 + " annotations but found " + all);
155 }
157 if (expected_visibles != visibles) {
158 errors++;
159 System.err.println("expected " + expected_visibles
160 + " visibles annotations but found " + visibles);
161 }
163 if (expected_invisibles != invisibles) {
164 errors++;
165 System.err.println("expected " + expected_invisibles
166 + " invisibles annotations but found " + invisibles);
167 }
169 }
171 int errors;
172 int all;
173 int visibles;
174 int invisibles;
175 }