langtools/test/tools/javap/T8038414.java
author hannesw
Tue, 22 Mar 2016 14:23:16 +0100
changeset 36690 06b714373aa4
parent 30846 2b3f379840f0
child 36526 3b41f1c69604
permissions -rw-r--r--
8151810: for-in iteration does not provide per-iteration scope Reviewed-by: attila, lagergren
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26390
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 26390
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
26390
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
     4
 *
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
     8
 *
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    13
 * accompanied this code).
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    14
 *
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    18
 *
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    21
 * questions.
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    22
 */
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    23
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    24
import java.io.PrintWriter;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    25
import java.io.StringWriter;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    26
import java.util.ArrayList;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    27
import java.util.List;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    28
import java.util.regex.Matcher;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    29
import java.util.regex.Pattern;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    30
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    31
/*
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    32
 * @test
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    33
 * @bug 8038414
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    34
 * @summary Constant pool's strings are not escaped properly
30846
2b3f379840f0 8074432: Move jdeps and javap to jdk.jdeps module
mchung
parents: 30730
diff changeset
    35
 * @modules jdk.jdeps
26390
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    36
 */
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    37
public class T8038414 {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    38
    private static final String NEW_LINE = System.getProperty("line.separator");
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    39
    private static final String TEST_CLASSES = System.getProperty("test.classes", ".");
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    40
    private static final String GOLDEN_STRING = escapeString(Test.test);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    41
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    42
    private static String escapeString(String s) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    43
        StringBuilder sb = new StringBuilder();
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    44
        for (int i = 0; i < s.length(); i++) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    45
            char c = s.charAt(i);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    46
            switch (c) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    47
                case '\t':
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    48
                    sb.append('\\').append('t');
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    49
                    break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    50
                case '\n':
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    51
                    sb.append('\\').append('n');
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    52
                    break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    53
                case '\r':
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    54
                    sb.append('\\').append('r');
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    55
                    break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    56
                case '\b':
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    57
                    sb.append('\\').append('b');
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    58
                    break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    59
                case '\f':
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    60
                    sb.append('\\').append('f');
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    61
                    break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    62
                case '\"':
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    63
                    sb.append('\\').append('\"');
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    64
                    break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    65
                case '\'':
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    66
                    sb.append('\\').append('\'');
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    67
                    break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    68
                case '\\':
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    69
                    sb.append('\\').append('\\');
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    70
                    break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    71
                default:
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    72
                    sb.append(c);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    73
            }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    74
        }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    75
        return sb.toString();
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    76
    }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    77
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    78
    public static void main(String... args) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    79
        new T8038414().run();
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    80
    }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    81
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    82
    public void run() {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    83
        String output = javap(Test.class.getName());
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    84
        List<String> actualValues = extractEscapedComments(output);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    85
        for (String a : actualValues) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    86
            check(!GOLDEN_STRING.equals(a), String.format("Expected: %s, got: %s", GOLDEN_STRING, a));
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    87
        }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    88
    }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    89
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    90
    private List<String> extractConstantPool(String output) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    91
        List<String> cp = new ArrayList<>();
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    92
        boolean inCp = false;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    93
        for (String s : output.split("\n")) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    94
            if (s.equals("{")) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    95
                break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    96
            }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    97
            if (inCp) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    98
                cp.add(s);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    99
            }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   100
            if (s.equals("Constant pool:")) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   101
                inCp = true;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   102
            }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   103
        }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   104
        return cp;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   105
    }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   106
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   107
    /**
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   108
     * Returns a list which contains comments of the string entry in the constant pool
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   109
     * and the appropriate UTF-8 value.
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   110
     *
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   111
     * @return a list
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   112
     */
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   113
    private List<String> extractEscapedComments(String output) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   114
        List<String> result = new ArrayList<>();
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   115
        Pattern stringPattern = Pattern.compile(" +#\\d+ = String +#(\\d+) +// +(.*)");
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   116
        int index = -1;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   117
        List<String> cp = extractConstantPool(output);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   118
        for (String c : cp) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   119
            Matcher matcher = stringPattern.matcher(c);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   120
            if (matcher.matches()) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   121
                index = Integer.parseInt(matcher.group(1)) - 1;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   122
                result.add(matcher.group(2));
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   123
                // only one String entry
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   124
                break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   125
            }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   126
        }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   127
        check(index == -1, "Escaped string is not found in constant pool");
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   128
        result.add(cp.get(index).replaceAll(".* +", "")); // remove #16 = Utf8
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   129
        return result;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   130
    }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   131
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   132
    private String javap(String className) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   133
        StringWriter sw = new StringWriter();
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   134
        PrintWriter out = new PrintWriter(sw);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   135
        int rc = com.sun.tools.javap.Main.run(new String[]{"-v", "-classpath", TEST_CLASSES, className}, out);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   136
        out.close();
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   137
        String output = sw.toString();
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   138
        System.err.println("class " + className);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   139
        System.err.println(output);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   140
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   141
        check(rc != 0, "javap failed. rc=" + rc);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   142
        return output.replaceAll(NEW_LINE, "\n");
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   143
    }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   144
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   145
    private void check(boolean cond, String msg) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   146
        if (cond) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   147
            throw new RuntimeException(msg);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   148
        }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   149
    }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   150
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   151
    static class Test {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   152
        static String test = "\\t\t\b\r\n\f\"\'\\";
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   153
    }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   154
}