boolean binary expressions of the format false && non_constant and true || non_constant can be evaluated as constant at compile time

This commit is contained in:
Bas Leijdekkers
2011-06-27 23:28:02 +02:00
parent ea6dca5366
commit 3645fce7be
3 changed files with 60 additions and 30 deletions
@@ -113,15 +113,43 @@ class ConstantExpressionVisitor extends JavaElementVisitor implements PsiConstan
}
Object rOperandValue = getStoredValue(expression.getROperand());
if (rOperandValue == null) {
myResult = null;
return;
}
PsiJavaToken operationSign = expression.getOperationSign();
final IElementType tokenType = operationSign.getTokenType();
Object value = null;
if (tokenType == JavaTokenType.ANDAND) {
if (lOperandValue instanceof Boolean && !((Boolean)lOperandValue).booleanValue()) {
myResult = Boolean.FALSE;
return;
}
if (rOperandValue instanceof Boolean && !((Boolean)rOperandValue).booleanValue()) {
myResult = Boolean.FALSE;
return;
}
if (lOperandValue instanceof Boolean && rOperandValue instanceof Boolean) {
value = Boolean.valueOf(((Boolean)lOperandValue).booleanValue() && ((Boolean)rOperandValue).booleanValue());
}
}
else if (tokenType == JavaTokenType.OROR) {
if (lOperandValue instanceof Boolean && ((Boolean)lOperandValue).booleanValue()) {
myResult = Boolean.TRUE;
return;
}
if (rOperandValue instanceof Boolean && ((Boolean)rOperandValue).booleanValue()) {
myResult = Boolean.TRUE;
return;
}
if (lOperandValue instanceof Boolean && rOperandValue instanceof Boolean) {
value = Boolean.valueOf(((Boolean)lOperandValue).booleanValue() || ((Boolean)rOperandValue).booleanValue());
}
}
if (rOperandValue == null) {
myResult = null;
return;
}
if (tokenType == JavaTokenType.PLUS) {
if (lOperandValue instanceof String || rOperandValue instanceof String) {
value = myInterner.intern(lOperandValue.toString() + rOperandValue.toString());
@@ -180,32 +208,6 @@ class ConstantExpressionVisitor extends JavaElementVisitor implements PsiConstan
}
}
}
else if (tokenType == JavaTokenType.ANDAND) {
if (lOperandValue instanceof Boolean && !((Boolean)lOperandValue).booleanValue()) {
myResult = Boolean.FALSE;
return;
}
if (rOperandValue instanceof Boolean && !((Boolean)rOperandValue).booleanValue()) {
myResult = Boolean.FALSE;
return;
}
if (lOperandValue instanceof Boolean && rOperandValue instanceof Boolean) {
value = Boolean.valueOf(((Boolean)lOperandValue).booleanValue() && ((Boolean)rOperandValue).booleanValue());
}
}
else if (tokenType == JavaTokenType.OROR) {
if (lOperandValue instanceof Boolean && ((Boolean)lOperandValue).booleanValue()) {
myResult = Boolean.TRUE;
return;
}
if (rOperandValue instanceof Boolean && ((Boolean)rOperandValue).booleanValue()) {
myResult = Boolean.TRUE;
return;
}
if (lOperandValue instanceof Boolean && rOperandValue instanceof Boolean) {
value = Boolean.valueOf(((Boolean)lOperandValue).booleanValue() || ((Boolean)rOperandValue).booleanValue());
}
}
else if (tokenType == JavaTokenType.LT) {
if (lOperandValue instanceof Character) lOperandValue = Integer.valueOf(((Character)lOperandValue).charValue());
if (rOperandValue instanceof Character) rOperandValue = Integer.valueOf(((Character)rOperandValue).charValue());
@@ -9,6 +9,12 @@ public class ClassWithConstants {
public static final byte BYTE_CONST = 4;
public static final char CHAR_CONST = '5';
public static final boolean BOOL_CONST = true;
public static final boolean BOOL_CONST2 = false && foo();
public static final boolean BOOL_CONST3 = true || foo();
static boolean foo() {
return false;
}
public static final float FLOAT_CONST = 1.234f;
public static final double DOUBLE_CONST = 3.456;
public static final java.lang.String STRING_CONST = "a\r\n\"bcd";
@@ -197,6 +197,28 @@ public class ConstantValuesTest extends PsiTestCase{
assertEquals(Boolean.TRUE, field.computeConstantValue());
}
public void testBoolean2(){
PsiField field = myClass.findFieldByName("BOOL_CONST2", false);
assertNotNull(field);
final PsiExpression initializer = field.getInitializer();
assertNotNull(initializer);
assertEquals(PsiType.BOOLEAN, initializer.getType());
assertEquals("false && foo()", initializer.getText());
assertEquals(Boolean.FALSE, field.computeConstantValue());
}
public void testBoolean3(){
PsiField field = myClass.findFieldByName("BOOL_CONST3", false);
assertNotNull(field);
final PsiExpression initializer = field.getInitializer();
assertNotNull(initializer);
assertEquals(PsiType.BOOLEAN, initializer.getType());
assertEquals("true || foo()", initializer.getText());
assertEquals(Boolean.TRUE, field.computeConstantValue());
}
public void testFloat(){
PsiField field = myClass.findFieldByName("FLOAT_CONST", false);
assertNotNull(field);