From f53a873116342516ddd358a0eee8feaa9fd6dcdf Mon Sep 17 00:00:00 2001 From: "Egor.Ushakov" Date: Tue, 7 Feb 2017 17:51:02 +0300 Subject: [PATCH] IDEA-167346 Do not escape single quote in strings --- .../modules/decompiler/exps/ConstExprent.java | 34 ++++++++-------- .../java/decompiler/SingleClassesTest.java | 6 ++- testData/classes/pkg/TestStringLiterals.class | Bin 0 -> 534 bytes testData/results/TestStringLiterals.dec | 38 ++++++++++++++++++ testData/src/pkg/TestStringLiterals.java | 25 ++++++++++++ 5 files changed, 84 insertions(+), 19 deletions(-) create mode 100644 testData/classes/pkg/TestStringLiterals.class create mode 100644 testData/results/TestStringLiterals.dec create mode 100644 testData/src/pkg/TestStringLiterals.java diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/ConstExprent.java b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/ConstExprent.java index e82fec7..962bb77 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/ConstExprent.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/ConstExprent.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 ESCAPES; + private static final Map 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); diff --git a/test/org/jetbrains/java/decompiler/SingleClassesTest.java b/test/org/jetbrains/java/decompiler/SingleClassesTest.java index 8b876c8..2388d8d 100644 --- a/test/org/jetbrains/java/decompiler/SingleClassesTest.java +++ b/test/org/jetbrains/java/decompiler/SingleClassesTest.java @@ -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(); diff --git a/testData/classes/pkg/TestStringLiterals.class b/testData/classes/pkg/TestStringLiterals.class new file mode 100644 index 0000000000000000000000000000000000000000..f4cb2b9568d3e936cfb164a575f43dfb2f01d16c GIT binary patch literal 534 zcmZut$xZ@65Pc1^F%HrqiyJOnz$KCxF9u^YCZ2Ffa7ZR_8K5PtFc=t2{470SVvHZ) zM;WW(V2JkNRrTvvRj>N%`~4HZAvSH8;1(8QNMg}O3TXuy6Il~W492kxWO%|5EpFEs zwDW#jFeEB65LcsKQw(nXW|tVP=gWX0Q><6+{U^WF^@C2S8V+R8DXT)e+8+&C;zBBM z_ErqT>0?EPV&Hd&`>M);g&4!U-)vbi+ll83jkdTmvFu<4t0vYQ*w1wY{8&!RVMN{^>FO)FM5gHRRNiqj<%9T_?LzLoKGE(8zhweqW z<$4;Axt`8#*E6`|dM1y%p2hR7#|>T}G 5 +21 <-> 6 +22 <-> 7 +23 <-> 8 +24 <-> 9 diff --git a/testData/src/pkg/TestStringLiterals.java b/testData/src/pkg/TestStringLiterals.java new file mode 100644 index 0000000..ebefa32 --- /dev/null +++ b/testData/src/pkg/TestStringLiterals.java @@ -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); + } +}