IDEA-204223 Decompiler doesn't add mandatory narrowing cast on integer type

This commit is contained in:
Egor Ushakov
2018-12-14 17:56:47 +03:00
parent e9989d15e3
commit f320e3abd4
6 changed files with 499 additions and 476 deletions

View File

@@ -1,6 +1,4 @@
/* // Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
* Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package org.jetbrains.java.decompiler.modules.decompiler; package org.jetbrains.java.decompiler.modules.decompiler;
import org.jetbrains.java.decompiler.code.CodeConstants; import org.jetbrains.java.decompiler.code.CodeConstants;
@@ -855,7 +853,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, false, tracer); return getCastedExprent(exprent, leftType, buffer, indent, castNull, false, false, false, tracer);
} }
public static boolean getCastedExprent(Exprent exprent, public static boolean getCastedExprent(Exprent exprent,
@@ -865,7 +863,21 @@ public class ExprProcessor implements CodeConstants {
boolean castNull, boolean castNull,
boolean castAlways, boolean castAlways,
boolean castNarrowing, boolean castNarrowing,
boolean unbox,
BytecodeMappingTracer tracer) { BytecodeMappingTracer tracer) {
if (unbox) {
// "unbox" invocation parameters, e.g. 'byteSet.add((byte)123)' or 'new ShortContainer((short)813)'
if (exprent.type == Exprent.EXPRENT_INVOCATION && ((InvocationExprent)exprent).isBoxingCall()) {
InvocationExprent invocationExprent = (InvocationExprent)exprent;
exprent = invocationExprent.getLstParameters().get(0);
int paramType = invocationExprent.getDescriptor().params[0].type;
if (exprent.type == Exprent.EXPRENT_CONST && ((ConstExprent)exprent).getConstType().type != paramType) {
leftType = new VarType(paramType);
}
}
}
VarType rightType = exprent.getExprType(); VarType rightType = exprent.getExprType();
boolean cast = boolean cast =

View File

@@ -1,6 +1,4 @@
/* // Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
* Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package org.jetbrains.java.decompiler.modules.decompiler.exps; package org.jetbrains.java.decompiler.modules.decompiler.exps;
import org.jetbrains.java.decompiler.code.CodeConstants; import org.jetbrains.java.decompiler.code.CodeConstants;
@@ -214,7 +212,7 @@ public class InvocationExprent extends Exprent {
if (isBoxingCall() && canIgnoreBoxing) { if (isBoxingCall() && canIgnoreBoxing) {
// process general "boxing" calls, e.g. 'Object[] data = { true }' or 'Byte b = 123' // 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 // 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); ExprProcessor.getCastedExprent(lstParameters.get(0), descriptor.params[0], buf, indent, false, false, false, false, tracer);
return buf; return buf;
} }
@@ -351,9 +349,8 @@ public class InvocationExprent extends Exprent {
TextBuffer buff = new TextBuffer(); TextBuffer buff = new TextBuffer();
boolean ambiguous = setAmbiguousParameters.get(i); boolean ambiguous = setAmbiguousParameters.get(i);
Exprent param = unboxIfNeeded(lstParameters.get(i));
// 'byte' and 'short' literals need an explicit narrowing type cast when used as a parameter // '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); ExprProcessor.getCastedExprent(lstParameters.get(i), descriptor.params[i], buff, indent, true, ambiguous, true, true, tracer);
// the last "new Object[0]" in the vararg call is not printed // the last "new Object[0]" in the vararg call is not printed
if (buff.length() > 0) { if (buff.length() > 0) {
@@ -372,14 +369,6 @@ public class InvocationExprent extends Exprent {
return buf; return buf;
} }
public static Exprent unboxIfNeeded(Exprent param) {
// "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);
}
return param;
}
private boolean isVarArgCall() { private boolean isVarArgCall() {
StructClass cl = DecompilerContext.getStructContext().getClass(classname); StructClass cl = DecompilerContext.getStructContext().getClass(classname);
if (cl != null) { if (cl != null) {
@@ -398,7 +387,7 @@ public class InvocationExprent extends Exprent {
return false; return false;
} }
private boolean isBoxingCall() { public boolean isBoxingCall() {
if (isStatic && "valueOf".equals(name) && lstParameters.size() == 1) { if (isStatic && "valueOf".equals(name) && lstParameters.size() == 1) {
int paramType = lstParameters.get(0).getExprType().type; int paramType = lstParameters.get(0).getExprType().type;

View File

@@ -1,6 +1,4 @@
/* // Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
* Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package org.jetbrains.java.decompiler.modules.decompiler.exps; package org.jetbrains.java.decompiler.modules.decompiler.exps;
import org.jetbrains.java.decompiler.code.CodeConstants; import org.jetbrains.java.decompiler.code.CodeConstants;
@@ -282,7 +280,7 @@ public class NewExprent extends Exprent {
boolean firstParam = true; boolean firstParam = true;
for (int i = start; i < parameters.size(); i++) { for (int i = start; i < parameters.size(); i++) {
if (mask == null || mask.get(i) == null) { if (mask == null || mask.get(i) == null) {
Exprent expr = InvocationExprent.unboxIfNeeded(parameters.get(i)); Exprent expr = parameters.get(i);
VarType leftType = constructor.getDescriptor().params[i]; VarType leftType = constructor.getDescriptor().params[i];
if (i == parameters.size() - 1 && expr.getExprType() == VarType.VARTYPE_NULL && probablySyntheticParameter(leftType.value)) { if (i == parameters.size() - 1 && expr.getExprType() == VarType.VARTYPE_NULL && probablySyntheticParameter(leftType.value)) {
@@ -293,7 +291,7 @@ public class NewExprent extends Exprent {
buf.append(", "); buf.append(", ");
} }
ExprProcessor.getCastedExprent(expr, leftType, buf, indent, true, false, true, tracer); ExprProcessor.getCastedExprent(expr, leftType, buf, indent, true, false, true, true, tracer);
firstParam = false; firstParam = false;
} }

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,7 @@
package pkg; package pkg;
import java.util.*;
public class TestPrimitives { public class TestPrimitives {
public void printAll() { public void printAll() {
@@ -171,4 +173,8 @@ public class TestPrimitives {
Boolean.valueOf(value).hashCode(); Boolean.valueOf(value).hashCode();
} }
void testCastRequired() {
HashMap<String, Byte> map = new HashMap<String, Byte>();
map.put("test", (byte) 0);
}
} }