langtools/test/tools/javap/T8038414.java
author jjg
Thu, 31 Mar 2016 15:20:50 -0700
changeset 36778 e04318f39f92
parent 36526 3b41f1c69604
permissions -rw-r--r--
8152897: refactor ToolBox to allow reduced documented dependencies Reviewed-by: vromero
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
/*
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
     2
 * Copyright (c) 2014, 2016, 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
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    24
/*
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    25
 * @test
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    26
 * @bug 8038414
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    27
 * @summary Constant pool's strings are not escaped properly
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    28
 * @modules jdk.jdeps/com.sun.tools.javap
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    29
 */
3b41f1c69604 8142968: Module System implementation
alanb
parents: 30846
diff changeset
    30
26390
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    31
import java.io.PrintWriter;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    32
import java.io.StringWriter;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    33
import java.util.ArrayList;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    34
import java.util.List;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    35
import java.util.regex.Matcher;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    36
import java.util.regex.Pattern;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    37
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    38
public class T8038414 {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    39
    private static final String NEW_LINE = System.getProperty("line.separator");
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    40
    private static final String TEST_CLASSES = System.getProperty("test.classes", ".");
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    41
    private static final String GOLDEN_STRING = escapeString(Test.test);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    42
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    43
    private static String escapeString(String s) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    44
        StringBuilder sb = new StringBuilder();
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    45
        for (int i = 0; i < s.length(); i++) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    46
            char c = s.charAt(i);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    47
            switch (c) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    48
                case '\t':
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    49
                    sb.append('\\').append('t');
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    50
                    break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    51
                case '\n':
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    52
                    sb.append('\\').append('n');
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    53
                    break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    54
                case '\r':
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    55
                    sb.append('\\').append('r');
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    56
                    break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    57
                case '\b':
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    58
                    sb.append('\\').append('b');
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    59
                    break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    60
                case '\f':
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    61
                    sb.append('\\').append('f');
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    62
                    break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    63
                case '\"':
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    64
                    sb.append('\\').append('\"');
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    65
                    break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    66
                case '\'':
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    67
                    sb.append('\\').append('\'');
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    68
                    break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    69
                case '\\':
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    70
                    sb.append('\\').append('\\');
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    71
                    break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    72
                default:
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    73
                    sb.append(c);
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
        }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    76
        return sb.toString();
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
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    79
    public static void main(String... args) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    80
        new T8038414().run();
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
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    83
    public void run() {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    84
        String output = javap(Test.class.getName());
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    85
        List<String> actualValues = extractEscapedComments(output);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    86
        for (String a : actualValues) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    87
            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
    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
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    91
    private List<String> extractConstantPool(String output) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    92
        List<String> cp = new ArrayList<>();
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    93
        boolean inCp = false;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    94
        for (String s : output.split("\n")) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    95
            if (s.equals("{")) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    96
                break;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    97
            }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    98
            if (inCp) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
    99
                cp.add(s);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   100
            }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   101
            if (s.equals("Constant pool:")) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   102
                inCp = true;
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
        }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   105
        return cp;
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
    /**
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   109
     * 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
   110
     * and the appropriate UTF-8 value.
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   111
     *
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   112
     * @return a list
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   113
     */
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   114
    private List<String> extractEscapedComments(String output) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   115
        List<String> result = new ArrayList<>();
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   116
        Pattern stringPattern = Pattern.compile(" +#\\d+ = String +#(\\d+) +// +(.*)");
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   117
        int index = -1;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   118
        List<String> cp = extractConstantPool(output);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   119
        for (String c : cp) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   120
            Matcher matcher = stringPattern.matcher(c);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   121
            if (matcher.matches()) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   122
                index = Integer.parseInt(matcher.group(1)) - 1;
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   123
                result.add(matcher.group(2));
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   124
                // only one String entry
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   125
                break;
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
        }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   128
        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
   129
        result.add(cp.get(index).replaceAll(".* +", "")); // remove #16 = Utf8
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   130
        return result;
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
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   133
    private String javap(String className) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   134
        StringWriter sw = new StringWriter();
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   135
        PrintWriter out = new PrintWriter(sw);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   136
        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
   137
        out.close();
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   138
        String output = sw.toString();
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   139
        System.err.println("class " + className);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   140
        System.err.println(output);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   141
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   142
        check(rc != 0, "javap failed. rc=" + rc);
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   143
        return output.replaceAll(NEW_LINE, "\n");
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
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   146
    private void check(boolean cond, String msg) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   147
        if (cond) {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   148
            throw new RuntimeException(msg);
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
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   152
    static class Test {
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   153
        static String test = "\\t\t\b\r\n\f\"\'\\";
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   154
    }
1d0902fe3ca0 8038414: Constant pool's strings are not escaped properly
jjg
parents:
diff changeset
   155
}