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

master
Egor Ushakov 5 years ago
parent e9989d15e3
commit f320e3abd4
  1. 20
      src/org/jetbrains/java/decompiler/modules/decompiler/ExprProcessor.java
  2. 19
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/InvocationExprent.java
  3. 8
      src/org/jetbrains/java/decompiler/modules/decompiler/exps/NewExprent.java
  4. BIN
      testData/classes/pkg/TestPrimitives.class
  5. 922
      testData/results/TestPrimitives.dec
  6. 6
      testData/src/pkg/TestPrimitives.java

@ -1,6 +1,4 @@
/*
* 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.
*/
// 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.
package org.jetbrains.java.decompiler.modules.decompiler;
import org.jetbrains.java.decompiler.code.CodeConstants;
@ -855,7 +853,7 @@ public class ExprProcessor implements CodeConstants {
int indent,
boolean castNull,
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,
@ -865,7 +863,21 @@ public class ExprProcessor implements CodeConstants {
boolean castNull,
boolean castAlways,
boolean castNarrowing,
boolean unbox,
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();
boolean cast =

@ -1,6 +1,4 @@
/*
* 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.
*/
// 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.
package org.jetbrains.java.decompiler.modules.decompiler.exps;
import org.jetbrains.java.decompiler.code.CodeConstants;
@ -214,7 +212,7 @@ public class InvocationExprent extends Exprent {
if (isBoxingCall() && canIgnoreBoxing) {
// 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);
ExprProcessor.getCastedExprent(lstParameters.get(0), descriptor.params[0], buf, indent, false, false, false, false, tracer);
return buf;
}
@ -351,9 +349,8 @@ public class InvocationExprent extends Exprent {
TextBuffer buff = new TextBuffer();
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
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
if (buff.length() > 0) {
@ -372,14 +369,6 @@ public class InvocationExprent extends Exprent {
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() {
StructClass cl = DecompilerContext.getStructContext().getClass(classname);
if (cl != null) {
@ -398,7 +387,7 @@ public class InvocationExprent extends Exprent {
return false;
}
private boolean isBoxingCall() {
public boolean isBoxingCall() {
if (isStatic && "valueOf".equals(name) && lstParameters.size() == 1) {
int paramType = lstParameters.get(0).getExprType().type;

@ -1,6 +1,4 @@
/*
* 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.
*/
// 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.
package org.jetbrains.java.decompiler.modules.decompiler.exps;
import org.jetbrains.java.decompiler.code.CodeConstants;
@ -282,7 +280,7 @@ public class NewExprent extends Exprent {
boolean firstParam = true;
for (int i = start; i < parameters.size(); i++) {
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];
if (i == parameters.size() - 1 && expr.getExprType() == VarType.VARTYPE_NULL && probablySyntheticParameter(leftType.value)) {
@ -293,7 +291,7 @@ public class NewExprent extends Exprent {
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;
}

File diff suppressed because it is too large Load Diff

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

Loading…
Cancel
Save