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

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

mercurial