langtools/test/tools/javac/classfiles/attributes/LineNumberTable/LineNumberTestBase.java
author aeremeev
Mon, 28 Sep 2015 13:23:35 +0300
changeset 32908 bef9744cf56f
parent 27126 0fa6f84c1195
child 37754 7b4f67ce5cb5
permissions -rw-r--r--
8081472: Add a mode to the tests for class-file attributes which dumps in-memory sources to disk Reviewed-by: ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27126
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
     1
/*
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
     4
 *
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
     8
 *
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    13
 * accompanied this code).
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    14
 *
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    18
 *
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    21
 * questions.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    22
 */
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    23
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    24
import com.sun.tools.classfile.*;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    25
32908
bef9744cf56f 8081472: Add a mode to the tests for class-file attributes which dumps in-memory sources to disk
aeremeev
parents: 27126
diff changeset
    26
import java.nio.file.Paths;
27126
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    27
import java.util.HashSet;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    28
import java.util.List;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    29
import java.util.Set;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    30
import java.util.stream.IntStream;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    31
import java.util.stream.Stream;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    32
import javax.tools.JavaFileObject;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    33
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    34
import static com.sun.tools.classfile.Attribute.Code;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    35
import static com.sun.tools.classfile.Attribute.LineNumberTable;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    36
import static java.lang.String.format;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    37
import static java.util.stream.Collectors.toList;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    38
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    39
/**
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    40
 * Base class for line number table attribute tests.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    41
 * To add new tests cases(e.g. for new language constructions) you should modify TestData in LineNumberTest.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    42
 * If you plan to add new tests you should extends LineNumberTestBase and invoke one of two "test(...)" methods.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    43
 *
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    44
 * @see #test(Container) test methods for more info.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    45
 */
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    46
public class LineNumberTestBase extends TestBase {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    47
    /**
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    48
     * Generates test cases and passes to {@link #test(java.util.List)}
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    49
     * Generation: Replaces placeholder in template by value of enum {@link Constructions}.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    50
     */
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    51
    protected void test(Container container) throws Exception {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    52
        test(container.generate(Constructions.values()));
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    53
    }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    54
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    55
    /**
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    56
     * Takes list of test cases. Compiles source of test case.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    57
     * Checks what expected lines are covered by line number table.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    58
     * Does general check of line number table for consistency.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    59
     *
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    60
     * @param testCases list of test cases.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    61
     */
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    62
    protected void test(List<TestCase> testCases) throws Exception {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    63
        boolean failed = false;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    64
        for (TestCase testCase : testCases) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    65
            try {
32908
bef9744cf56f 8081472: Add a mode to the tests for class-file attributes which dumps in-memory sources to disk
aeremeev
parents: 27126
diff changeset
    66
                writeToFileIfEnabled(Paths.get(testCase.getName() + ".java"), testCase.src);
27126
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    67
                Set<Integer> coveredLines = new HashSet<>();
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    68
                for (JavaFileObject file : compile(testCase.src).getClasses().values()) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    69
                    ClassFile classFile = ClassFile.read(file.openInputStream());
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    70
                    for (Method m : classFile.methods) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    71
                        Code_attribute code_attribute = (Code_attribute) m.attributes.get(Code);
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    72
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    73
                        assertEquals(
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    74
                                countAttributes(LineNumberTable, code_attribute.attributes.attrs, classFile.constant_pool),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    75
                                1,
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    76
                                "Can be more than one LNT attribute, but javac should generate only one.");
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    77
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    78
                        LineNumberTable_attribute tableAttribute =
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    79
                                (LineNumberTable_attribute) code_attribute.attributes.get(LineNumberTable);
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    80
                        checkAttribute(testCase, tableAttribute, code_attribute.code_length);
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    81
                        coveredLines.addAll(
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    82
                                Stream.of(tableAttribute.line_number_table)
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    83
                                        .map(e -> e.line_number)
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    84
                                        .collect(toList()));
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    85
                    }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    86
                }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    87
                assertTrue(coveredLines.containsAll(testCase.expectedLines),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    88
                        format("All significant lines are not covered.%n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    89
                                "Covered: %s%n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    90
                                "Expected: %s%n", coveredLines, testCase.expectedLines));
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    91
            } catch (AssertionFailedException | CompilationException ex) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    92
                System.err.printf("#       %-20s#%n", testCase.getName());
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    93
                int l = 0;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    94
                for (String line : testCase.src.split("\n")) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    95
                    System.err.println(++l + line);
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    96
                }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    97
                System.err.println(ex);
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    98
                failed = true;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    99
                continue;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   100
            }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   101
            System.err.printf("#       %-20s#%n", testCase.getName());
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   102
            System.err.println("Passed");
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   103
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   104
        if (failed) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   105
            throw new RuntimeException("Test failed");
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   106
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   107
    }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   108
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   109
    private int countAttributes(String name, Attribute[] attrs, ConstantPool constant_pool) throws ConstantPoolException {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   110
        int i = 0;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   111
        for (Attribute attribute : attrs) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   112
            if (name.equals(attribute.getName(constant_pool))) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   113
                i++;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   114
            }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   115
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   116
        return i;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   117
    }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   118
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   119
    private void checkAttribute(TestCase testCase, LineNumberTable_attribute tableAttribute, int code_length) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   120
        assertEquals(tableAttribute.line_number_table_length, tableAttribute.line_number_table.length,
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   121
                "Incorrect line number table length.");
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   122
        //attribute length is offset(line_number_table_length) + element_size*element_count
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   123
        assertEquals(tableAttribute.attribute_length, 2 + 4 * tableAttribute.line_number_table_length,
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   124
                "Incorrect attribute length");
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   125
        testNonEmptyLine(testCase.src.split("\n"), tableAttribute);
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   126
        assertEquals(
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   127
                Stream.of(tableAttribute.line_number_table)
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   128
                        .filter(e -> e.start_pc >= code_length)
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   129
                        .count()
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   130
                , 0L, "StartPC is out of bounds.");
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   131
    }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   132
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   133
    /**
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   134
     * Expects line number table point to non empty lines.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   135
     * The method can't recognize commented lines as empty(insensible) in case of multiline comment.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   136
     */
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   137
    private void testNonEmptyLine(String[] source, LineNumberTable_attribute attribute) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   138
        for (LineNumberTable_attribute.Entry e : attribute.line_number_table) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   139
            String line = source[e.line_number - 1].trim();
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   140
            assertTrue(!("".equals(line) || line.startsWith("//") || line.startsWith("/*")),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   141
                    format("Expect that line #%d is not empty.%n", e.line_number));
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   142
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   143
    }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   144
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   145
    protected static enum Constructions implements Container.Construction {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   146
        STORE("testField = 10;"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   147
        LOAD("int p;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   148
                "p = testField;", 2),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   149
        ASSERT("assert false: \"Assert error\";"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   150
        ARRAY("double arr[] = new double[10];"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   151
        ARRAY2("int arr2[][] = {{1,2},{}};"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   152
        LAMBDA("Runnable runnable = () -> \n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   153
                "   System.out.println();"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   154
        LAMBDA_BODY("Runnable runnable = () -> {\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   155
                "   testField++;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   156
                "};"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   157
        METHOD_REFERENCE("Runnable run = System.out::println;\nrun.run();"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   158
        INVOKE_STATIC_METHOD("System.out.println(\"\");"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   159
        INVOKE_INTERFACE("Runnable runnable = new Runnable() {\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   160
                "    @Override\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   161
                "    public void run() {\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   162
                "        System.out.println(\"runnable\");\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   163
                "    }\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   164
                "};\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   165
                "runnable.run();", 1, 7),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   166
        INVOKE_VIRTUAL_METHOD("testMethod();"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   167
        INVOKE_CONSTRUCTOR("new Integer(2);"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   168
        INVOKE_LAMBDA(LAMBDA.getSource() + "\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   169
                "runnable.run();"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   170
        DO_WHILE("do{\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   171
                "    testField++;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   172
                "}while(testField == 1);", 2, 3),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   173
        WHILE("while(testField == 1);"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   174
        FOR("for(int i = 0; i < 3 ; i++);"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   175
        FOR_ENHANCEMENT("int[] ints = {1,2,3};\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   176
                "for(int i:  ints);"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   177
        LABEL("int i=0;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   178
                "label:{\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   179
                "    label2:\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   180
                "    for(;i<5;i++){\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   181
                "        if(i==3)\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   182
                "            break label;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   183
                "        if(i==0){\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   184
                "            continue label2;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   185
                "        }\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   186
                "        return;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   187
                "    }\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   188
                "    i++;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   189
                "}\n"
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   190
                , 1, 4, 5, 6, 7, 8, 10, 12),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   191
        CONDITION("int res = \n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   192
                "testField == 2 ?\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   193
                "10\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   194
                ":9;", 1, 3, 4), // see issue https://bugs.openjdk.java.net/browse/JDK-8050993
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   195
        TRY("try{\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   196
                "    --testField;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   197
                "}\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   198
                "catch(Exception e){\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   199
                "    --testField;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   200
                "}\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   201
                "catch(Error e){\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   202
                "    System.out.print(e);\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   203
                "    throw e;\n " +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   204
                "}\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   205
                "finally{\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   206
                "    ++testField;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   207
                "}", 2, 4, 5, 7, 8, 9, 12),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   208
        TRY_WITH_RESOURCES("try (\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   209
                "    Writer writer = new StringWriter();\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   210
                "    Reader reader = new StringReader(\"\")) {\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   211
                "        writer.write(1);\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   212
                "        reader.read();\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   213
                "} catch (IOException e) {}\n"
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   214
                , 2, 3, 4, 5),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   215
        SYNCHRONIZE("" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   216
                "synchronized(this){\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   217
                "    testField++;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   218
                "}"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   219
        SWITCH("switch (testField){\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   220
                "case 1:\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   221
                "    break;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   222
                "case 2:\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   223
                "    testField++;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   224
                "default: \n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   225
                "    testField+=2; \n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   226
                "}", 1, 3, 5, 7),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   227
        SWITCH_STRING(
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   228
                "String str = String.valueOf(testField);\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   229
                        "switch (str){\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   230
                        "case \"1\":\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   231
                        "    break;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   232
                        "case \"2\":\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   233
                        "    testField++;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   234
                        "default: \n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   235
                        "    testField+=2; \n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   236
                        "}", 1, 2, 4, 6, 8);
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   237
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   238
        private final String source;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   239
        private int[] expectedLines;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   240
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   241
        Constructions(String source) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   242
            this.source = source;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   243
            expectedLines = IntStream.rangeClosed(1, source.split("\n").length).toArray();
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   244
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   245
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   246
        Constructions(String source, int... expectedLines) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   247
            this.source = source;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   248
            this.expectedLines = expectedLines;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   249
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   250
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   251
        @Override
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   252
        public String getSource() {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   253
            return source;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   254
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   255
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   256
        @Override
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   257
        public int[] getExpectedLines() {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   258
            return expectedLines;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   259
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   260
    }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   261
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   262
    protected static class MainContainer extends Container {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   263
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   264
        public MainContainer() {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   265
            super("import java.io.*;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   266
                    "public class Main{\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   267
                    "    public int testField;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   268
                    "\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   269
                    "    public void testMethod() {\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   270
                    "        #SUB_TEMPLATE\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   271
                    "    }\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   272
                    "}");
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   273
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   274
    }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   275
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   276
    protected static class LocalClassContainer extends Container {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   277
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   278
        public LocalClassContainer() {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   279
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   280
            super("class Local#LEVEL{\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   281
                    "    public void m(){\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   282
                    "        #SUB_TEMPLATE\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   283
                    "        return;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   284
                    "    }" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   285
                    "}");
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   286
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   287
    }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   288
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   289
    protected static class LambdaContainer extends Container {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   290
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   291
        public LambdaContainer() {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   292
            super("Runnable lambda#LEVEL = () -> {\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   293
                    "    #SUB_TEMPLATE\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   294
                    "};\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   295
                    "lambda#LEVEL.run();\n");
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   296
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   297
    }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   298
}