test/langtools/tools/javac/annotations/typeAnnotations/classfile/ClassfileTestHelper.java
author jlahoda
Wed, 27 Nov 2019 09:00:01 +0100
changeset 59285 7799a51dbe30
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231826: Implement javac changes for pattern matching for instanceof Reviewed-by: mcimadamore Contributed-by: brian.goetz@oracle.com, gavin.bierman@oracle.com, maurizio.cimadamore@oracle.com, srikanth.adayapalam@oracle.com, vicente.romero@oracle.com, jan.lahoda@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
     1
/*
59285
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 47216
diff changeset
     2
 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
     4
 *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
     8
 *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    13
 * accompanied this code).
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    14
 *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    18
 *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    21
 * questions.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    22
 */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    23
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    24
import java.lang.annotation.*;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    25
import java.io.*;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    26
import java.net.URL;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    27
import java.util.List;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    28
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    29
import com.sun.tools.classfile.*;
59285
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 47216
diff changeset
    30
import java.util.ArrayList;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    31
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    32
public class ClassfileTestHelper {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    33
    int expected_tinvisibles = 0;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    34
    int expected_tvisibles = 0;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    35
    int expected_invisibles = 0;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    36
    int expected_visibles = 0;
59285
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 47216
diff changeset
    37
    List<String> extraOptions = List.of();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    38
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    39
    //Makes debugging much easier. Set to 'false' for less output.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    40
    public Boolean verbose = true;
24895
dd091d389fbf 8027262: Determine location for type annotations earlier in compiler pipeline
emc
parents: 23393
diff changeset
    41
    void println(String msg) { if (verbose) System.err.println(msg); }
dd091d389fbf 8027262: Determine location for type annotations earlier in compiler pipeline
emc
parents: 23393
diff changeset
    42
    void print(String msg) { if (verbose) System.err.print(msg); }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    43
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    44
    File writeTestFile(String fname, String source) throws IOException {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    45
      File f = new File(fname);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    46
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    47
        out.println(source);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    48
        out.close();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    49
        return f;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    50
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    51
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    52
    File compile(File f) {
59285
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 47216
diff changeset
    53
        List<String> options = new ArrayList<>(List.of("-g", f.getPath()));
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 47216
diff changeset
    54
        options.addAll(extraOptions);
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 47216
diff changeset
    55
        int rc = com.sun.tools.javac.Main.compile(options.toArray(new String[0]));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    56
        if (rc != 0)
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    57
            throw new Error("compilation failed. rc=" + rc);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    58
        String path = f.getPath();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    59
        return new File(path.substring(0, path.length() - 5) + ".class");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    60
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    61
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    62
    ClassFile getClassFile(String name) throws IOException, ConstantPoolException {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    63
        URL url = getClass().getResource(name);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    64
        InputStream in = url.openStream();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    65
        try {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    66
            return ClassFile.read(in);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    67
        } finally {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    68
            in.close();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    69
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    70
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    71
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    72
    ClassFile getClassFile(URL url) throws IOException, ConstantPoolException {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
    73
        InputStream in = url.openStream();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
    74
        try {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
    75
            return ClassFile.read(in);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
    76
        } finally {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
    77
            in.close();
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
    78
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    79
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    80
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    81
    /************ Helper annotations counting methods ******************/
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    82
    void test(ClassFile cf) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    83
        test("CLASS",cf, null, null, Attribute.RuntimeVisibleTypeAnnotations, true);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    84
        test("CLASS",cf, null, null, Attribute.RuntimeInvisibleTypeAnnotations, false);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    85
        //RuntimeAnnotations since one annotation can result in two attributes.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    86
        test("CLASS",cf, null, null, Attribute.RuntimeVisibleAnnotations, true);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    87
        test("CLASS",cf, null, null, Attribute.RuntimeInvisibleAnnotations, false);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    88
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
    89
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
    90
    void test(ClassFile cf, Field f, Boolean local) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
    91
        if (!local) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
    92
            test("FIELD",cf, f, null, Attribute.RuntimeVisibleTypeAnnotations, true);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
    93
            test("FIELD",cf, f, null, Attribute.RuntimeInvisibleTypeAnnotations, false);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
    94
            test("FIELD",cf, f, null, Attribute.RuntimeVisibleAnnotations, true);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
    95
            test("FIELD",cf, f, null, Attribute.RuntimeInvisibleAnnotations, false);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
    96
        } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
    97
            test("CODE",cf, f, null, Attribute.RuntimeVisibleTypeAnnotations, true);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
    98
            test("CODE",cf, f, null, Attribute.RuntimeInvisibleTypeAnnotations, false);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
    99
            test("CODE",cf, f, null, Attribute.RuntimeVisibleAnnotations, true);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   100
            test("CODE",cf, f, null, Attribute.RuntimeInvisibleAnnotations, false);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   101
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   102
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   103
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   104
    void test(ClassFile cf, Field f) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   105
        test(cf, f, false);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   106
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   107
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   108
    // 'local' determines whether to look for annotations in code attribute or not.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   109
    void test(ClassFile cf, Method m, Boolean local) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   110
        if (!local) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   111
            test("METHOD",cf, null, m, Attribute.RuntimeVisibleTypeAnnotations, true);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   112
            test("METHOD",cf, null, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   113
            test("METHOD",cf, null, m, Attribute.RuntimeVisibleAnnotations, true);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   114
            test("METHOD",cf, null, m, Attribute.RuntimeInvisibleAnnotations, false);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   115
        } else  {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   116
            test("MCODE",cf, null, m, Attribute.RuntimeVisibleTypeAnnotations, true);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   117
            test("MCODE",cf, null, m, Attribute.RuntimeInvisibleTypeAnnotations, false);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   118
            test("MCODE",cf, null, m, Attribute.RuntimeVisibleAnnotations, true);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   119
            test("MCODE",cf, null, m, Attribute.RuntimeInvisibleAnnotations, false);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   120
        }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   121
    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   122
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   123
    // default to not looking in code attribute
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   124
    void test(ClassFile cf, Method m ) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   125
        test(cf, m, false);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   126
    }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   127
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   128
    // Test the result of Attributes.getIndex according to expectations
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   129
    // encoded in the class/field/method name; increment annotations counts.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   130
    void test(String ttype, ClassFile cf, Field f, Method m, String annName, boolean visible) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   131
        String testtype = ttype;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   132
        String name = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   133
        int index = -1;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   134
        Attribute attr = null;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   135
        Code_attribute cAttr = null;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   136
        boolean isTAattr = annName.contains("TypeAnnotations");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   137
        try {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   138
            switch(testtype) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   139
                case "FIELD":
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   140
                    name = f.getName(cf.constant_pool);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   141
                    index = f.attributes.getIndex(cf.constant_pool, annName);
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   142
                    if(index!= -1)
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   143
                        attr = f.attributes.get(index);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   144
                    break;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   145
                case "CODE":
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   146
                    name = f.getName(cf.constant_pool);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   147
                    //fetch index of and code attribute and annotations from code attribute.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   148
                    index = cf.attributes.getIndex(cf.constant_pool, Attribute.Code);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   149
                    if(index!= -1) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   150
                        attr = cf.attributes.get(index);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   151
                        assert attr instanceof Code_attribute;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   152
                        cAttr = (Code_attribute)attr;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   153
                        index = cAttr.attributes.getIndex(cf.constant_pool, annName);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   154
                        if(index!= -1)
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   155
                            attr = cAttr.attributes.get(index);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   156
                    }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   157
                    break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   158
                case "METHOD":
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   159
                    name = m.getName(cf.constant_pool);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   160
                    index = m.attributes.getIndex(cf.constant_pool, annName);
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   161
                    if(index!= -1)
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   162
                        attr = m.attributes.get(index);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   163
                    break;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   164
                case "MCODE":
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   165
                    name = m.getName(cf.constant_pool);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   166
                    //fetch index of and code attribute and annotations from code attribute.
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   167
                    index = m.attributes.getIndex(cf.constant_pool, Attribute.Code);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   168
                    if(index!= -1) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   169
                        attr = m.attributes.get(index);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   170
                        assert attr instanceof Code_attribute;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   171
                        cAttr = (Code_attribute)attr;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   172
                        index = cAttr.attributes.getIndex(cf.constant_pool, annName);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   173
                        if(index!= -1)
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   174
                            attr = cAttr.attributes.get(index);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 15557
diff changeset
   175
                    }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   176
                    break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   177
                default:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   178
                    name = cf.getName();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   179
                    index = cf.attributes.getIndex(cf.constant_pool, annName);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   180
                    if(index!= -1) attr = cf.attributes.get(index);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   181
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   182
        } catch(ConstantPoolException cpe) { cpe.printStackTrace(); }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   183
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   184
        if (index != -1) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   185
            if(isTAattr) { //count RuntimeTypeAnnotations
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   186
                RuntimeTypeAnnotations_attribute tAttr =
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   187
                        (RuntimeTypeAnnotations_attribute)attr;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   188
                println(testtype + ": " + name + ", " + annName + ": " +
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   189
                        tAttr.annotations.length );
21041
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 17578
diff changeset
   190
                if (tAttr.annotations.length > 0) {
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 17578
diff changeset
   191
                    for (int i = 0; i < tAttr.annotations.length; i++) {
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 17578
diff changeset
   192
                        println("  types:" + tAttr.annotations[i].position.type);
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 17578
diff changeset
   193
                    }
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 17578
diff changeset
   194
                } else {
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 17578
diff changeset
   195
                    println("");
99f5e5e97425 8026564: import changes from type-annotations forest
jjg
parents: 17578
diff changeset
   196
                }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   197
                allt += tAttr.annotations.length;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   198
                if (visible)
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   199
                    tvisibles += tAttr.annotations.length;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   200
                else
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   201
                    tinvisibles += tAttr.annotations.length;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   202
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   203
                RuntimeAnnotations_attribute tAttr =
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   204
                        (RuntimeAnnotations_attribute)attr;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   205
                println(testtype + ": " + name + ", " + annName + ": " +
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   206
                        tAttr.annotations.length );
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   207
                all += tAttr.annotations.length;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   208
                if (visible)
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   209
                    visibles += tAttr.annotations.length;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   210
                else
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   211
                    invisibles += tAttr.annotations.length;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   212
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   213
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   214
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   215
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   216
    void countAnnotations() {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   217
        errors=0;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   218
        int expected_allt = expected_tvisibles + expected_tinvisibles;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   219
        int expected_all = expected_visibles + expected_invisibles;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   220
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   221
        if (expected_allt != allt) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   222
            errors++;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   223
            System.err.println("Failure: expected " + expected_allt +
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   224
                    " type annotations but found " + allt);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   225
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   226
        if (expected_all != all) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   227
            errors++;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   228
            System.err.println("Failure: expected " + expected_all +
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   229
                    " annotations but found " + all);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   230
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   231
        if (expected_tvisibles != tvisibles) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   232
            errors++;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   233
            System.err.println("Failure: expected " + expected_tvisibles +
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   234
                    " typevisible annotations but found " + tvisibles);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   235
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   236
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   237
        if (expected_tinvisibles != tinvisibles) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   238
            errors++;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   239
            System.err.println("Failure: expected " + expected_tinvisibles +
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   240
                    " typeinvisible annotations but found " + tinvisibles);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   241
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   242
        allt=0;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   243
        tvisibles=0;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   244
        tinvisibles=0;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   245
        all=0;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   246
        visibles=0;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   247
        invisibles=0;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   248
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   249
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   250
    int errors;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   251
    int allt;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   252
    int tvisibles;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   253
    int tinvisibles;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   254
    int all;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   255
    int visibles;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   256
    int invisibles;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents:
diff changeset
   257
}