test/tools/javac/annotations/typeAnnotations/classfile/ClassfileTestHelper.java

changeset 1521
71f35e4b93a5
child 1534
bec996065c45
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/annotations/typeAnnotations/classfile/ClassfileTestHelper.java	Wed Jan 23 13:27:24 2013 -0800
     1.3 @@ -0,0 +1,195 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 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.lang.annotation.*;
    1.28 +import java.io.*;
    1.29 +import java.net.URL;
    1.30 +import java.util.List;
    1.31 +
    1.32 +import com.sun.tools.classfile.*;
    1.33 +
    1.34 +public class ClassfileTestHelper {
    1.35 +    int expected_tinvisibles = 0;
    1.36 +    int expected_tvisibles = 0;
    1.37 +    int expected_invisibles = 0;
    1.38 +    int expected_visibles = 0;
    1.39 +
    1.40 +    //Makes debugging much easier. Set to 'false' for less output.
    1.41 +    public Boolean verbose = true;
    1.42 +    void println(String msg) { if(verbose) System.out.println(msg); }
    1.43 +
    1.44 +    File writeTestFile(String fname, String source) throws IOException {
    1.45 +      File f = new File(fname);
    1.46 +        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
    1.47 +        out.println(source);
    1.48 +        out.close();
    1.49 +        return f;
    1.50 +    }
    1.51 +
    1.52 +    File compile(File f) {
    1.53 +        int rc = com.sun.tools.javac.Main.compile(new String[] {
    1.54 +                "-source", "1.8", "-g", f.getPath() });
    1.55 +        if (rc != 0)
    1.56 +            throw new Error("compilation failed. rc=" + rc);
    1.57 +        String path = f.getPath();
    1.58 +        return new File(path.substring(0, path.length() - 5) + ".class");
    1.59 +    }
    1.60 +
    1.61 +    ClassFile getClassFile(String name) throws IOException, ConstantPoolException {
    1.62 +        URL url = getClass().getResource(name);
    1.63 +        InputStream in = url.openStream();
    1.64 +        try {
    1.65 +            return ClassFile.read(in);
    1.66 +        } finally {
    1.67 +            in.close();
    1.68 +        }
    1.69 +    }
    1.70 +
    1.71 +    ClassFile getClassFile(URL url) throws IOException, ConstantPoolException {
    1.72 +            InputStream in = url.openStream();
    1.73 +            try {
    1.74 +                return ClassFile.read(in);
    1.75 +            } finally {
    1.76 +                in.close();
    1.77 +            }
    1.78 +    }
    1.79 +
    1.80 +    /************ Helper annotations counting methods ******************/
    1.81 +    void test(ClassFile cf) {
    1.82 +        test("CLASS",cf, null, null, Attribute.RuntimeVisibleTypeAnnotations, true);
    1.83 +        test("CLASS",cf, null, null, Attribute.RuntimeInvisibleTypeAnnotations, false);
    1.84 +        //RuntimeAnnotations since one annotation can result in two attributes.
    1.85 +        test("CLASS",cf, null, null, Attribute.RuntimeVisibleAnnotations, true);
    1.86 +        test("CLASS",cf, null, null, Attribute.RuntimeInvisibleAnnotations, false);
    1.87 +    }
    1.88 +
    1.89 +    void test(ClassFile cf, Method m) {
    1.90 +        test("METHOD",cf, null, m, Attribute.RuntimeVisibleTypeAnnotations, true);
    1.91 +        test("METHOD",cf, null, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
    1.92 +        test("METHOD",cf, null, m, Attribute.RuntimeVisibleAnnotations, true);
    1.93 +        test("METHOD",cf, null, m, Attribute.RuntimeInvisibleAnnotations, false);
    1.94 +    }
    1.95 +
    1.96 +    void test(ClassFile cf, Field f) {
    1.97 +        test("FIELD",cf, f, null, Attribute.RuntimeVisibleTypeAnnotations, true);
    1.98 +        test("FIELD",cf, f, null, Attribute.RuntimeInvisibleTypeAnnotations, false);
    1.99 +        test("FIELD",cf, f, null, Attribute.RuntimeVisibleAnnotations, true);
   1.100 +        test("FIELD",cf, f, null, Attribute.RuntimeInvisibleAnnotations, false);
   1.101 +    }
   1.102 +
   1.103 +
   1.104 +    // Test the result of Attributes.getIndex according to expectations
   1.105 +    // encoded in the class/field/method name; increment annotations counts.
   1.106 +    void test(String ttype, ClassFile cf, Field f, Method m, String annName, boolean visible) {
   1.107 +        String testtype = ttype;
   1.108 +        String name = null;
   1.109 +        int index = -1;
   1.110 +        Attribute attr = null;
   1.111 +        boolean isTAattr = annName.contains("TypeAnnotations");
   1.112 +        try {
   1.113 +            switch(testtype) {
   1.114 +                case "FIELD":
   1.115 +                    name = f.getName(cf.constant_pool);
   1.116 +                    index = f.attributes.getIndex(cf.constant_pool, annName);
   1.117 +                    if(index!= -1) attr = f.attributes.get(index);
   1.118 +                    break;
   1.119 +                case "METHOD":
   1.120 +                    name = m.getName(cf.constant_pool);
   1.121 +                    index = m.attributes.getIndex(cf.constant_pool, annName);
   1.122 +                    if(index!= -1) attr = m.attributes.get(index);
   1.123 +                    break;
   1.124 +                default:
   1.125 +                    name = cf.getName();
   1.126 +                    index = cf.attributes.getIndex(cf.constant_pool, annName);
   1.127 +                    if(index!= -1) attr = cf.attributes.get(index);
   1.128 +            }
   1.129 +        } catch(ConstantPoolException cpe) { cpe.printStackTrace(); }
   1.130 +
   1.131 +        if (index != -1) {
   1.132 +            assert attr instanceof RuntimeTypeAnnotations_attribute;
   1.133 +            if(isTAattr) { //count RuntimeTypeAnnotations
   1.134 +                RuntimeTypeAnnotations_attribute tAttr =
   1.135 +                        (RuntimeTypeAnnotations_attribute)attr;
   1.136 +                println(testtype + ": " + name + ", " + annName + ": " +
   1.137 +                        tAttr.annotations.length );
   1.138 +                allt += tAttr.annotations.length;
   1.139 +                if (visible)
   1.140 +                    tvisibles += tAttr.annotations.length;
   1.141 +                else
   1.142 +                    tinvisibles += tAttr.annotations.length;
   1.143 +            } else {
   1.144 +                RuntimeAnnotations_attribute tAttr =
   1.145 +                        (RuntimeAnnotations_attribute)attr;
   1.146 +                println(testtype + ": " + name + ", " + annName + ": " +
   1.147 +                        tAttr.annotations.length );
   1.148 +                all += tAttr.annotations.length;
   1.149 +                if (visible)
   1.150 +                    visibles += tAttr.annotations.length;
   1.151 +                else
   1.152 +                    invisibles += tAttr.annotations.length;
   1.153 +            }
   1.154 +        }
   1.155 +    }
   1.156 +
   1.157 +    void countAnnotations() {
   1.158 +        errors=0;
   1.159 +        int expected_allt = expected_tvisibles + expected_tinvisibles;
   1.160 +        int expected_all = expected_visibles + expected_invisibles;
   1.161 +
   1.162 +        if (expected_allt != allt) {
   1.163 +            errors++;
   1.164 +            System.err.println("Failure: expected " + expected_allt +
   1.165 +                    " type annotations but found " + allt);
   1.166 +        }
   1.167 +        if (expected_all != all) {
   1.168 +            errors++;
   1.169 +            System.err.println("Failure: expected " + expected_all +
   1.170 +                    " annotations but found " + all);
   1.171 +        }
   1.172 +        if (expected_tvisibles != tvisibles) {
   1.173 +            errors++;
   1.174 +            System.err.println("Failure: expected " + expected_tvisibles +
   1.175 +                    " typevisible annotations but found " + tvisibles);
   1.176 +        }
   1.177 +
   1.178 +        if (expected_tinvisibles != tinvisibles) {
   1.179 +            errors++;
   1.180 +            System.err.println("Failure: expected " + expected_tinvisibles +
   1.181 +                    " typeinvisible annotations but found " + tinvisibles);
   1.182 +        }
   1.183 +        allt=0;
   1.184 +        tvisibles=0;
   1.185 +        tinvisibles=0;
   1.186 +        all=0;
   1.187 +        visibles=0;
   1.188 +        invisibles=0;
   1.189 +    }
   1.190 +
   1.191 +    int errors;
   1.192 +    int allt;
   1.193 +    int tvisibles;
   1.194 +    int tinvisibles;
   1.195 +    int all;
   1.196 +    int visibles;
   1.197 +    int invisibles;
   1.198 +}

mercurial