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 0000000..f4cb2b9 Binary files /dev/null and b/testData/classes/pkg/TestStringLiterals.class differ diff --git a/testData/results/TestStringLiterals.dec b/testData/results/TestStringLiterals.dec new file mode 100644 index 0000000..c5afe18 --- /dev/null +++ b/testData/results/TestStringLiterals.dec @@ -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 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); + } +}