test/tools/javap/typeAnnotations/JSR175Annotations.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 JSR175Annotations
jjg@309 29 * @bug 6843077
jjg@309 30 * @summary test that only type annotations are recorded as such in classfile
jjg@309 31 */
jjg@309 32
jjg@309 33 public class JSR175Annotations {
jjg@309 34 public static void main(String[] args) throws Exception {
jjg@309 35 new JSR175Annotations().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 for (Field f : cf.fields) {
jjg@309 44 test(cf, f);
jjg@309 45 }
jjg@309 46 for (Method m: cf.methods) {
jjg@309 47 test(cf, m);
jjg@309 48 }
jjg@309 49
jjg@309 50 countAnnotations();
jjg@309 51
jjg@309 52 if (errors > 0)
jjg@309 53 throw new Exception(errors + " errors found");
jjg@309 54 System.out.println("PASSED");
jjg@309 55 }
jjg@309 56
jjg@309 57 void test(ClassFile cf, Method m) {
jjg@309 58 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
jjg@309 59 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
jjg@309 60 }
jjg@309 61
jjg@309 62 void test(ClassFile cf, Field m) {
jjg@309 63 test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
jjg@309 64 test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
jjg@309 65 }
jjg@309 66
jjg@309 67 // test the result of Attributes.getIndex according to expectations
jjg@309 68 // encoded in the method's name
jjg@309 69 void test(ClassFile cf, Method m, String name, boolean visible) {
jjg@309 70 int index = m.attributes.getIndex(cf.constant_pool, name);
jjg@309 71 if (index != -1) {
jjg@309 72 Attribute attr = m.attributes.get(index);
jjg@309 73 assert attr instanceof RuntimeTypeAnnotations_attribute;
jjg@309 74 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
jjg@309 75 all += tAttr.annotations.length;
jjg@309 76 if (visible)
jjg@309 77 visibles += tAttr.annotations.length;
jjg@309 78 else
jjg@309 79 invisibles += tAttr.annotations.length;
jjg@309 80 }
jjg@309 81 }
jjg@309 82
jjg@309 83 // test the result of Attributes.getIndex according to expectations
jjg@309 84 // encoded in the method's name
jjg@309 85 void test(ClassFile cf, Field m, String name, boolean visible) {
jjg@309 86 int index = m.attributes.getIndex(cf.constant_pool, name);
jjg@309 87 if (index != -1) {
jjg@309 88 Attribute attr = m.attributes.get(index);
jjg@309 89 assert attr instanceof RuntimeTypeAnnotations_attribute;
jjg@309 90 RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
jjg@309 91 all += tAttr.annotations.length;
jjg@309 92 if (visible)
jjg@309 93 visibles += tAttr.annotations.length;
jjg@309 94 else
jjg@309 95 invisibles += tAttr.annotations.length;
jjg@309 96 }
jjg@309 97 }
jjg@309 98
jjg@309 99 File writeTestFile() throws IOException {
jjg@309 100 File f = new File("Test.java");
jjg@309 101 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
jjg@309 102 out.println("import java.lang.annotation.Retention;");
jjg@309 103 out.println("import java.lang.annotation.RetentionPolicy;");
jjg@309 104 out.println("abstract class Test { ");
jjg@309 105 out.println(" @Retention(RetentionPolicy.RUNTIME)");
jjg@309 106 out.println(" @interface A { }");
jjg@309 107 out.println(" @A String m;");
jjg@309 108 out.println(" @A String method(@A String a) {");
jjg@309 109 out.println(" return a;");
jjg@309 110 out.println(" }");
jjg@309 111 out.println("}");
jjg@309 112 out.close();
jjg@309 113 return f;
jjg@309 114 }
jjg@309 115
jjg@309 116 File compileTestFile(File f) {
jjg@309 117 int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.7", "-g", f.getPath() });
jjg@309 118 if (rc != 0)
jjg@309 119 throw new Error("compilation failed. rc=" + rc);
jjg@309 120 String path = f.getPath();
jjg@309 121 return new File(path.substring(0, path.length() - 5) + ".class");
jjg@309 122 }
jjg@309 123
jjg@309 124 void countAnnotations() {
jjg@309 125 int expected_visibles = 0, expected_invisibles = 0;
jjg@309 126 int expected_all = expected_visibles + expected_invisibles;
jjg@309 127
jjg@309 128 if (expected_all != all) {
jjg@309 129 errors++;
jjg@309 130 System.err.println("expected " + expected_all
jjg@309 131 + " annotations but found " + all);
jjg@309 132 }
jjg@309 133
jjg@309 134 if (expected_visibles != visibles) {
jjg@309 135 errors++;
jjg@309 136 System.err.println("expected " + expected_visibles
jjg@309 137 + " visibles annotations but found " + visibles);
jjg@309 138 }
jjg@309 139
jjg@309 140 if (expected_invisibles != invisibles) {
jjg@309 141 errors++;
jjg@309 142 System.err.println("expected " + expected_invisibles
jjg@309 143 + " invisibles annotations but found " + invisibles);
jjg@309 144 }
jjg@309 145
jjg@309 146 }
jjg@309 147
jjg@309 148 int errors;
jjg@309 149 int all;
jjg@309 150 int visibles;
jjg@309 151 int invisibles;
jjg@309 152 }

mercurial