IDEA-167346 Do not escape single quote in strings

master
Egor.Ushakov 7 years ago
parent 45384fb8c5
commit f53a873116
  1. 34
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/ConstExprent.java
  2. 6
      test/org/jetbrains/java/decompiler/SingleClassesTest.java
  3. BIN
      testData/classes/pkg/TestStringLiterals.class
  4. 38
      testData/results/TestStringLiterals.dec
  5. 25
      testData/src/pkg/TestStringLiterals.java

@ -1,5 +1,5 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -33,17 +33,17 @@ import java.util.*;
import java.util.Map.Entry;
public class ConstExprent extends Exprent {
private static final Map<Integer, String> ESCAPES;
private static final Map<Integer, String> CHAR_ESCAPES;
static {
ESCAPES = new HashMap<>();
ESCAPES.put(new Integer(0x8), "\\b"); /* \u0008: backspace BS */
ESCAPES.put(new Integer(0x9), "\\t"); /* \u0009: horizontal tab HT */
ESCAPES.put(new Integer(0xA), "\\n"); /* \u000a: linefeed LF */
ESCAPES.put(new Integer(0xC), "\\f"); /* \u000c: form feed FF */
ESCAPES.put(new Integer(0xD), "\\r"); /* \u000d: carriage return CR */
ESCAPES.put(new Integer(0x22), "\\\""); /* \u0022: double quote " */
ESCAPES.put(new Integer(0x27), "\\\'"); /* \u0027: single quote ' */
ESCAPES.put(new Integer(0x5C), "\\\\"); /* \u005c: backslash \ */
CHAR_ESCAPES = new HashMap<>();
CHAR_ESCAPES.put(new Integer(0x8), "\\b"); /* \u0008: backspace BS */
CHAR_ESCAPES.put(new Integer(0x9), "\\t"); /* \u0009: horizontal tab HT */
CHAR_ESCAPES.put(new Integer(0xA), "\\n"); /* \u000a: linefeed LF */
CHAR_ESCAPES.put(new Integer(0xC), "\\f"); /* \u000c: form feed FF */
CHAR_ESCAPES.put(new Integer(0xD), "\\r"); /* \u000d: carriage return CR */
//CHAR_ESCAPES.put(new Integer(0x22), "\\\""); /* \u0022: double quote " */
CHAR_ESCAPES.put(new Integer(0x27), "\\\'"); /* \u0027: single quote ' */
CHAR_ESCAPES.put(new Integer(0x5C), "\\\\"); /* \u005c: backslash \ */
}
private VarType constType;
@ -130,7 +130,7 @@ public class ConstExprent extends Exprent {
case CodeConstants.TYPE_CHAR:
Integer val = (Integer)value;
String ret = ESCAPES.get(val);
String ret = CHAR_ESCAPES.get(val);
if (ret == null) {
char c = (char)val.intValue();
if (c >= 32 && c < 127 || !ascii && TextUtil.isPrintableUnicode(c)) {
@ -140,7 +140,7 @@ public class ConstExprent extends Exprent {
ret = TextUtil.charToUnicodeLiteral(c);
}
}
return new TextBuffer().append('\'').append(ret).append('\'');
return new TextBuffer(ret).enclose("'", "'");
case CodeConstants.TYPE_BYTE:
case CodeConstants.TYPE_BYTECHAR:
@ -235,7 +235,7 @@ public class ConstExprent extends Exprent {
case CodeConstants.TYPE_OBJECT:
if (constType.equals(VarType.VARTYPE_STRING)) {
return new TextBuffer().append('"').append(convertStringToJava(value.toString(), ascii)).append('"');
return new TextBuffer(convertStringToJava(value.toString(), ascii)).enclose("\"", "\"");
}
else if (constType.equals(VarType.VARTYPE_CLASS)) {
String stringVal = value.toString();
@ -274,9 +274,9 @@ public class ConstExprent extends Exprent {
case 0x22: //"\\\\\""); // u0022: double quote "
buffer.append("\\\"");
break;
case 0x27: //"\\\\'"); // u0027: single quote '
buffer.append("\\\'");
break;
//case 0x27: //"\\\\'"); // u0027: single quote '
// buffer.append("\\\'");
// break;
default:
if (c >= 32 && c < 127 || !ascii && TextUtil.isPrintableUnicode(c)) {
buffer.append(c);

@ -22,9 +22,10 @@ import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static org.jetbrains.java.decompiler.DecompilerTestFixture.assertFilesEqual;
import static org.junit.Assert.assertTrue;
@ -98,6 +99,7 @@ public class SingleClassesTest {
@Test public void testAnonymousParamNames() { doTest("pkg/TestAnonymousParamNames"); }
@Test public void testAnonymousParams() { doTest("pkg/TestAnonymousParams"); }
@Test public void testAccessReplace() { doTest("pkg/TestAccessReplace"); }
@Test public void testStringLiterals() { doTest("pkg/TestStringLiterals"); }
private void doTest(String testFile, String... companionFiles) {
ConsoleDecompiler decompiler = fixture.getDecompiler();

@ -0,0 +1,38 @@
package pkg;
public class TestStringLiterals {
public static void main(String[] var0) {
String var1 = "abc\b\t\n\f\r\"'\\def";// 20
char[] var2 = new char[]{'\b', '\t', '\n', '\f', '\r', '"', '\'', '\\'};// 21
System.out.println(var1);// 22
System.out.println(var2);// 23
}// 24
}
class 'pkg/TestStringLiterals' {
method 'main ([Ljava/lang/String;)V' {
0 4
2 4
9 5
e 5
13 5
18 5
1d 5
22 5
28 5
2e 5
31 5
32 6
36 6
39 7
3d 7
40 8
}
}
Lines mapping:
20 <-> 5
21 <-> 6
22 <-> 7
23 <-> 8
24 <-> 9

@ -0,0 +1,25 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pkg;
public class TestStringLiterals {
public static void main(String[] args) {
String a = "abc\b\t\n\f\r\"'\\def";
char chars[] = {'\b', '\t', '\n', '\f', '\r', '"', '\'', '\\'};
System.out.println(a);
System.out.println(chars);
}
}
Loading…
Cancel
Save