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 77d0913..a2e23ba 100644 Binary files a/testData/classes/pkg/TestPrimitives.class and b/testData/classes/pkg/TestPrimitives.class differ 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()); + } }