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

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

mercurial