test/tools/javap/typeAnnotations/ArrayClassLiterals.java

Thu, 23 Jul 2009 11:37:44 -0700

author
jjg
date
Thu, 23 Jul 2009 11:37:44 -0700
changeset 328
99b7a25185aa
child 329
49387c1440d0
permissions
-rw-r--r--

6863814: javap crashes when facing array class literals
Reviewed-by: jjg
Contributed-by: mali@csail.mit.edu

jjg@328 1 /*
jjg@328 2 * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
jjg@328 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@328 4 *
jjg@328 5 * This code is free software; you can redistribute it and/or modify it
jjg@328 6 * under the terms of the GNU General Public License version 2 only, as
jjg@328 7 * published by the Free Software Foundation.
jjg@328 8 *
jjg@328 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@328 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@328 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@328 12 * version 2 for more details (a copy is included in the LICENSE file that
jjg@328 13 * accompanied this code).
jjg@328 14 *
jjg@328 15 * You should have received a copy of the GNU General Public License version
jjg@328 16 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@328 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@328 18 *
jjg@328 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@328 20 * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@328 21 * have any questions.
jjg@328 22 */
jjg@328 23
jjg@328 24 import java.io.*;
jjg@328 25 import com.sun.tools.classfile.*;
jjg@328 26
jjg@328 27 /*
jjg@328 28 * @test ArrayClassLiterals
jjg@328 29 * @summary test that class literals array doesn't crash javap
jjg@328 30 */
jjg@328 31
jjg@328 32 public class ArrayClassLiterals {
jjg@328 33 public static void main(String[] args) throws Exception {
jjg@328 34 new ArrayClassLiterals().run();
jjg@328 35 }
jjg@328 36
jjg@328 37 public void run() throws Exception {
jjg@328 38 File javaFile = writeTestFile();
jjg@328 39 File classFile = compileTestFile(javaFile);
jjg@328 40
jjg@328 41 ClassFile cf = ClassFile.read(classFile);
jjg@328 42 test(cf);
jjg@328 43 for (Field f : cf.fields) {
jjg@328 44 test(cf, f);
jjg@328 45 }
jjg@328 46 for (Method m: cf.methods) {
jjg@328 47 test(cf, m);
jjg@328 48 }
jjg@328 49
jjg@328 50 countAnnotations();
jjg@328 51
jjg@328 52 if (errors > 0)
jjg@328 53 throw new Exception(errors + " errors found");
jjg@328 54 System.out.println("PASSED");
jjg@328 55 }
jjg@328 56
jjg@328 57 void test(ClassFile cf) {
jjg@328 58 test(cf, Attribute.RuntimeVisibleTypeAnnotations, true);
jjg@328 59 test(cf, Attribute.RuntimeInvisibleTypeAnnotations, false);
jjg@328 60 }
jjg@328 61
jjg@328 62 void test(ClassFile cf, Method m) {
jjg@328 63 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
jjg@328 64 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
jjg@328 65 }
jjg@328 66
jjg@328 67 void test(ClassFile cf, Field m) {
jjg@328 68 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
jjg@328 69 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
jjg@328 70 }
jjg@328 71
jjg@328 72 // test the result of Attributes.getIndex according to expectations
jjg@328 73 // encoded in the method's name
jjg@328 74 void test(ClassFile cf, String name, boolean visible) {
jjg@328 75 int index = cf.attributes.getIndex(cf.constant_pool, name);
jjg@328 76 if (index != -1) {
jjg@328 77 Attribute attr = cf.attributes.get(index);
jjg@328 78 assert attr instanceof RuntimeTypeAnnotations_attribute;
jjg@328 79 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
jjg@328 80 all += tAttr.annotations.length;
jjg@328 81 if (visible)
jjg@328 82 visibles += tAttr.annotations.length;
jjg@328 83 else
jjg@328 84 invisibles += tAttr.annotations.length;
jjg@328 85
jjg@328 86 for (ExtendedAnnotation anno : tAttr.annotations)
jjg@328 87 anno.position.toString();
jjg@328 88 }
jjg@328 89 }
jjg@328 90
jjg@328 91 // test the result of Attributes.getIndex according to expectations
jjg@328 92 // encoded in the method's name
jjg@328 93 void test(ClassFile cf, Method m, String name, boolean visible) {
jjg@328 94 int index = m.attributes.getIndex(cf.constant_pool, name);
jjg@328 95 if (index != -1) {
jjg@328 96 Attribute attr = m.attributes.get(index);
jjg@328 97 assert attr instanceof RuntimeTypeAnnotations_attribute;
jjg@328 98 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
jjg@328 99 all += tAttr.annotations.length;
jjg@328 100 if (visible)
jjg@328 101 visibles += tAttr.annotations.length;
jjg@328 102 else
jjg@328 103 invisibles += tAttr.annotations.length;
jjg@328 104
jjg@328 105 for (ExtendedAnnotation anno : tAttr.annotations)
jjg@328 106 anno.position.toString();
jjg@328 107 }
jjg@328 108 }
jjg@328 109
jjg@328 110 // test the result of Attributes.getIndex according to expectations
jjg@328 111 // encoded in the method's name
jjg@328 112 void test(ClassFile cf, Field m, String name, boolean visible) {
jjg@328 113 int index = m.attributes.getIndex(cf.constant_pool, name);
jjg@328 114 if (index != -1) {
jjg@328 115 Attribute attr = m.attributes.get(index);
jjg@328 116 assert attr instanceof RuntimeTypeAnnotations_attribute;
jjg@328 117 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
jjg@328 118 all += tAttr.annotations.length;
jjg@328 119 if (visible)
jjg@328 120 visibles += tAttr.annotations.length;
jjg@328 121 else
jjg@328 122 invisibles += tAttr.annotations.length;
jjg@328 123
jjg@328 124 for (ExtendedAnnotation anno : tAttr.annotations)
jjg@328 125 anno.position.toString();
jjg@328 126 }
jjg@328 127 }
jjg@328 128
jjg@328 129 File writeTestFile() throws IOException {
jjg@328 130 File f = new File("Testa.java");
jjg@328 131 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
jjg@328 132 out.println("import java.util.*;");
jjg@328 133 out.println("class Testa { ");
jjg@328 134 out.println(" @interface A { }");
jjg@328 135
jjg@328 136 out.println(" void test() {");
jjg@328 137 out.println(" Object a = @A String.class;");
jjg@328 138 out.println(" Object b = @A String @A [] @A [].class;");
jjg@328 139 out.println(" }");
jjg@328 140 out.println("}");
jjg@328 141
jjg@328 142 out.close();
jjg@328 143 return f;
jjg@328 144 }
jjg@328 145
jjg@328 146 File compileTestFile(File f) {
jjg@328 147 int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.7", "-g", f.getPath() });
jjg@328 148 if (rc != 0)
jjg@328 149 throw new Error("compilation failed. rc=" + rc);
jjg@328 150 String path = f.getPath();
jjg@328 151 return new File(path.substring(0, path.length() - 5) + ".class");
jjg@328 152 }
jjg@328 153
jjg@328 154 void countAnnotations() {
jjg@328 155 int expected_visibles = 0, expected_invisibles = 4;
jjg@328 156 int expected_all = expected_visibles + expected_invisibles;
jjg@328 157
jjg@328 158 if (expected_all != all) {
jjg@328 159 errors++;
jjg@328 160 System.err.println("expected " + expected_all
jjg@328 161 + " annotations but found " + all);
jjg@328 162 }
jjg@328 163
jjg@328 164 if (expected_visibles != visibles) {
jjg@328 165 errors++;
jjg@328 166 System.err.println("expected " + expected_visibles
jjg@328 167 + " visibles annotations but found " + visibles);
jjg@328 168 }
jjg@328 169
jjg@328 170 if (expected_invisibles != invisibles) {
jjg@328 171 errors++;
jjg@328 172 System.err.println("expected " + expected_invisibles
jjg@328 173 + " invisibles annotations but found " + invisibles);
jjg@328 174 }
jjg@328 175
jjg@328 176 }
jjg@328 177
jjg@328 178 int errors;
jjg@328 179 int all;
jjg@328 180 int visibles;
jjg@328 181 int invisibles;
jjg@328 182 }

mercurial