Add '(byte)' and '(short)' type cast for int literals only in invocation parameters

master
Dmitry Cherniachenko 8 years ago committed by Egor.Ushakov
parent 7e1cb88fe2
commit 0a7a60fa7b
  1. 5
      src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java
  2. 13
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/InvocationExprent.java
  3. BIN
      testData/classes/pkg/TestPrimitives.class
  4. 8
      testData/results/MoreAnnotations.dec
  5. 22
      testData/results/TestPrimitives.dec
  6. 5
      testData/src/pkg/TestPrimitives.java

@ -860,7 +860,7 @@ public class ExprProcessor implements CodeConstants {
int indent, int indent,
boolean castNull, boolean castNull,
BytecodeMappingTracer tracer) { 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, public static boolean getCastedExprent(Exprent exprent,
@ -869,6 +869,7 @@ public class ExprProcessor implements CodeConstants {
int indent, int indent,
boolean castNull, boolean castNull,
boolean castAlways, boolean castAlways,
boolean castNarrowing,
BytecodeMappingTracer tracer) { BytecodeMappingTracer tracer) {
VarType rightType = exprent.getExprType(); VarType rightType = exprent.getExprType();
@ -876,7 +877,7 @@ public class ExprProcessor implements CodeConstants {
castAlways || castAlways ||
(!leftType.isSuperset(rightType) && (rightType.equals(VarType.VARTYPE_OBJECT) || leftType.type != CodeConstants.TYPE_OBJECT)) || (!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))) || (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); boolean quote = cast && exprent.getPrecedence() >= FunctionExprent.getPrecedence(FunctionExprent.FUNCTION_CAST);

@ -217,7 +217,9 @@ public class InvocationExprent extends Exprent {
if (isStatic) { if (isStatic) {
if (isBoxingCall()) { 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; return buf;
} }
@ -351,7 +353,14 @@ public class InvocationExprent extends Exprent {
TextBuffer buff = new TextBuffer(); TextBuffer buff = new TextBuffer();
boolean ambiguous = setAmbiguousParameters.get(i); 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); buf.append(buff);
firstParameter = false; firstParameter = false;

@ -33,11 +33,11 @@ public @interface MoreAnnotations {
String annotatedWithEmptyArrays = ""; String annotatedWithEmptyArrays = "";
@MoreAnnotations( @MoreAnnotations(
intArray = {1, 0, 2147483647, -2147483648}, 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}, 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}, 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}, 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}, longArray = {1L, 0L, 9223372036854775807L, -9223372036854775808L},
charArray = {'a', '\n', '\u0001', '\u0000', '\uffff', '\u0000'}, charArray = {'a', '\n', '\u0001', '\u0000', '\uffff', '\u0000'},
enumArray = {MoreAnnotations.TestEnum.FirstValue, MoreAnnotations.TestEnum.SecondValue}, enumArray = {MoreAnnotations.TestEnum.FirstValue, MoreAnnotations.TestEnum.SecondValue},
@ -73,7 +73,7 @@ public @interface MoreAnnotations {
int[] intArray() default {1, 0, 2147483647, -2147483648}; 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}; 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}; 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}; long[] longArray() default {1L, 0L, 9223372036854775807L, -9223372036854775808L};

@ -50,16 +50,21 @@ public class TestPrimitives {
} }
public byte getByte() { public byte getByte() {
return (byte)-128;// 56 return -128;// 56
} }
public short getShort() { public short getShort() {
return (short)-32768;// 60 return -32768;// 60
} }
public int getInt() { public int getInt() {
return 42;// 64 return 42;// 64
} }
public void printNarrowed() {
this.printByte((byte)this.getInt());// 68
this.printShort((short)this.getInt());// 69
}// 70
} }
class 'pkg/TestPrimitives' { class 'pkg/TestPrimitives' {
@ -186,6 +191,16 @@ class 'pkg/TestPrimitives' {
0 60 0 60
2 60 2 60
} }
method 'printNarrowed ()V' {
2 64
5 64
6 64
b 65
e 65
f 65
12 66
}
} }
Lines mapping: Lines mapping:
@ -219,3 +234,6 @@ Lines mapping:
56 <-> 53 56 <-> 53
60 <-> 57 60 <-> 57
64 <-> 61 64 <-> 61
68 <-> 65
69 <-> 66
70 <-> 67

@ -63,4 +63,9 @@ public class TestPrimitives {
public int getInt() { public int getInt() {
return 42; return 42;
} }
public void printNarrowed() {
printByte((byte)getInt());
printShort((short)getInt());
}
} }

Loading…
Cancel
Save