test/tools/javap/typeAnnotations/PresenceInner.java

changeset 733
c491eec0acc7
parent 720
5bb96781fb58
parent 732
534afdc92cdc
child 734
814561077c44
child 762
4f086529d05c
     1.1 --- a/test/tools/javap/typeAnnotations/PresenceInner.java	Thu Nov 04 15:54:46 2010 -0700
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,185 +0,0 @@
     1.4 -/*
     1.5 - * Copyright (c) 2009, 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 PresenceInner
    1.32 - * @bug 6843077
    1.33 - * @summary test that annotations in inner types count only once
    1.34 - */
    1.35 -
    1.36 -public class PresenceInner {
    1.37 -    public static void main(String[] args) throws Exception {
    1.38 -        new PresenceInner().run();
    1.39 -    }
    1.40 -
    1.41 -    public void run() throws Exception {
    1.42 -        File javaFile = writeTestFile();
    1.43 -        File classFile = compileTestFile(javaFile);
    1.44 -
    1.45 -        ClassFile cf = ClassFile.read(classFile);
    1.46 -        test(cf);
    1.47 -        for (Field f : cf.fields) {
    1.48 -            test(cf, f);
    1.49 -        }
    1.50 -        for (Method m: cf.methods) {
    1.51 -            test(cf, m);
    1.52 -        }
    1.53 -
    1.54 -        // counts are zero when vising outer class
    1.55 -        countAnnotations(0);
    1.56 -
    1.57 -        // visit inner class
    1.58 -        File innerFile = new File("Test$1Inner.class");
    1.59 -        ClassFile icf = ClassFile.read(innerFile);
    1.60 -        test(icf);
    1.61 -        for (Field f : icf.fields) {
    1.62 -            test(cf, f);
    1.63 -        }
    1.64 -        for (Method m: icf.methods) {
    1.65 -            test(cf, m);
    1.66 -        }
    1.67 -
    1.68 -        countAnnotations(1);
    1.69 -        if (errors > 0)
    1.70 -            throw new Exception(errors + " errors found");
    1.71 -        System.out.println("PASSED");
    1.72 -    }
    1.73 -
    1.74 -    void test(ClassFile cf) {
    1.75 -        test(cf, Attribute.RuntimeVisibleTypeAnnotations, true);
    1.76 -        test(cf, Attribute.RuntimeInvisibleTypeAnnotations, false);
    1.77 -    }
    1.78 -
    1.79 -    void test(ClassFile cf, Method m) {
    1.80 -        test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
    1.81 -        test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
    1.82 -    }
    1.83 -
    1.84 -    void test(ClassFile cf, Field m) {
    1.85 -        test(cf, m, Attribute.RuntimeVisibleTypeAnnotations, true);
    1.86 -        test(cf, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
    1.87 -    }
    1.88 -
    1.89 -    // test the result of Attributes.getIndex according to expectations
    1.90 -    // encoded in the method's name
    1.91 -    void test(ClassFile cf, String name, boolean visible) {
    1.92 -        int index = cf.attributes.getIndex(cf.constant_pool, name);
    1.93 -        if (index != -1) {
    1.94 -            Attribute attr = cf.attributes.get(index);
    1.95 -            assert attr instanceof RuntimeTypeAnnotations_attribute;
    1.96 -            RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
    1.97 -            all += tAttr.annotations.length;
    1.98 -            if (visible)
    1.99 -                visibles += tAttr.annotations.length;
   1.100 -            else
   1.101 -                invisibles += tAttr.annotations.length;
   1.102 -        }
   1.103 -    }
   1.104 -
   1.105 -    // test the result of Attributes.getIndex according to expectations
   1.106 -    // encoded in the method's name
   1.107 -    void test(ClassFile cf, Method m, String name, boolean visible) {
   1.108 -        int index = m.attributes.getIndex(cf.constant_pool, name);
   1.109 -        if (index != -1) {
   1.110 -            Attribute attr = m.attributes.get(index);
   1.111 -            assert attr instanceof RuntimeTypeAnnotations_attribute;
   1.112 -            RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
   1.113 -            all += tAttr.annotations.length;
   1.114 -            if (visible)
   1.115 -                visibles += tAttr.annotations.length;
   1.116 -            else
   1.117 -                invisibles += tAttr.annotations.length;
   1.118 -        }
   1.119 -    }
   1.120 -
   1.121 -    // test the result of Attributes.getIndex according to expectations
   1.122 -    // encoded in the method's name
   1.123 -    void test(ClassFile cf, Field m, String name, boolean visible) {
   1.124 -        int index = m.attributes.getIndex(cf.constant_pool, name);
   1.125 -        if (index != -1) {
   1.126 -            Attribute attr = m.attributes.get(index);
   1.127 -            assert attr instanceof RuntimeTypeAnnotations_attribute;
   1.128 -            RuntimeTypeAnnotations_attribute tAttr = (RuntimeTypeAnnotations_attribute)attr;
   1.129 -            all += tAttr.annotations.length;
   1.130 -            if (visible)
   1.131 -                visibles += tAttr.annotations.length;
   1.132 -            else
   1.133 -                invisibles += tAttr.annotations.length;
   1.134 -        }
   1.135 -    }
   1.136 -
   1.137 -    File writeTestFile() throws IOException {
   1.138 -        File f = new File("Test.java");
   1.139 -        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
   1.140 -
   1.141 -        out.println("class Test {");
   1.142 -        out.println("  void method() {");
   1.143 -        out.println("    class Inner<T extends @A Object> { }");
   1.144 -        out.println("  }");
   1.145 -        out.println("}");
   1.146 -        out.println("@interface A { }");
   1.147 -        out.close();
   1.148 -        System.out.println(f.getAbsolutePath());
   1.149 -        return f;
   1.150 -    }
   1.151 -
   1.152 -    File compileTestFile(File f) {
   1.153 -        int rc = com.sun.tools.javac.Main.compile(new String[] { "-source", "1.7", "-g", f.getPath() });
   1.154 -        if (rc != 0)
   1.155 -            throw new Error("compilation failed. rc=" + rc);
   1.156 -        String path = f.getPath();
   1.157 -        return new File(path.substring(0, path.length() - 5) + ".class");
   1.158 -    }
   1.159 -
   1.160 -    void countAnnotations(int expected_invisibles) {
   1.161 -        int expected_visibles = 0;
   1.162 -        int expected_all = expected_visibles + expected_invisibles;
   1.163 -
   1.164 -        if (expected_all != all) {
   1.165 -            errors++;
   1.166 -            System.err.println("expected " + expected_all
   1.167 -                    + " annotations but found " + all);
   1.168 -        }
   1.169 -
   1.170 -        if (expected_visibles != visibles) {
   1.171 -            errors++;
   1.172 -            System.err.println("expected " + expected_visibles
   1.173 -                    + " visibles annotations but found " + visibles);
   1.174 -        }
   1.175 -
   1.176 -        if (expected_invisibles != invisibles) {
   1.177 -            errors++;
   1.178 -            System.err.println("expected " + expected_invisibles
   1.179 -                    + " invisibles annotations but found " + invisibles);
   1.180 -        }
   1.181 -
   1.182 -    }
   1.183 -
   1.184 -    int errors;
   1.185 -    int all;
   1.186 -    int visibles;
   1.187 -    int invisibles;
   1.188 -}

mercurial