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.
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.
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).
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.
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
27
* @summary Constant pool's strings are not escaped properly
28
* @modules jdk.jdeps/com.sun.tools.javap
31
import java.io.PrintWriter;
32
import java.io.StringWriter;
33
import java.util.ArrayList;
35
import java.util.regex.Matcher;
36
import java.util.regex.Pattern;
38
public class T8038414 {
39
private static final String NEW_LINE = System.getProperty("line.separator");
40
private static final String TEST_CLASSES = System.getProperty("test.classes", ".");
41
private static final String GOLDEN_STRING = escapeString(Test.test);
43
private static String escapeString(String s) {
44
StringBuilder sb = new StringBuilder();
45
for (int i = 0; i < s.length(); i++) {
49
sb.append('\\').append('t');
52
sb.append('\\').append('n');
55
sb.append('\\').append('r');
58
sb.append('\\').append('b');
61
sb.append('\\').append('f');
64
sb.append('\\').append('\"');
67
sb.append('\\').append('\'');
70
sb.append('\\').append('\\');
79
public static void main(String... args) {
84
String output = javap(Test.class.getName());
85
List<String> actualValues = extractEscapedComments(output);
86
for (String a : actualValues) {
87
check(!GOLDEN_STRING.equals(a), String.format("Expected: %s, got: %s", GOLDEN_STRING, a));
91
private List<String> extractConstantPool(String output) {
92
List<String> cp = new ArrayList<>();
94
for (String s : output.split("\n")) {
101
if (s.equals("Constant pool:")) {
109
* Returns a list which contains comments of the string entry in the constant pool
110
* and the appropriate UTF-8 value.
114
private List<String> extractEscapedComments(String output) {
115
List<String> result = new ArrayList<>();
116
Pattern stringPattern = Pattern.compile(" +#\\d+ = String +#(\\d+) +// +(.*)");
118
List<String> cp = extractConstantPool(output);
119
for (String c : cp) {
120
Matcher matcher = stringPattern.matcher(c);
121
if (matcher.matches()) {
122
index = Integer.parseInt(matcher.group(1)) - 1;
123
result.add(matcher.group(2));
124
// only one String entry
128
check(index == -1, "Escaped string is not found in constant pool");
129
result.add(cp.get(index).replaceAll(".* +", "")); // remove #16 = Utf8
133
private String javap(String className) {
134
StringWriter sw = new StringWriter();
135
PrintWriter out = new PrintWriter(sw);
136
int rc = com.sun.tools.javap.Main.run(new String[]{"-v", "-classpath", TEST_CLASSES, className}, out);
138
String output = sw.toString();
139
System.err.println("class " + className);
140
System.err.println(output);
142
check(rc != 0, "javap failed. rc=" + rc);
143
return output.replaceAll(NEW_LINE, "\n");
146
private void check(boolean cond, String msg) {
148
throw new RuntimeException(msg);
153
static String test = "\\t\t\b\r\n\f\"\'\\";