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

This commit is contained in:
Dmitry Cherniachenko
2017-04-19 01:16:47 +02:00
committed by Egor.Ushakov
parent 7e1cb88fe2
commit 0a7a60fa7b
6 changed files with 43 additions and 10 deletions

View File

@@ -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);

View File

@@ -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;