From 0a7a60fa7baf8560f97681301fc4369fda851f40 Mon Sep 17 00:00:00 2001 From: Dmitry Cherniachenko Date: Wed, 19 Apr 2017 01:16:47 +0200 Subject: [PATCH] Add '(byte)' and '(short)' type cast for int literals only in invocation parameters --- .../modules/decompiler/ExprProcessor.java | 5 ++-- .../decompiler/exps/InvocationExprent.java | 13 +++++++++-- testData/classes/pkg/TestPrimitives.class | Bin 2632 -> 2731 bytes testData/results/MoreAnnotations.dec | 8 +++---- testData/results/TestPrimitives.dec | 22 ++++++++++++++++-- testData/src/pkg/TestPrimitives.java | 5 ++++ 6 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java b/src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java index 1b834a0..930b1ee 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java @@ -860,7 +860,7 @@ public class ExprProcessor implements CodeConstants { int indent, boolean castNull, BytecodeMappingTracer tracer) { - return getCastedExprent(exprent, leftType, buffer, indent, castNull, false, tracer); + return getCastedExprent(exprent, leftType, buffer, indent, castNull, false, false, tracer); } public static boolean getCastedExprent(Exprent exprent, @@ -869,6 +869,7 @@ public class ExprProcessor implements CodeConstants { int indent, boolean castNull, boolean castAlways, + boolean castNarrowing, BytecodeMappingTracer tracer) { VarType rightType = exprent.getExprType(); @@ -876,7 +877,7 @@ public class ExprProcessor implements CodeConstants { castAlways || (!leftType.isSuperset(rightType) && (rightType.equals(VarType.VARTYPE_OBJECT) || leftType.type != CodeConstants.TYPE_OBJECT)) || (castNull && rightType.type == CodeConstants.TYPE_NULL && !UNDEFINED_TYPE_STRING.equals(getTypeName(leftType))) || - (isIntConstant(exprent) && VarType.VARTYPE_INT.isStrictSuperset(leftType)); + (castNarrowing && isIntConstant(exprent) && VarType.VARTYPE_INT.isStrictSuperset(leftType)); boolean quote = cast && exprent.getPrecedence() >= FunctionExprent.getPrecedence(FunctionExprent.FUNCTION_CAST); 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 ff70462..dd8e26e 100644 --- a/src/org/jetbrains/java/decompiler/modules/decompiler/exps/InvocationExprent.java +++ b/src/org/jetbrains/java/decompiler/modules/decompiler/exps/InvocationExprent.java @@ -217,7 +217,9 @@ public class InvocationExprent extends Exprent { if (isStatic) { if (isBoxingCall()) { - ExprProcessor.getCastedExprent(lstParameters.get(0), descriptor.params[0], buf, indent, false, false, tracer); + // process general "boxing" calls, e.g. 'Object[] data = { true }' or 'Byte b = 123' + // here 'byte' and 'short' values do not need an explicit narrowing type cast + ExprProcessor.getCastedExprent(lstParameters.get(0), descriptor.params[0], buf, indent, false, false, false, tracer); return buf; } @@ -351,7 +353,14 @@ public class InvocationExprent extends Exprent { TextBuffer buff = new TextBuffer(); boolean ambiguous = setAmbiguousParameters.get(i); - ExprProcessor.getCastedExprent(lstParameters.get(i), descriptor.params[i], buff, indent, true, ambiguous, tracer); + + Exprent param = lstParameters.get(i); + // "unbox" invocation parameters, e.g. 'byteSet.add((byte)123)' or 'new ShortContainer((short)813)' + if (param.type == Exprent.EXPRENT_INVOCATION && ((InvocationExprent)param).isBoxingCall()) { + param = ((InvocationExprent)param).lstParameters.get(0); + } + // 'byte' and 'short' literals need an explicit narrowing type cast when used as a parameter + ExprProcessor.getCastedExprent(param, descriptor.params[i], buff, indent, true, ambiguous, true, tracer); buf.append(buff); firstParameter = false; diff --git a/testData/classes/pkg/TestPrimitives.class b/testData/classes/pkg/TestPrimitives.class index 77d091354d71f969eb2dc6aa4bea17012bd698fb..a2e23ba7c71e119a3f73fa320e5092fb1e1cba65 100644 GIT binary patch delta 443 zcmXAlOHWfl7(maxEuGuR6=SK1KR_Oq1u?j1Be1Jjl{X^LQcAVG+?I!}T3^W12V8uh zVaLv-2_fmi1#9AOaKmqKPnb_#OfpDUD;Q@&qZ&ZL8zb}I79 z%}$( uEr~>fYdZ}>|0HM-J~GEP9fr+>3~v)P<1zZUVaomgj0BygEFKuz3|;`o!c%Ji delta 345 zcmWlVxlTf15Ju0v0{`{Tjfn=|Kr0Ih?I|ei6!(2&6>&j8QBZNieZOv)*x8wo7&;4U z<1^U!4E9E5iZkDtnQC5}KfT4TzxNNIgC|WRgPInG1jB+6!Kk~omnw*jx%|(m7fItD z|8K>c|LLrVO$a9InPS>vMiCMZYa&D?#WZndB_%X-Bqhyj7JMvfmPl!qNn2zrvYHiE zEpnPQ)-`zw78{yPwiLy;O382nX3(>3v%{`aHrtAOHv5!py69FMh#cAk=}{bs9NYBL zr#KNgRh-$JbD_Ajx#HTuYz+w_iW|9bL}H3tkvqk`^amM+6^|ligQiq6?(2_cvcYts T8mMbGjfQH@{q3r~%4Fal?bSsx diff --git a/testData/results/MoreAnnotations.dec b/testData/results/MoreAnnotations.dec index d7287c1..37302cc 100644 --- a/testData/results/MoreAnnotations.dec +++ b/testData/results/MoreAnnotations.dec @@ -33,11 +33,11 @@ public @interface MoreAnnotations { String annotatedWithEmptyArrays = ""; @MoreAnnotations( intArray = {1, 0, 2147483647, -2147483648}, - byteArray = {(byte)1, (byte)0, (byte)127, (byte)-128, (byte)-1}, + byteArray = {1, 0, 127, -128, -1}, floatArray = {1.0F, 0.0F, 3.4028235E38F, 1.4E-45F, 0.0F / 0.0, 1.0F / 0.0, -1.0F / 0.0}, doubleArray = {1.0D, 0.0D, 1.7976931348623157E308D, 4.9E-324D, 0.0D / 0.0, 1.0D / 0.0, -1.0D / 0.0}, booleanArray = {true, false}, - shortArray = {(short)1, (short)0, (short)32767, (short)-32768, (short)-1}, + shortArray = {1, 0, 32767, -32768, -1}, longArray = {1L, 0L, 9223372036854775807L, -9223372036854775808L}, charArray = {'a', '\n', '\u0001', '\u0000', '\uffff', '\u0000'}, enumArray = {MoreAnnotations.TestEnum.FirstValue, MoreAnnotations.TestEnum.SecondValue}, @@ -73,7 +73,7 @@ public @interface MoreAnnotations { int[] intArray() default {1, 0, 2147483647, -2147483648}; - byte[] byteArray() default {(byte)1, (byte)0, (byte)127, (byte)-128, (byte)-1}; + byte[] byteArray() default {1, 0, 127, -128, -1}; float[] floatArray() default {1.0F, 0.0F, 3.4028235E38F, 1.4E-45F, 0.0F / 0.0, 1.0F / 0.0, -1.0F / 0.0}; @@ -81,7 +81,7 @@ public @interface MoreAnnotations { boolean[] booleanArray() default {true, false}; - short[] shortArray() default {(short)1, (short)0, (short)32767, (short)-32768, (short)-1}; + short[] shortArray() default {1, 0, 32767, -32768, -1}; long[] longArray() default {1L, 0L, 9223372036854775807L, -9223372036854775808L}; diff --git a/testData/results/TestPrimitives.dec b/testData/results/TestPrimitives.dec index d2be8be..63f2f64 100644 --- a/testData/results/TestPrimitives.dec +++ b/testData/results/TestPrimitives.dec @@ -50,16 +50,21 @@ public class TestPrimitives { } public byte getByte() { - return (byte)-128;// 56 + return -128;// 56 } public short getShort() { - return (short)-32768;// 60 + return -32768;// 60 } public int getInt() { return 42;// 64 } + + public void printNarrowed() { + this.printByte((byte)this.getInt());// 68 + this.printShort((short)this.getInt());// 69 + }// 70 } class 'pkg/TestPrimitives' { @@ -186,6 +191,16 @@ class 'pkg/TestPrimitives' { 0 60 2 60 } + + method 'printNarrowed ()V' { + 2 64 + 5 64 + 6 64 + b 65 + e 65 + f 65 + 12 66 + } } Lines mapping: @@ -219,3 +234,6 @@ Lines mapping: 56 <-> 53 60 <-> 57 64 <-> 61 +68 <-> 65 +69 <-> 66 +70 <-> 67 diff --git a/testData/src/pkg/TestPrimitives.java b/testData/src/pkg/TestPrimitives.java index f82f585..1ecb5e7 100644 --- a/testData/src/pkg/TestPrimitives.java +++ b/testData/src/pkg/TestPrimitives.java @@ -63,4 +63,9 @@ public class TestPrimitives { public int getInt() { return 42; } + + public void printNarrowed() { + printByte((byte)getInt()); + printShort((short)getInt()); + } }