langtools/test/tools/javac/classfiles/attributes/LocalVariableTable/LocalVariableTestBase.java
author ksrini
Wed, 23 Jul 2014 09:19:23 -0700
changeset 25699 7ca97d2d0405
parent 24223 90b07e33a49d
child 25845 14935053bb07
permissions -rw-r--r--
8050319: LocalVariableTestBase has unexpected dependency on LocalVariableTableTest Reviewed-by: jjg, ksrini Contributed-by: andrey.x.nazarov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     1
/*
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     4
 *
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     8
 *
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    13
 * accompanied this code).
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    14
 *
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    18
 *
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    21
 * questions.
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    22
 */
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    23
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    24
import com.sun.tools.classfile.*;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    25
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    26
import java.io.IOException;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    27
import java.lang.annotation.Repeatable;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    28
import java.lang.annotation.Retention;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    29
import java.lang.annotation.RetentionPolicy;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    30
import java.util.Collection;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    31
import java.util.HashMap;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    32
import java.util.List;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    33
import java.util.Map;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    34
import java.util.stream.Stream;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    35
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    36
import static java.lang.String.format;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    37
import static java.util.stream.Collectors.*;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    38
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    39
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    40
public abstract class LocalVariableTestBase extends TestBase {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    41
    public static final int DEFAULT_SCOPE = 0;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    42
    private final ClassFile classFile;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    43
    private final Class<?> clazz;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    44
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    45
    protected abstract List<VariableTable> getVariableTables(Code_attribute codeAttribute);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    46
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    47
    public LocalVariableTestBase(Class<?> clazz) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    48
        this.clazz = clazz;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    49
        try {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    50
            this.classFile = ClassFile.read(getClassFile(clazz));
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    51
        } catch (IOException | ConstantPoolException e) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    52
            throw new IllegalArgumentException("Can't read classfile for specified class", e);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    53
        }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    54
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    55
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    56
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    57
    //info in the LocalVariableTable attribute is compared against expected info stored in annotations
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    58
    public void test() throws IOException {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    59
        List<java.lang.reflect.Method> testMethods = Stream.of(clazz.getDeclaredMethods())
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    60
                .filter(m -> m.getAnnotationsByType(ExpectedLocals.class).length > 0)
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    61
                .collect(toList());
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    62
        int failed = 0;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    63
        for (java.lang.reflect.Method method : testMethods) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    64
            try {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    65
                Map<String, String> expectedLocals2Types = new HashMap<>();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    66
                Map<String, Integer> sig2scope = new HashMap<>();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    67
                for (ExpectedLocals anno : method.getDeclaredAnnotationsByType(ExpectedLocals.class)) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    68
                    expectedLocals2Types.put(anno.name(), anno.type());
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    69
                    sig2scope.put(anno.name() + "&" + anno.type(), anno.scope());
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    70
                }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    71
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    72
                test(method.getName(), expectedLocals2Types, sig2scope);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    73
            } catch (AssertionFailedException ex) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    74
                System.err.printf("Test %s failed.%n", method.getName());
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    75
                ex.printStackTrace();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    76
                failed++;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    77
            }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    78
        }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    79
        if (failed > 0)
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    80
            throw new RuntimeException(format("Failed %d out of %d. See logs.", failed, testMethods.size()));
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    81
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    82
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    83
    public void test(String methodName, Map<String, String> expectedLocals2Types, Map<String, Integer> sig2scope)
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    84
            throws IOException {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    85
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    86
        for (Method m : classFile.methods) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    87
            String mName = getString(m.name_index);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    88
            if (methodName.equals(mName)) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    89
                System.out.println("Testing local variable table in method " + mName);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    90
                Code_attribute code_attribute = (Code_attribute) m.attributes.get(Attribute.Code);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    91
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    92
                List<? extends VariableTable> variableTables = getVariableTables(code_attribute);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    93
                generalLocalVariableTableCheck(variableTables);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    94
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    95
                List<VariableTable.Entry> entries = variableTables.stream()
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    96
                        .flatMap(table -> table.entries().stream())
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    97
                        .collect(toList());
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    98
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
    99
                generalEntriesCheck(entries, code_attribute);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   100
                assertIndexesAreUnique(entries, sig2scope);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   101
                checkNamesAndTypes(entries, expectedLocals2Types);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   102
                checkDoubleAndLongIndexes(entries, sig2scope, code_attribute.max_locals);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   103
            }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   104
        }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   105
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   106
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   107
    private void generalLocalVariableTableCheck(List<? extends VariableTable> variableTables) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   108
        for (VariableTable localTable : variableTables) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   109
            //only one per variable.
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   110
            assertEquals(localTable.localVariableTableLength(),
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   111
                    localTable.entries().size(), "Incorrect local variable table length");
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   112
            //attribute length is offset(line_number_table_length) + element_size*element_count
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   113
            assertEquals(localTable.attributeLength(),
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   114
                    2 + (5 * 2) * localTable.localVariableTableLength(), "Incorrect attribute length");
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   115
        }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   116
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   117
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   118
    private void generalEntriesCheck(List<VariableTable.Entry> entries, Code_attribute code_attribute) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   119
        for (VariableTable.Entry e : entries) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   120
            assertTrue(e.index() >= 0 && e.index() < code_attribute.max_locals,
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   121
                    "Index " + e.index() + " out of variable array. Size of array is " + code_attribute.max_locals);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   122
            assertTrue(e.startPC() >= 0, "StartPC is less then 0. StartPC = " + e.startPC());
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   123
            assertTrue(e.length() >= 0, "Length is less then 0. Length = " + e.length());
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   124
            assertTrue(e.startPC() + e.length() <= code_attribute.code_length,
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   125
                    format("StartPC+Length > code length.%n" +
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   126
                            "%s%n" +
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   127
                            "code_length = %s"
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   128
                            , e, code_attribute.code_length));
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   129
        }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   130
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   131
25699
7ca97d2d0405 8050319: LocalVariableTestBase has unexpected dependency on LocalVariableTableTest
ksrini
parents: 24223
diff changeset
   132
    private void checkNamesAndTypes(List<VariableTable.Entry> entries,
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   133
                                    Map<String, String> expectedLocals2Types) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   134
        Map<String, List<String>> actualNames2Types = entries.stream()
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   135
                .collect(
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   136
                        groupingBy(VariableTable.Entry::name,
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   137
                                mapping(VariableTable.Entry::type, toList())));
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   138
        for (Map.Entry<String, String> name2type : expectedLocals2Types.entrySet()) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   139
            String name = name2type.getKey();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   140
            String type = name2type.getValue();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   141
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   142
            assertTrue(actualNames2Types.containsKey(name),
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   143
                    format("There is no record for local variable %s%nEntries: %s", name, entries));
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   144
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   145
            assertTrue(actualNames2Types.get(name).contains(type),
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   146
                    format("Types are different for local variable %s%nExpected type: %s%nActual type: %s",
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   147
                            name, type, actualNames2Types.get(name)));
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   148
        }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   149
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   150
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   151
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   152
    private void assertIndexesAreUnique(Collection<VariableTable.Entry> entries, Map<String, Integer> scopes) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   153
        //check every scope separately
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   154
        Map<Object, List<VariableTable.Entry>> entriesByScope = groupByScope(entries, scopes);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   155
        for (Map.Entry<Object, List<VariableTable.Entry>> mapEntry : entriesByScope.entrySet()) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   156
            mapEntry.getValue().stream()
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   157
                    .collect(groupingBy(VariableTable.Entry::index))
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   158
                    .entrySet()
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   159
                    .forEach(e ->
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   160
                            assertTrue(e.getValue().size() == 1,
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   161
                                    "Multiple variables point to the same index in common scope. " + e.getValue()));
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   162
        }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   163
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   164
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   165
25699
7ca97d2d0405 8050319: LocalVariableTestBase has unexpected dependency on LocalVariableTableTest
ksrini
parents: 24223
diff changeset
   166
    private void checkDoubleAndLongIndexes(Collection<VariableTable.Entry> entries,
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   167
                                           Map<String, Integer> scopes, int maxLocals) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   168
        //check every scope separately
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   169
        Map<Object, List<VariableTable.Entry>> entriesByScope = groupByScope(entries, scopes);
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   170
        for (List<VariableTable.Entry> entryList : entriesByScope.values()) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   171
            Map<Integer, VariableTable.Entry> index2Entry = entryList.stream()
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   172
                    .collect(toMap(VariableTable.Entry::index, e -> e));
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   173
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   174
            entryList.stream()
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   175
                    .filter(e -> "J".equals(e.type()) || "D".equals(e.type()))
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   176
                    .forEach(e -> {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   177
                        assertTrue(e.index() + 1 < maxLocals,
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   178
                                format("Index %s is out of variable array. Long and double occupy 2 cells." +
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   179
                                        " Size of array is %d", e.index() + 1, maxLocals));
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   180
                        assertTrue(!index2Entry.containsKey(e.index() + 1),
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   181
                                format("An entry points to the second cell of long/double entry.%n%s%n%s", e,
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   182
                                        index2Entry.get(e.index() + 1)));
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   183
                    });
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   184
        }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   185
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   186
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   187
    private Map<Object, List<VariableTable.Entry>> groupByScope(
25699
7ca97d2d0405 8050319: LocalVariableTestBase has unexpected dependency on LocalVariableTableTest
ksrini
parents: 24223
diff changeset
   188
            Collection<VariableTable.Entry> entries, Map<String, Integer> scopes) {
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   189
        return entries.stream().collect(groupingBy(e -> scopes.getOrDefault(e.name() + "&" + e.type(), DEFAULT_SCOPE)));
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   190
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   191
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   192
    protected String getString(int i) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   193
        try {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   194
            return classFile.constant_pool.getUTF8Info(i).value;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   195
        } catch (ConstantPool.InvalidIndex | ConstantPool.UnexpectedEntry ex) {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   196
            ex.printStackTrace();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   197
            throw new AssertionFailedException("Issue while reading constant pool");
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   198
        }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   199
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   200
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   201
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   202
    interface VariableTable {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   203
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   204
        int localVariableTableLength();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   205
25699
7ca97d2d0405 8050319: LocalVariableTestBase has unexpected dependency on LocalVariableTableTest
ksrini
parents: 24223
diff changeset
   206
        List<VariableTable.Entry> entries();
24223
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   207
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   208
        int attributeLength();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   209
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   210
        interface Entry {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   211
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   212
            int index();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   213
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   214
            int startPC();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   215
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   216
            int length();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   217
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   218
            String name();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   219
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   220
            String type();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   221
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   222
            default String dump() {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   223
                return format("Entry{" +
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   224
                        "%n    name    = %s" +
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   225
                        "%n    type    = %s" +
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   226
                        "%n    index   = %d" +
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   227
                        "%n    startPC = %d" +
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   228
                        "%n    length  = %d" +
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   229
                        "%n}", name(), type(), index(), startPC(), length());
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   230
            }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   231
        }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   232
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   233
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   234
    @Retention(RetentionPolicy.RUNTIME)
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   235
    @Repeatable(Container.class)
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   236
    @interface ExpectedLocals {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   237
        String name();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   238
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   239
        String type();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   240
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   241
        //variables from different scopes can share local variable table index and/or name.
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   242
        int scope() default DEFAULT_SCOPE;
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   243
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   244
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   245
    @Retention(RetentionPolicy.RUNTIME)
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   246
    @interface Container {
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   247
        ExpectedLocals[] value();
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   248
    }
90b07e33a49d 8040097: Implement classfile tests for LocalVariableTable and LocalVariableTypeTable attribute.
anazarov
parents:
diff changeset
   249
}