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

mercurial