test/tools/javap/typeAnnotations/ClassLiterals.java

Fri, 26 Jun 2009 19:12:41 -0700

author
jjg
date
Fri, 26 Jun 2009 19:12:41 -0700
changeset 309
664edca41e34
child 554
9d9f26857129
permissions
-rw-r--r--

6855544: add missing files
Reviewed-by: jjg, mcimadamore, darcy
Contributed-by: mernst@cs.washington.edu, mali@csail.mit.edu, mpapi@csail.mit.edu

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

mercurial