test/tools/javap/typeAnnotations/NewArray.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javap/typeAnnotations/NewArray.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,140 @@
     1.4 +/*
     1.5 + * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +import java.io.*;
    1.28 +import com.sun.tools.classfile.*;
    1.29 +
    1.30 +/*
    1.31 + * @test NewArray
    1.32 + * @bug 6843077
    1.33 + * @summary Test type annotations on local array are in method's code attribute.
    1.34 + */
    1.35 +
    1.36 +public class NewArray {
    1.37 +
    1.38 +    public static void main(String[] args) throws Exception {
    1.39 +        new NewArray().run();
    1.40 +    }
    1.41 +
    1.42 +    public void run() throws Exception {
    1.43 +        File javaFile = writeTestFile();
    1.44 +        File classFile = compileTestFile(javaFile);
    1.45 +
    1.46 +        ClassFile cf = ClassFile.read(classFile);
    1.47 +        for (Method m: cf.methods) {
    1.48 +            test(cf, m);
    1.49 +        }
    1.50 +
    1.51 +        countAnnotations();
    1.52 +
    1.53 +        if (errors > 0)
    1.54 +            throw new Exception(errors + " errors found");
    1.55 +        System.out.println("PASSED");
    1.56 +    }
    1.57 +
    1.58 +    void test(ClassFile cf, Method m) {
    1.59 +        test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
    1.60 +        test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
    1.61 +    }
    1.62 +
    1.63 +    // test the result of Attributes.getIndex according to expectations
    1.64 +    // encoded in the method's name
    1.65 +    void test(ClassFile cf, Method m, String name, boolean visible) {
    1.66 +        Attribute attr = null;
    1.67 +        Code_attribute cAttr = null;
    1.68 +        RuntimeTypeAnnotations_attribute tAttr = null;
    1.69 +
    1.70 +        int index = m.attributes.getIndex(cf.constant_pool, Attribute.Code);
    1.71 +        if(index!= -1) {
    1.72 +            attr = m.attributes.get(index);
    1.73 +            assert attr instanceof Code_attribute;
    1.74 +            cAttr = (Code_attribute)attr;
    1.75 +            index = cAttr.attributes.getIndex(cf.constant_pool, name);
    1.76 +            if(index!= -1) {
    1.77 +                attr = cAttr.attributes.get(index);
    1.78 +                assert attr instanceof RuntimeTypeAnnotations_attribute;
    1.79 +                tAttr = (RuntimeTypeAnnotations_attribute)attr;
    1.80 +                all += tAttr.annotations.length;
    1.81 +                if (visible)
    1.82 +                    visibles += tAttr.annotations.length;
    1.83 +                else
    1.84 +                    invisibles += tAttr.annotations.length;
    1.85 +               }
    1.86 +        }
    1.87 +    }
    1.88 +
    1.89 +    File writeTestFile() throws IOException {
    1.90 +      File f = new File("Test.java");
    1.91 +        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
    1.92 +        out.println("import java.lang.annotation.*;");
    1.93 +        out.println("import java.util.*;");
    1.94 +        out.println("class Test { ");
    1.95 +        out.println("  @Target(ElementType.TYPE_USE) @interface A { }");
    1.96 +        out.println("  void test() {");
    1.97 +        out.println("    Object a = new @A String @A [5] @A  [];");
    1.98 +        out.println("    Object b = new @A String @A [5] @A [3];");
    1.99 +        out.println("    Object c = new @A String @A [] @A [] {};");
   1.100 +        out.println("  }");
   1.101 +        out.println("}");
   1.102 +
   1.103 +        out.close();
   1.104 +        return f;
   1.105 +    }
   1.106 +
   1.107 +    File compileTestFile(File f) {
   1.108 +        int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.8", "-g", f.getPath() });
   1.109 +        if (rc != 0)
   1.110 +            throw new Error("compilation failed. rc=" + rc);
   1.111 +        String path = f.getPath();
   1.112 +        return new File(path.substring(0, path.length() - 5) + ".class");
   1.113 +    }
   1.114 +
   1.115 +    void countAnnotations() {
   1.116 +        int expected_visibles = 0, expected_invisibles = 9;
   1.117 +        int expected_all = expected_visibles + expected_invisibles;
   1.118 +
   1.119 +        if (expected_all != all) {
   1.120 +            errors++;
   1.121 +            System.err.println("expected " + expected_all
   1.122 +                    + " annotations but found " + all);
   1.123 +        }
   1.124 +
   1.125 +        if (expected_visibles != visibles) {
   1.126 +            errors++;
   1.127 +            System.err.println("expected " + expected_visibles
   1.128 +                    + " visibles annotations but found " + visibles);
   1.129 +        }
   1.130 +
   1.131 +        if (expected_invisibles != invisibles) {
   1.132 +            errors++;
   1.133 +            System.err.println("expected " + expected_invisibles
   1.134 +                    + " invisibles annotations but found " + invisibles);
   1.135 +        }
   1.136 +
   1.137 +    }
   1.138 +
   1.139 +    int errors;
   1.140 +    int all;
   1.141 +    int visibles;
   1.142 +    int invisibles;
   1.143 +}

mercurial