IDEA-127533 int field is displayed as char

This commit is contained in:
Unknown
2017-10-14 22:25:20 +02:00
parent 0f7a14bf7b
commit a94daf713e
9 changed files with 493 additions and 11 deletions

View File

@@ -459,6 +459,11 @@ public class ClassWriter {
}
else {
buffer.append(" = ");
if (initializer.type == Exprent.EXPRENT_CONST) {
((ConstExprent) initializer).adjustConstType(fieldType);
}
// FIXME: special case field initializer. Can map to more than one method (constructor) and bytecode intruction.
buffer.append(initializer.toJava(indent, tracer));
}

View File

@@ -896,23 +896,25 @@ public class ExprProcessor implements CodeConstants {
VarType rightType = exprent.getExprType();
TextBuffer res = exprent.toJava(indent, tracer);
boolean cast =
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));
boolean quote = cast && exprent.getPrecedence() >= FunctionExprent.getPrecedence(FunctionExprent.FUNCTION_CAST);
if (cast) {
if (exprent.getPrecedence() >= FunctionExprent.getPrecedence(FunctionExprent.FUNCTION_CAST)) {
res.enclose("(", ")");
}
if (cast) buffer.append('(').append(getCastTypeName(leftType)).append(')');
res.prepend("(" + getCastTypeName(leftType) + ")");
if (quote) buffer.append('(');
if (exprent.type == Exprent.EXPRENT_CONST) {
((ConstExprent) exprent).adjustConstType(leftType);
}
buffer.append(res);
buffer.append(exprent.toJava(indent, tracer));
if (quote) buffer.append(')');
return cast;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -138,6 +138,10 @@ public class AssignmentExprent extends Exprent {
buffer.append(left.toJava(indent, tracer));
}
if (right.type == EXPRENT_CONST) {
((ConstExprent) right).adjustConstType(leftType);
}
TextBuffer res = right.toJava(indent, tracer);
if (condType == CONDITION_NONE &&

View File

@@ -389,6 +389,21 @@ public class ConstExprent extends Exprent {
this.constType = constType;
}
public void adjustConstType(VarType expectedType) {
// BYTECHAR and SHORTCHAR => CHAR in the CHAR context
if (expectedType.equals(VarType.VARTYPE_CHAR) &&
(constType.equals(VarType.VARTYPE_BYTECHAR) || constType.equals(VarType.VARTYPE_SHORTCHAR))) {
if (getIntValue() != 0) {
setConstType(VarType.VARTYPE_CHAR);
}
}
// CHAR => INT in the INT context
else if (expectedType.equals(VarType.VARTYPE_INT) &&
constType.equals(VarType.VARTYPE_CHAR)) {
setConstType(VarType.VARTYPE_INT);
}
}
public Object getValue() {
return value;
}

View File

@@ -459,7 +459,20 @@ public class FunctionExprent extends Exprent {
.append(wrapOperandString(lstOperands.get(1), true, indent, tracer));
}
// try to determine more accurate type for 'char' literals
if (funcType >= FUNCTION_EQ) {
if (funcType <= FUNCTION_LE) {
Exprent left = lstOperands.get(0);
Exprent right = lstOperands.get(1);
if (right.type == EXPRENT_CONST) {
((ConstExprent) right).adjustConstType(left.getExprType());
}
else if (left.type == EXPRENT_CONST) {
((ConstExprent) left).adjustConstType(right.getExprType());
}
}
return wrapOperandString(lstOperands.get(0), false, indent, tracer)
.append(OPERATORS[funcType - FUNCTION_EQ + 11])
.append(wrapOperandString(lstOperands.get(1), true, indent, tracer));