1/*
2 * Copyright (c) 2010, 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 7004698
27 * @summary javap does not output CharacterRangeTable attributes correctly
28 * @modules jdk.jdeps/com.sun.tools.javap
29 */
30
31import java.io.*;
32import java.util.*;
33import java.util.regex.*;
34
35public class T7004698 {
36    public static void main(String... args) throws Exception {
37        new T7004698().run();
38    }
39
40    void run() throws Exception {
41        File srcDir = new File(System.getProperty("test.src"));
42        File srcFile = new File(srcDir, T7004698.class.getSimpleName() + ".java");
43        File classesDir = new File(".");
44        compile("-Xjcov",
45                "--add-modules", "jdk.jdeps",
46                "--add-exports", "jdk.jdeps/com.sun.tools.javap=ALL-UNNAMED",
47                "-d", classesDir.getPath(),
48                srcFile.getPath());
49
50        File classFile = new File(classesDir, T7004698.class.getSimpleName() + ".class");
51        String out = javap("-v", classFile.getPath());
52
53        Pattern attrBody = Pattern.compile("[0-9a-f, ]+//[-0-9a-z:, ]+");
54        Pattern endOfAttr = Pattern.compile("(^$|[A-Z][A-Za-z0-9_]+:.*|})");
55        boolean inAttr = false;
56        int count = 0;
57        int errors = 0;
58        for (String line: out.split(System.getProperty("line.separator"))) {
59            line = line.trim();
60            if (line.equals("CharacterRangeTable:")) {
61                inAttr = true;
62                count++;
63            } else if (inAttr) {
64                if (endOfAttr.matcher(line).matches()) {
65                    inAttr = false;
66                } else if (!attrBody.matcher(line).matches()) {
67                    System.err.println("unexpected line found: " + line);
68                    errors++;
69                }
70            }
71        }
72        if (count == 0)
73            throw new Exception("no attribute instances found");
74
75        if (errors > 0)
76            throw new Exception(errors + " errors found");
77    }
78
79    void compile(String... args) throws Exception {
80        StringWriter sw = new StringWriter();
81        PrintWriter pw = new PrintWriter(sw);
82        int rc = com.sun.tools.javac.Main.compile(args, pw);
83        pw.close();
84        String out = sw.toString();
85        if (!out.isEmpty())
86            System.err.println(out);
87        if (rc != 0)
88            throw new Exception("javac failed unexpectedly; rc=" + rc);
89    }
90
91    String javap(String... args) throws Exception {
92        StringWriter sw = new StringWriter();
93        PrintWriter pw = new PrintWriter(sw);
94        int rc = com.sun.tools.javap.Main.run(args, pw);
95        pw.close();
96        String out = sw.toString();
97        if (!out.isEmpty())
98            System.err.println(out);
99        if (rc != 0)
100            throw new Exception("javap failed unexpectedly; rc=" + rc);
101        return out;
102    }
103}
104