langtools/test/tools/javap/StackMapTableTest.java
author katleman
Thu, 21 Aug 2014 14:16:14 -0700
changeset 25878 6d561031123e
parent 23112 4fa26477332d
child 30730 d3ce7619db2c
permissions -rw-r--r--
Added tag jdk9-b27 for changeset 98ce0879ab4c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23112
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
     1
/*
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
     4
 *
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
     8
 *
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    13
 * accompanied this code).
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    14
 *
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    18
 *
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    21
 * questions.
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    22
 */
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    23
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    24
/*
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    25
 * @test
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    26
 * @bug 8033930 8033913
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    27
 * @summary bad formatting of StackMapTable entries
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    28
 */
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    29
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    30
import java.io.*;
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    31
import java.util.*;
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    32
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    33
public class StackMapTableTest {
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    34
    public static void main(String... args) throws Exception {
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    35
        new StackMapTableTest().run();
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    36
    }
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    37
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    38
    void run() throws Exception {
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    39
        String testClasses = System.getProperty("test.classes");
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    40
        String out = javap("-v", "-classpath", testClasses, A.class.getName());
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    41
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    42
        String nl = System.getProperty("line.separator");
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    43
        out = out.replaceAll(nl, "\n");
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    44
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    45
        if (out.contains("\n\n\n"))
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    46
            error("double blank line found");
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    47
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    48
        String expect =
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    49
            "      StackMapTable: number_of_entries = 2\n" +
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    50
            "        frame_type = 252 /* append */\n" +
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    51
            "          offset_delta = 2\n" +
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    52
            "          locals = [ int ]\n" +
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    53
            "        frame_type = 250 /* chop */\n" +
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    54
            "          offset_delta = 18\n";
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    55
        if (!out.contains(expect))
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    56
            error("expected text not found");
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    57
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    58
        if (errors > 0)
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    59
            throw new Exception(errors + " errors found");
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    60
    }
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    61
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    62
    String javap(String... args) throws Exception {
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    63
        StringWriter sw = new StringWriter();
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    64
        PrintWriter out = new PrintWriter(sw);
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    65
        int rc = com.sun.tools.javap.Main.run(args, out);
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    66
        out.close();
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    67
        System.out.println(sw.toString());
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    68
        if (rc < 0)
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    69
            throw new Exception("javap exited, rc=" + rc);
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    70
        return sw.toString();
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    71
    }
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    72
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    73
    void error(String msg) {
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    74
        System.out.println("Error: " + msg);
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    75
        errors++;
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    76
    }
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    77
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    78
    int errors;
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    79
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    80
    /** Simple test class to run through javap. */
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    81
    public class A {
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    82
        public void a() {
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    83
            for (int i = 0; i < 10; i++) {
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    84
                System.out.println(i);
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    85
            }
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    86
        }
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    87
        public void b() {
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    88
        }
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    89
        public void c() {
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    90
        }
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    91
    }
4fa26477332d 8033930: Unnecessary new line after the StackMapTable
jjg
parents:
diff changeset
    92
}