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