langtools/test/tools/javac/classfiles/attributes/LineNumberTable/LineNumberTestBase.java
author jjg
Thu, 16 Oct 2014 14:31:37 -0700
changeset 27126 0fa6f84c1195
child 32908 bef9744cf56f
permissions -rw-r--r--
8040131: Implement classfile test for LineNumberTable attribute. Reviewed-by: jjg, shurailine
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
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    26
import java.util.HashSet;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    27
import java.util.List;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    28
import java.util.Set;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    29
import java.util.stream.IntStream;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    30
import java.util.stream.Stream;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    31
import javax.tools.JavaFileObject;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    32
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    33
import static com.sun.tools.classfile.Attribute.Code;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    34
import static com.sun.tools.classfile.Attribute.LineNumberTable;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    35
import static java.lang.String.format;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    36
import static java.util.stream.Collectors.toList;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    37
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
 * Base class for line number table attribute tests.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    40
 * 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
    41
 * 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
    42
 *
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    43
 * @see #test(Container) test methods for more info.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    44
 */
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    45
public class LineNumberTestBase extends TestBase {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    46
    /**
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    47
     * Generates test cases and passes to {@link #test(java.util.List)}
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    48
     * Generation: Replaces placeholder in template by value of enum {@link Constructions}.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    49
     */
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    50
    protected void test(Container container) throws Exception {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    51
        test(container.generate(Constructions.values()));
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    52
    }
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
     * Takes list of test cases. Compiles source of test case.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    56
     * Checks what expected lines are covered by line number table.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    57
     * Does general check of line number table for consistency.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    58
     *
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    59
     * @param testCases list of test cases.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    60
     */
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    61
    protected void test(List<TestCase> testCases) throws Exception {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    62
        boolean failed = false;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    63
        for (TestCase testCase : testCases) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    64
            try {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    65
                Set<Integer> coveredLines = new HashSet<>();
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    66
                for (JavaFileObject file : compile(testCase.src).getClasses().values()) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    67
                    ClassFile classFile = ClassFile.read(file.openInputStream());
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    68
                    for (Method m : classFile.methods) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    69
                        Code_attribute code_attribute = (Code_attribute) m.attributes.get(Code);
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    70
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    71
                        assertEquals(
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    72
                                countAttributes(LineNumberTable, code_attribute.attributes.attrs, classFile.constant_pool),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    73
                                1,
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    74
                                "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
    75
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    76
                        LineNumberTable_attribute tableAttribute =
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    77
                                (LineNumberTable_attribute) code_attribute.attributes.get(LineNumberTable);
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    78
                        checkAttribute(testCase, tableAttribute, code_attribute.code_length);
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    79
                        coveredLines.addAll(
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    80
                                Stream.of(tableAttribute.line_number_table)
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    81
                                        .map(e -> e.line_number)
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    82
                                        .collect(toList()));
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    83
                    }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    84
                }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    85
                assertTrue(coveredLines.containsAll(testCase.expectedLines),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    86
                        format("All significant lines are not covered.%n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    87
                                "Covered: %s%n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    88
                                "Expected: %s%n", coveredLines, testCase.expectedLines));
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    89
            } catch (AssertionFailedException | CompilationException ex) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    90
                System.err.printf("#       %-20s#%n", testCase.getName());
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    91
                int l = 0;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    92
                for (String line : testCase.src.split("\n")) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    93
                    System.err.println(++l + line);
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    94
                }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    95
                System.err.println(ex);
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    96
                failed = true;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    97
                continue;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    98
            }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
    99
            System.err.printf("#       %-20s#%n", testCase.getName());
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   100
            System.err.println("Passed");
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   101
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   102
        if (failed) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   103
            throw new RuntimeException("Test failed");
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   104
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   105
    }
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
    private int countAttributes(String name, Attribute[] attrs, ConstantPool constant_pool) throws ConstantPoolException {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   108
        int i = 0;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   109
        for (Attribute attribute : attrs) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   110
            if (name.equals(attribute.getName(constant_pool))) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   111
                i++;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   112
            }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   113
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   114
        return i;
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
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   117
    private void checkAttribute(TestCase testCase, LineNumberTable_attribute tableAttribute, int code_length) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   118
        assertEquals(tableAttribute.line_number_table_length, tableAttribute.line_number_table.length,
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   119
                "Incorrect line number table length.");
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   120
        //attribute length is offset(line_number_table_length) + element_size*element_count
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   121
        assertEquals(tableAttribute.attribute_length, 2 + 4 * tableAttribute.line_number_table_length,
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   122
                "Incorrect attribute length");
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   123
        testNonEmptyLine(testCase.src.split("\n"), tableAttribute);
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   124
        assertEquals(
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   125
                Stream.of(tableAttribute.line_number_table)
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   126
                        .filter(e -> e.start_pc >= code_length)
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   127
                        .count()
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   128
                , 0L, "StartPC is out of bounds.");
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   129
    }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   130
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
     * Expects line number table point to non empty lines.
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   133
     * 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
   134
     */
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   135
    private void testNonEmptyLine(String[] source, LineNumberTable_attribute attribute) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   136
        for (LineNumberTable_attribute.Entry e : attribute.line_number_table) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   137
            String line = source[e.line_number - 1].trim();
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   138
            assertTrue(!("".equals(line) || line.startsWith("//") || line.startsWith("/*")),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   139
                    format("Expect that line #%d is not empty.%n", e.line_number));
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   140
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   141
    }
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
    protected static enum Constructions implements Container.Construction {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   144
        STORE("testField = 10;"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   145
        LOAD("int p;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   146
                "p = testField;", 2),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   147
        ASSERT("assert false: \"Assert error\";"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   148
        ARRAY("double arr[] = new double[10];"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   149
        ARRAY2("int arr2[][] = {{1,2},{}};"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   150
        LAMBDA("Runnable runnable = () -> \n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   151
                "   System.out.println();"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   152
        LAMBDA_BODY("Runnable runnable = () -> {\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   153
                "   testField++;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   154
                "};"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   155
        METHOD_REFERENCE("Runnable run = System.out::println;\nrun.run();"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   156
        INVOKE_STATIC_METHOD("System.out.println(\"\");"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   157
        INVOKE_INTERFACE("Runnable runnable = new Runnable() {\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   158
                "    @Override\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   159
                "    public void run() {\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   160
                "        System.out.println(\"runnable\");\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   161
                "    }\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   162
                "};\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   163
                "runnable.run();", 1, 7),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   164
        INVOKE_VIRTUAL_METHOD("testMethod();"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   165
        INVOKE_CONSTRUCTOR("new Integer(2);"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   166
        INVOKE_LAMBDA(LAMBDA.getSource() + "\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   167
                "runnable.run();"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   168
        DO_WHILE("do{\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   169
                "    testField++;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   170
                "}while(testField == 1);", 2, 3),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   171
        WHILE("while(testField == 1);"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   172
        FOR("for(int i = 0; i < 3 ; i++);"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   173
        FOR_ENHANCEMENT("int[] ints = {1,2,3};\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   174
                "for(int i:  ints);"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   175
        LABEL("int i=0;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   176
                "label:{\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   177
                "    label2:\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   178
                "    for(;i<5;i++){\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   179
                "        if(i==3)\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   180
                "            break label;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   181
                "        if(i==0){\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   182
                "            continue label2;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   183
                "        }\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   184
                "        return;\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
                "    i++;\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
                , 1, 4, 5, 6, 7, 8, 10, 12),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   189
        CONDITION("int res = \n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   190
                "testField == 2 ?\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   191
                "10\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   192
                ":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
   193
        TRY("try{\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   194
                "    --testField;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   195
                "}\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   196
                "catch(Exception e){\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   197
                "    --testField;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   198
                "}\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   199
                "catch(Error e){\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   200
                "    System.out.print(e);\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   201
                "    throw e;\n " +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   202
                "}\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   203
                "finally{\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   204
                "    ++testField;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   205
                "}", 2, 4, 5, 7, 8, 9, 12),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   206
        TRY_WITH_RESOURCES("try (\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   207
                "    Writer writer = new StringWriter();\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   208
                "    Reader reader = new StringReader(\"\")) {\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   209
                "        writer.write(1);\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   210
                "        reader.read();\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   211
                "} catch (IOException e) {}\n"
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   212
                , 2, 3, 4, 5),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   213
        SYNCHRONIZE("" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   214
                "synchronized(this){\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   215
                "    testField++;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   216
                "}"),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   217
        SWITCH("switch (testField){\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   218
                "case 1:\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   219
                "    break;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   220
                "case 2:\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   221
                "    testField++;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   222
                "default: \n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   223
                "    testField+=2; \n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   224
                "}", 1, 3, 5, 7),
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   225
        SWITCH_STRING(
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   226
                "String str = String.valueOf(testField);\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   227
                        "switch (str){\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   228
                        "case \"1\":\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   229
                        "    break;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   230
                        "case \"2\":\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   231
                        "    testField++;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   232
                        "default: \n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   233
                        "    testField+=2; \n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   234
                        "}", 1, 2, 4, 6, 8);
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   235
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   236
        private final String source;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   237
        private int[] expectedLines;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   238
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   239
        Constructions(String source) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   240
            this.source = source;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   241
            expectedLines = IntStream.rangeClosed(1, source.split("\n").length).toArray();
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   242
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   243
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   244
        Constructions(String source, int... expectedLines) {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   245
            this.source = source;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   246
            this.expectedLines = expectedLines;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   247
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   248
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   249
        @Override
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   250
        public String getSource() {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   251
            return source;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   252
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   253
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   254
        @Override
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   255
        public int[] getExpectedLines() {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   256
            return expectedLines;
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   257
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   258
    }
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
    protected static class MainContainer extends Container {
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
        public MainContainer() {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   263
            super("import java.io.*;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   264
                    "public class Main{\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   265
                    "    public int testField;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   266
                    "\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   267
                    "    public void testMethod() {\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   268
                    "        #SUB_TEMPLATE\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   269
                    "    }\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   270
                    "}");
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   271
        }
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
    protected static class LocalClassContainer extends Container {
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
        public LocalClassContainer() {
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
            super("class Local#LEVEL{\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   279
                    "    public void m(){\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   280
                    "        #SUB_TEMPLATE\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   281
                    "        return;\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   282
                    "    }" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   283
                    "}");
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
    protected static class LambdaContainer extends Container {
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
        public LambdaContainer() {
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   290
            super("Runnable lambda#LEVEL = () -> {\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   291
                    "    #SUB_TEMPLATE\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   292
                    "};\n" +
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   293
                    "lambda#LEVEL.run();\n");
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   294
        }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   295
    }
0fa6f84c1195 8040131: Implement classfile test for LineNumberTable attribute.
jjg
parents:
diff changeset
   296
}