T8032814.java revision 3294:9adfb22ff08f
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 8032814
27 * @summary LineNumberTable/LocalVariableTable tables duplication for the
28 *          "-v -l" combination of options
29 * @modules jdk.jdeps/com.sun.tools.javap
30 * @compile -g T8032814.java
31 * @run main T8032814
32 */
33
34import java.io.*;
35import java.util.*;
36
37public class T8032814 {
38    public static void main(String... args) throws Exception {
39        new T8032814().run();
40    }
41
42    void run() throws Exception {
43        Class<?> clazz = T8032814.class;
44        int count = clazz.getDeclaredConstructors().length
45                + clazz.getDeclaredMethods().length;
46        test(clazz, 0);
47        test(clazz, count, "-v");
48        test(clazz, count, "-l");
49        test(clazz, count, "-v", "-l");
50
51        if (errors > 0)
52            throw new Exception(errors + " errors occurred");
53    }
54
55    void test(Class<?> clazz, int expectedCount, String... opts) throws Exception {
56        System.err.println("test class " + clazz.getName() + " " + Arrays.asList(opts) + ": expect: " + expectedCount);
57        List<String> args = new ArrayList<String>();
58        args.addAll(Arrays.asList(opts));
59        args.addAll(Arrays.asList("-classpath", System.getProperty("test.classes")));
60        args.add(clazz.getName());
61        StringWriter sw = new StringWriter();
62        PrintWriter pw = new PrintWriter(sw);
63        int rc = com.sun.tools.javap.Main.run(args.toArray(new String[args.size()]), pw);
64        pw.close();
65        String out = sw.toString();
66        if (rc != 0)
67            throw new Exception("javap failed unexpectedly: rc=" + rc);
68
69        int lntCount = 0, lvtCount = 0;
70        for (String line: out.split("[\r\n]+")) {
71            if (line.matches("^ *LineNumberTable:$"))
72                lntCount++;
73            if (line.matches("^ *LocalVariableTable:$"))
74                lvtCount++;
75        }
76        checkEqual("LineNumberTable", lntCount, expectedCount);
77        checkEqual("LocalVariableTable", lvtCount, expectedCount);
78    }
79
80    void checkEqual(String attr, int found, int expect) {
81        if (found != expect) {
82            error("Unexpected number of occurrences of " + attr + "\n" +
83                "found: " + found + ", expected: " + expect);
84        }
85    }
86
87    void error(String msg) {
88        System.err.println("Error: " + msg);
89        errors++;
90    }
91
92    int errors = 0;
93}
94
95