test/tools/javap/typeAnnotations/ArrayClassLiterals.java

changeset 328
99b7a25185aa
child 329
49387c1440d0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javap/typeAnnotations/ArrayClassLiterals.java	Thu Jul 23 11:37:44 2009 -0700
     1.3 @@ -0,0 +1,182 @@
     1.4 +/*
     1.5 + * Copyright 2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + */
    1.26 +
    1.27 +import java.io.*;
    1.28 +import com.sun.tools.classfile.*;
    1.29 +
    1.30 +/*
    1.31 + * @test ArrayClassLiterals
    1.32 + * @summary test that class literals array doesn't crash javap
    1.33 + */
    1.34 +
    1.35 +public class ArrayClassLiterals {
    1.36 +    public static void main(String[] args) throws Exception {
    1.37 +        new ArrayClassLiterals().run();
    1.38 +    }
    1.39 +
    1.40 +    public void run() throws Exception {
    1.41 +        File javaFile = writeTestFile();
    1.42 +        File classFile = compileTestFile(javaFile);
    1.43 +
    1.44 +        ClassFile cf = ClassFile.read(classFile);
    1.45 +        test(cf);
    1.46 +        for (Field f : cf.fields) {
    1.47 +            test(cf, f);
    1.48 +        }
    1.49 +        for (Method m: cf.methods) {
    1.50 +            test(cf, m);
    1.51 +        }
    1.52 +
    1.53 +        countAnnotations();
    1.54 +
    1.55 +        if (errors > 0)
    1.56 +            throw new Exception(errors + " errors found");
    1.57 +        System.out.println("PASSED");
    1.58 +    }
    1.59 +
    1.60 +    void test(ClassFile cf) {
    1.61 +        test(cf, Attribute.RuntimeVisibleTypeAnnotations, true);
    1.62 +        test(cf, Attribute.RuntimeInvisibleTypeAnnotations, false);
    1.63 +    }
    1.64 +
    1.65 +    void test(ClassFile cf, Method m) {
    1.66 +        test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
    1.67 +        test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
    1.68 +    }
    1.69 +
    1.70 +    void test(ClassFile cf, Field m) {
    1.71 +        test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
    1.72 +        test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
    1.73 +    }
    1.74 +
    1.75 +    // test the result of Attributes.getIndex according to expectations
    1.76 +    // encoded in the method's name
    1.77 +    void test(ClassFile cf, String name, boolean visible) {
    1.78 +        int index = cf.attributes.getIndex(cf.constant_pool, name);
    1.79 +        if (index != -1) {
    1.80 +            Attribute attr = cf.attributes.get(index);
    1.81 +            assert attr instanceof RuntimeTypeAnnotations_attribute;
    1.82 +            RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
    1.83 +            all += tAttr.annotations.length;
    1.84 +            if (visible)
    1.85 +                visibles += tAttr.annotations.length;
    1.86 +            else
    1.87 +                invisibles += tAttr.annotations.length;
    1.88 +
    1.89 +            for (ExtendedAnnotation anno : tAttr.annotations)
    1.90 +                anno.position.toString();
    1.91 +        }
    1.92 +    }
    1.93 +
    1.94 +    // test the result of Attributes.getIndex according to expectations
    1.95 +    // encoded in the method's name
    1.96 +    void test(ClassFile cf, Method m, String name, boolean visible) {
    1.97 +        int index = m.attributes.getIndex(cf.constant_pool, name);
    1.98 +        if (index != -1) {
    1.99 +            Attribute attr = m.attributes.get(index);
   1.100 +            assert attr instanceof RuntimeTypeAnnotations_attribute;
   1.101 +            RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
   1.102 +            all += tAttr.annotations.length;
   1.103 +            if (visible)
   1.104 +                visibles += tAttr.annotations.length;
   1.105 +            else
   1.106 +                invisibles += tAttr.annotations.length;
   1.107 +
   1.108 +            for (ExtendedAnnotation anno : tAttr.annotations)
   1.109 +                anno.position.toString();
   1.110 +        }
   1.111 +    }
   1.112 +
   1.113 +    // test the result of Attributes.getIndex according to expectations
   1.114 +    // encoded in the method's name
   1.115 +    void test(ClassFile cf, Field m, String name, boolean visible) {
   1.116 +        int index = m.attributes.getIndex(cf.constant_pool, name);
   1.117 +        if (index != -1) {
   1.118 +            Attribute attr = m.attributes.get(index);
   1.119 +            assert attr instanceof RuntimeTypeAnnotations_attribute;
   1.120 +            RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
   1.121 +            all += tAttr.annotations.length;
   1.122 +            if (visible)
   1.123 +                visibles += tAttr.annotations.length;
   1.124 +            else
   1.125 +                invisibles += tAttr.annotations.length;
   1.126 +
   1.127 +            for (ExtendedAnnotation anno : tAttr.annotations)
   1.128 +                anno.position.toString();
   1.129 +        }
   1.130 +    }
   1.131 +
   1.132 +    File writeTestFile() throws IOException {
   1.133 +      File f = new File("Testa.java");
   1.134 +        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
   1.135 +        out.println("import java.util.*;");
   1.136 +        out.println("class Testa { ");
   1.137 +        out.println("  @interface A { }");
   1.138 +
   1.139 +        out.println(" void test() {");
   1.140 +        out.println("  Object a = @A String.class;");
   1.141 +        out.println("  Object b = @A String @A [] @A [].class;");
   1.142 +        out.println(" }");
   1.143 +        out.println("}");
   1.144 +
   1.145 +        out.close();
   1.146 +        return f;
   1.147 +    }
   1.148 +
   1.149 +    File compileTestFile(File f) {
   1.150 +      int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.7", "-g", f.getPath() });
   1.151 +        if (rc != 0)
   1.152 +            throw new Error("compilation failed. rc=" + rc);
   1.153 +        String path = f.getPath();
   1.154 +        return new File(path.substring(0, path.length() - 5) + ".class");
   1.155 +    }
   1.156 +
   1.157 +    void countAnnotations() {
   1.158 +        int expected_visibles = 0, expected_invisibles = 4;
   1.159 +        int expected_all = expected_visibles + expected_invisibles;
   1.160 +
   1.161 +        if (expected_all != all) {
   1.162 +            errors++;
   1.163 +            System.err.println("expected " + expected_all
   1.164 +                    + " annotations but found " + all);
   1.165 +        }
   1.166 +
   1.167 +        if (expected_visibles != visibles) {
   1.168 +            errors++;
   1.169 +            System.err.println("expected " + expected_visibles
   1.170 +                    + " visibles annotations but found " + visibles);
   1.171 +        }
   1.172 +
   1.173 +        if (expected_invisibles != invisibles) {
   1.174 +            errors++;
   1.175 +            System.err.println("expected " + expected_invisibles
   1.176 +                    + " invisibles annotations but found " + invisibles);
   1.177 +        }
   1.178 +
   1.179 +    }
   1.180 +
   1.181 +    int errors;
   1.182 +    int all;
   1.183 +    int visibles;
   1.184 +    int invisibles;
   1.185 +}

mercurial