private Boolean getKnownValue ( Expression expr ) { Object value = expr . getConstantValue ( ) ; if ( value instanceof Boolean ) { return ( Boolean ) value ; } switch ( expr . getKind ( ) ) { case BOOLEAN_LITERAL : return ( ( BooleanLiteral ) expr ) . booleanValue ( ) ; case INFIX_EXPRESSION : { InfixExpression infixExpr = ( InfixExpression ) expr ; InfixExpression . Operator op = infixExpr . getOperator ( ) ; if ( op == CONDITIONAL_AND || op == CONDITIONAL_OR ) { // We assume that this node has already been visited and pruned so // if it has a known value, it will be equal to the last operand. List < Expression > operands = infixExpr . getOperands ( ) ; Boolean lastOperand = getKnownValue ( operands . get ( operands . size ( ) - 1 ) ) ; if ( lastOperand != null && lastOperand . booleanValue ( ) == ( op == CONDITIONAL_OR ) ) { return lastOperand ; } } return null ; } case PARENTHESIZED_EXPRESSION : return getKnownValue ( ( ( ParenthesizedExpression ) expr ) . getExpression ( ) ) ; default : return null ; } }
Returns TRUE of FALSE if expr is a boolean expression and its value is known statically . The caller should be careful when replacing this expression as it may have side effects .