test/tools/javap/typeAnnotations/ArrayClassLiterals.java

Thu, 04 Nov 2010 15:54:46 -0700

author
cl
date
Thu, 04 Nov 2010 15:54:46 -0700
changeset 720
5bb96781fb58
parent 554
9d9f26857129
permissions
-rw-r--r--

Added tag jdk7-b117 for changeset 2129a046f117

jjg@328 1 /*
ohair@554 2 * Copyright (c) 2009, Oracle and/or its affiliates. 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 *
ohair@554 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 20 * or visit www.oracle.com if you need additional information or have any
ohair@554 21 * 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@329 29 * @bug 6863814
jjg@328 30 * @summary test that class literals array doesn't crash javap
jjg@328 31 */
jjg@328 32
jjg@328 33 public class ArrayClassLiterals {
jjg@328 34 public static void main(String[] args) throws Exception {
jjg@328 35 new ArrayClassLiterals().run();
jjg@328 36 }
jjg@328 37
jjg@328 38 public void run() throws Exception {
jjg@328 39 File javaFile = writeTestFile();
jjg@328 40 File classFile = compileTestFile(javaFile);
jjg@328 41
jjg@328 42 ClassFile cf = ClassFile.read(classFile);
jjg@328 43 test(cf);
jjg@328 44 for (Field f : cf.fields) {
jjg@328 45 test(cf, f);
jjg@328 46 }
jjg@328 47 for (Method m: cf.methods) {
jjg@328 48 test(cf, m);
jjg@328 49 }
jjg@328 50
jjg@328 51 countAnnotations();
jjg@328 52
jjg@328 53 if (errors > 0)
jjg@328 54 throw new Exception(errors + " errors found");
jjg@328 55 System.out.println("PASSED");
jjg@328 56 }
jjg@328 57
jjg@328 58 void test(ClassFile cf) {
jjg@328 59 test(cf, Attribute.RuntimeVisibleTypeAnnotations, true);
jjg@328 60 test(cf, Attribute.RuntimeInvisibleTypeAnnotations, false);
jjg@328 61 }
jjg@328 62
jjg@328 63 void test(ClassFile cf, Method m) {
jjg@328 64 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
jjg@328 65 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
jjg@328 66 }
jjg@328 67
jjg@328 68 void test(ClassFile cf, Field m) {
jjg@328 69 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
jjg@328 70 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
jjg@328 71 }
jjg@328 72
jjg@328 73 // test the result of Attributes.getIndex according to expectations
jjg@328 74 // encoded in the method's name
jjg@328 75 void test(ClassFile cf, String name, boolean visible) {
jjg@328 76 int index = cf.attributes.getIndex(cf.constant_pool, name);
jjg@328 77 if (index != -1) {
jjg@328 78 Attribute attr = cf.attributes.get(index);
jjg@328 79 assert attr instanceof RuntimeTypeAnnotations_attribute;
jjg@328 80 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
jjg@328 81 all += tAttr.annotations.length;
jjg@328 82 if (visible)
jjg@328 83 visibles += tAttr.annotations.length;
jjg@328 84 else
jjg@328 85 invisibles += tAttr.annotations.length;
jjg@328 86
jjg@328 87 for (ExtendedAnnotation anno : tAttr.annotations)
jjg@328 88 anno.position.toString();
jjg@328 89 }
jjg@328 90 }
jjg@328 91
jjg@328 92 // test the result of Attributes.getIndex according to expectations
jjg@328 93 // encoded in the method's name
jjg@328 94 void test(ClassFile cf, Method m, String name, boolean visible) {
jjg@328 95 int index = m.attributes.getIndex(cf.constant_pool, name);
jjg@328 96 if (index != -1) {
jjg@328 97 Attribute attr = m.attributes.get(index);
jjg@328 98 assert attr instanceof RuntimeTypeAnnotations_attribute;
jjg@328 99 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
jjg@328 100 all += tAttr.annotations.length;
jjg@328 101 if (visible)
jjg@328 102 visibles += tAttr.annotations.length;
jjg@328 103 else
jjg@328 104 invisibles += tAttr.annotations.length;
jjg@328 105
jjg@328 106 for (ExtendedAnnotation anno : tAttr.annotations)
jjg@328 107 anno.position.toString();
jjg@328 108 }
jjg@328 109 }
jjg@328 110
jjg@328 111 // test the result of Attributes.getIndex according to expectations
jjg@328 112 // encoded in the method's name
jjg@328 113 void test(ClassFile cf, Field m, String name, boolean visible) {
jjg@328 114 int index = m.attributes.getIndex(cf.constant_pool, name);
jjg@328 115 if (index != -1) {
jjg@328 116 Attribute attr = m.attributes.get(index);
jjg@328 117 assert attr instanceof RuntimeTypeAnnotations_attribute;
jjg@328 118 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
jjg@328 119 all += tAttr.annotations.length;
jjg@328 120 if (visible)
jjg@328 121 visibles += tAttr.annotations.length;
jjg@328 122 else
jjg@328 123 invisibles += tAttr.annotations.length;
jjg@328 124
jjg@328 125 for (ExtendedAnnotation anno : tAttr.annotations)
jjg@328 126 anno.position.toString();
jjg@328 127 }
jjg@328 128 }
jjg@328 129
jjg@328 130 File writeTestFile() throws IOException {
jjg@328 131 File f = new File("Testa.java");
jjg@328 132 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
jjg@328 133 out.println("import java.util.*;");
jjg@328 134 out.println("class Testa { ");
jjg@328 135 out.println(" @interface A { }");
jjg@328 136
jjg@328 137 out.println(" void test() {");
jjg@328 138 out.println(" Object a = @A String.class;");
jjg@328 139 out.println(" Object b = @A String @A [] @A [].class;");
jjg@328 140 out.println(" }");
jjg@328 141 out.println("}");
jjg@328 142
jjg@328 143 out.close();
jjg@328 144 return f;
jjg@328 145 }
jjg@328 146
jjg@328 147 File compileTestFile(File f) {
jjg@328 148 int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.7", "-g", f.getPath() });
jjg@328 149 if (rc != 0)
jjg@328 150 throw new Error("compilation failed. rc=" + rc);
jjg@328 151 String path = f.getPath();
jjg@328 152 return new File(path.substring(0, path.length() - 5) + ".class");
jjg@328 153 }
jjg@328 154
jjg@328 155 void countAnnotations() {
jjg@328 156 int expected_visibles = 0, expected_invisibles = 4;
jjg@328 157 int expected_all = expected_visibles + expected_invisibles;
jjg@328 158
jjg@328 159 if (expected_all != all) {
jjg@328 160 errors++;
jjg@328 161 System.err.println("expected " + expected_all
jjg@328 162 + " annotations but found " + all);
jjg@328 163 }
jjg@328 164
jjg@328 165 if (expected_visibles != visibles) {
jjg@328 166 errors++;
jjg@328 167 System.err.println("expected " + expected_visibles
jjg@328 168 + " visibles annotations but found " + visibles);
jjg@328 169 }
jjg@328 170
jjg@328 171 if (expected_invisibles != invisibles) {
jjg@328 172 errors++;
jjg@328 173 System.err.println("expected " + expected_invisibles
jjg@328 174 + " invisibles annotations but found " + invisibles);
jjg@328 175 }
jjg@328 176
jjg@328 177 }
jjg@328 178
jjg@328 179 int errors;
jjg@328 180 int all;
jjg@328 181 int visibles;
jjg@328 182 int invisibles;
jjg@328 183 }

mercurial