From 5db9ad29c883f331780e161d6c54d277c217bbee Mon Sep 17 00:00:00 2001 From: Dmitry Cherniachenko Date: Fri, 23 Jun 2017 21:43:23 +0200 Subject: [PATCH] Removed setConstType() from is isBoxingCall() to avoid hidden side effect Const type is now adjusted correctly also when the target is java.lang.Character or java.lang.Integer --- .../modules/decompiler/exps/ConstExprent.java | 8 +- .../decompiler/exps/InvocationExprent.java | 4 +- .../java/decompiler/struct/gen/VarType.java | 2 + testData/classes/pkg/TestPrimitives.class | Bin 3298 -> 3478 bytes testData/results/TestPrimitives.dec | 446 +++++++++--------- testData/src/pkg/TestPrimitives.java | 9 +- 6 files changed, 248 insertions(+), 221 deletions(-) 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 612fd37..e77b680 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/ConstExprent.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/ConstExprent.java @@ -369,16 +369,16 @@ public class ConstExprent extends Exprent { public void adjustConstType(VarType expectedType) { // BYTECHAR and SHORTCHAR => CHAR in the CHAR context - if (expectedType.equals(VarType.VARTYPE_CHAR) && + if ((expectedType.equals(VarType.VARTYPE_CHAR) || expectedType.equals(VarType.VARTYPE_CHARACTER)) && (constType.equals(VarType.VARTYPE_BYTECHAR) || constType.equals(VarType.VARTYPE_SHORTCHAR))) { int intValue = getIntValue(); if (isPrintableAscii(intValue) || CHAR_ESCAPES.containsKey(intValue)) { setConstType(VarType.VARTYPE_CHAR); } } - // CHAR => INT in the INT context - else if (expectedType.equals(VarType.VARTYPE_INT) && - constType.equals(VarType.VARTYPE_CHAR)) { + // BYTE, BYTECHAR, SHORTCHAR, SHORT, CHAR => INT in the INT context + else if ((expectedType.equals(VarType.VARTYPE_INT) || expectedType.equals(VarType.VARTYPE_INTEGER)) && + constType.typeFamily == CodeConstants.TYPE_FAMILY_INTEGER) { setConstType(VarType.VARTYPE_INT); } } diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/InvocationExprent.java b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/InvocationExprent.java index 19f913e..e5ff698 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/InvocationExprent.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/InvocationExprent.java @@ -410,10 +410,10 @@ public class InvocationExprent extends Exprent { // special handling for ambiguous types if (lstParameters.get(0).type == Exprent.EXPRENT_CONST) { // 'Integer.valueOf(1)' has '1' type detected as TYPE_BYTECHAR + // 'Integer.valueOf(40_000)' has '40_000' type detected as TYPE_CHAR + // so we check the type family instead if (lstParameters.get(0).getExprType().typeFamily == CodeConstants.TYPE_FAMILY_INTEGER) { if (classname.equals("java/lang/Integer")) { - // 'Integer.valueOf(40_000)' will change to '40_000' and that will be interpreted as 'char' type - ((ConstExprent) lstParameters.get(0)).setConstType(VarType.VARTYPE_INT); return true; } } diff --git a/src/org/jetbrains/java/decompiler/struct/gen/VarType.java b/src/org/jetbrains/java/decompiler/struct/gen/VarType.java index 1eb177f..410cf09 100644 --- a/src/org/jetbrains/java/decompiler/struct/gen/VarType.java +++ b/src/org/jetbrains/java/decompiler/struct/gen/VarType.java @@ -38,6 +38,8 @@ public class VarType { // TODO: optimize switch public static final VarType VARTYPE_STRING = new VarType(CodeConstants.TYPE_OBJECT, 0, "java/lang/String"); public static final VarType VARTYPE_CLASS = new VarType(CodeConstants.TYPE_OBJECT, 0, "java/lang/Class"); public static final VarType VARTYPE_OBJECT = new VarType(CodeConstants.TYPE_OBJECT, 0, "java/lang/Object"); + public static final VarType VARTYPE_INTEGER = new VarType(CodeConstants.TYPE_OBJECT, 0, "java/lang/Integer"); + public static final VarType VARTYPE_CHARACTER = new VarType(CodeConstants.TYPE_OBJECT, 0, "java/lang/Character"); public static final VarType VARTYPE_VOID = new VarType(CodeConstants.TYPE_VOID); public final int type; diff --git a/testData/classes/pkg/TestPrimitives.class b/testData/class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ziW3k%x6zmR>8YVsnL3-7`jnNMwELM73z?yP^JXxq6*C^8J!12S7AAYu$-bVG>q`WY zJXXdP(-$*?6m~E|17-hS2G=u#mAOL6Y05dIU0yP#di0o67!!HDC8^B!M%xc7D(~Rh zRjNO0F89=gYmYJ8oJm3(JV4fI9T0b#2DUXO9OP|P*dpfCE-1O}=Y4{=SD7cZ!dy`@nx+PQ(6z1j`D+R9cubvstFXvSr|u7xJ93ZLuP zZ)K~otw`O5_9KN5*7F&a$}ook)0=<|*vOuShXUij{dqT1ks?B=_?aTYsaj1dA|g8# zsRW`$#8P#YMHuW<6;Yjv4;4{Uw;&$<74gAvWCpeA!3dkW^k79T{*Xenu3ol!JTrqt zdN7ti!weRsXR!FtPgqj7RF}r7B9?jfl$Ot61re0)n?YlGFiNROP-s-d%93;IQ6*ih zfKDlfI#gmY3^cOWOm|yQO(z%dbrr{T)G9%^5d?vChM^D<>}L={Y!``r-5-(rlAF47 zSwl`sU?CbLa-nDDBMHD}JVK}@>et=is{;6nc|LjY#e^>@3*RE)tc&-CH{paMs&tArtUP|shF0Q9SREDF8U{Ytxt&>azT_e zAzWl(E0vKNnS#4mJbG9{b}PvGU^j;YWE*D-r11-qgOS@{=j2O3HXlkt-G1P-Ih)s};FPku60YQslEGxfcHg zo#i6dVVNCUQA2q@t7<<>-T<1IQ!V@;YGY1y^P6aZ8FGYQM58=zmmfpZJi2oj!X+G* xVQBJP^}|4?X(6(*3g_`GOJxYpk>7%)l^xnYh-n=f1*80bRfovn=V=py^*?B -1;// 78 - res = c > 0;// 79 - res = c > 1;// 80 - res = c > '\b';// 81 - res = c > '\t';// 82 - res = c > '\n';// 83 - res = c > '\f';// 84 - res = c > '\r';// 85 - res = c > ' ';// 86 - res = c > 'a';// 87 - res = c > 'Z';// 88 - res = c > 127;// 89 - res = c > 255;// 90 - return res;// 91 + boolean res = c > -1;// 85 + res = c > 0;// 86 + res = c > 1;// 87 + res = c > '\b';// 88 + res = c > '\t';// 89 + res = c > '\n';// 90 + res = c > '\f';// 91 + res = c > '\r';// 92 + res = c > ' ';// 93 + res = c > 'a';// 94 + res = c > 'Z';// 95 + res = c > 127;// 96 + res = c > 255;// 97 + return res;// 98 } } @@ -107,182 +112,192 @@ class 'pkg/TestPrimitives' { 29 10 2d 11 2f 11 - 32 12 - 3b 12 - 3c 12 - 42 12 - 43 12 - 49 12 - 4c 12 - 52 12 - 54 12 - 5a 12 - 5c 12 - 62 12 - 65 12 - 69 12 - 6d 13 - 70 13 - 79 13 - 7c 13 - 83 13 - 86 13 - 8d 13 - 90 13 - 97 13 - 9a 13 - 9e 13 - a1 13 - a4 14 + 33 12 + 38 12 + 3b 13 + 44 13 + 45 13 + 4b 13 + 4c 13 + 52 13 + 55 13 + 5b 13 + 5d 13 + 63 13 + 65 13 + 6b 13 + 6e 13 + 72 13 + 76 14 + 79 14 + 82 14 + 85 14 + 8c 14 + 8f 14 + 96 14 + 99 14 + a0 14 + a3 14 + a7 14 + aa 14 + ad 15 } method 'printBoolean (Z)V' { - 0 17 - 3 17 - c 17 - 10 17 - 13 17 - 16 18 + 0 18 + 3 18 + c 18 + 10 18 + 13 18 + 16 19 } method 'printByte (B)V' { - 0 21 - 3 21 - c 21 - 10 21 - 13 21 - 16 22 + 0 22 + 3 22 + c 22 + 10 22 + 13 22 + 16 23 } method 'printShort (S)V' { - 0 25 - 3 25 - c 25 - 10 25 - 13 25 - 16 26 + 0 26 + 3 26 + c 26 + 10 26 + 13 26 + 16 27 } method 'printInt (I)V' { - 0 29 - 3 29 - c 29 - 10 29 - 13 29 - 16 30 + 0 30 + 3 30 + c 30 + 10 30 + 13 30 + 16 31 } method 'printLong (J)V' { - 0 33 - 3 33 - c 33 - 10 33 - 13 33 - 16 34 + 0 34 + 3 34 + c 34 + 10 34 + 13 34 + 16 35 } method 'printFloat (F)V' { - 0 37 - 3 37 - c 37 - 10 37 - 13 37 - 16 38 + 0 38 + 3 38 + c 38 + 10 38 + 13 38 + 16 39 } method 'printDouble (D)V' { - 0 41 - 3 41 - c 41 - 10 41 - 13 41 - 16 42 + 0 42 + 3 42 + c 42 + 10 42 + 13 42 + 16 43 } method 'printChar (C)V' { - 0 45 - 3 45 - c 45 - 10 45 - 13 45 - 16 46 + 0 46 + 3 46 + c 46 + 10 46 + 13 46 + 16 47 + } + + method 'printIntBoxed (Ljava/lang/Integer;)V' { + 0 50 + 3 50 + d 50 + 10 50 + 13 51 } method 'getBoolean ()Z' { - 0 49 - 1 49 + 0 54 + 1 54 } method 'getByte ()B' { - 0 53 - 2 53 + 0 58 + 2 58 } method 'getShort ()S' { - 0 57 - 3 57 + 0 62 + 3 62 } method 'getInt ()I' { - 0 61 - 2 61 + 0 66 + 2 66 } method 'printNarrowed ()V' { - 2 65 - 5 65 - 6 65 - b 66 - e 66 - f 66 - 12 67 + 2 70 + 5 70 + 6 70 + b 71 + e 71 + f 71 + 12 72 } method 'constructor ()V' { - 4 70 - 9 71 + 4 75 + 9 76 } method 'compare (C)Z' { - 1 74 - 2 74 - a 74 - c 75 - 14 75 - 16 76 - 17 76 - 1f 76 - 21 77 - 23 77 - 2b 77 - 2d 78 - 2f 78 - 37 78 - 39 79 - 3b 79 - 43 79 - 45 80 - 47 80 - 4f 80 - 51 81 - 53 81 - 5b 81 - 5d 82 - 5f 82 - 67 82 - 69 83 - 6b 83 - 73 83 - 75 84 - 77 84 - 7f 84 - 81 85 - 83 85 - 8b 85 - 8d 86 - 90 86 - 98 86 - 9a 87 + 1 79 + 2 79 + a 79 + c 80 + 14 80 + 16 81 + 17 81 + 1f 81 + 21 82 + 23 82 + 2b 82 + 2d 83 + 2f 83 + 37 83 + 39 84 + 3b 84 + 43 84 + 45 85 + 47 85 + 4f 85 + 51 86 + 53 86 + 5b 86 + 5d 87 + 5f 87 + 67 87 + 69 88 + 6b 88 + 73 88 + 75 89 + 77 89 + 7f 89 + 81 90 + 83 90 + 8b 90 + 8d 91 + 90 91 + 98 91 + 9a 92 } } @@ -296,44 +311,47 @@ Lines mapping: 12 <-> 11 13 <-> 12 15 <-> 13 -16 <-> 14 -17 <-> 15 -20 <-> 18 -21 <-> 19 -24 <-> 22 -25 <-> 23 -28 <-> 26 -29 <-> 27 -32 <-> 30 -33 <-> 31 -36 <-> 34 -37 <-> 35 -40 <-> 38 -41 <-> 39 -44 <-> 42 -45 <-> 43 -48 <-> 46 -49 <-> 47 -53 <-> 50 -57 <-> 54 -61 <-> 58 -65 <-> 62 -69 <-> 66 -70 <-> 67 -71 <-> 68 -74 <-> 71 -75 <-> 72 -78 <-> 75 -79 <-> 76 -80 <-> 77 -81 <-> 78 -82 <-> 79 -83 <-> 80 -84 <-> 81 -85 <-> 82 -86 <-> 83 -87 <-> 84 -88 <-> 85 -89 <-> 86 -90 <-> 87 -91 <-> 88 +17 <-> 14 +18 <-> 15 +19 <-> 16 +22 <-> 19 +23 <-> 20 +26 <-> 23 +27 <-> 24 +30 <-> 27 +31 <-> 28 +34 <-> 31 +35 <-> 32 +38 <-> 35 +39 <-> 36 +42 <-> 39 +43 <-> 40 +46 <-> 43 +47 <-> 44 +50 <-> 47 +51 <-> 48 +55 <-> 51 +56 <-> 52 +60 <-> 55 +64 <-> 59 +68 <-> 63 +72 <-> 67 +76 <-> 71 +77 <-> 72 +78 <-> 73 +81 <-> 76 +82 <-> 77 +85 <-> 80 +86 <-> 81 +87 <-> 82 +88 <-> 83 +89 <-> 84 +90 <-> 85 +91 <-> 86 +92 <-> 87 +93 <-> 88 +94 <-> 89 +95 <-> 90 +96 <-> 91 +97 <-> 92 +98 <-> 93 diff --git a/testData/src/pkg/TestPrimitives.java b/testData/src/pkg/TestPrimitives.java index 484eb84..9448e4d 100644 --- a/testData/src/pkg/TestPrimitives.java +++ b/testData/src/pkg/TestPrimitives.java @@ -12,7 +12,9 @@ public class TestPrimitives { printDouble(1.23); printChar('Z'); - String.format("%b, %d, %d, %d, %c", true, 1, 213, 40_000, 'c', 42L); + printIntBoxed(40_000); + + String.format("%b, %d, %d, %d, %c, %d", true, 1, 213, 40_000, 'c', 42L); System.out.println(String.format("%b, %d, %d, %d", getBoolean(), getByte(), getShort(), getInt())); } @@ -49,6 +51,11 @@ public class TestPrimitives { } + public void printIntBoxed(Integer i) { + System.out.println(String.format("%d", i)); + } + + public boolean getBoolean() { return false; }