/** * Evaluate the condition and if true evaluate the first alternative, otherwise * evaluate the second alternative. * @param state the expression state * @throws EvaluationException if the condition does not evaluate correctly to * a boolean or there is a problem executing the chosen alternative */ @Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { Boolean value = this.children[0].getValue(state, Boolean.class); if (value == null) { throw new SpelEvaluationException(getChild(0).getStartPosition(), SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean"); } TypedValue result = this.children[value ? 1 : 2].getValueInternal(state); computeExitTypeDescriptor(); return result; }
@Override public void generateCode(MethodVisitor mv, CodeFlow cf) { computeExitTypeDescriptor(); cf.enterCompilationScope(); this.children[0].generateCode(mv, cf);
eatToken(TokenKind.COLON); SpelNodeImpl ifFalseExprValue = eatExpression(); return new Ternary(t.startPos, t.endPos, expr, ifTrueExprValue, ifFalseExprValue);
@Override public void generateCode(MethodVisitor mv, CodeFlow cf) { computeExitTypeDescriptor(); cf.enterCompilationScope(); this.children[0].generateCode(mv, cf);
eatToken(TokenKind.COLON); SpelNodeImpl ifFalseExprValue = eatExpression(); return new Ternary(toPos(t), expr, ifTrueExprValue, ifFalseExprValue);
/** * Evaluate the condition and if true evaluate the first alternative, otherwise * evaluate the second alternative. * @param state the expression state * @throws EvaluationException if the condition does not evaluate correctly to * a boolean or there is a problem executing the chosen alternative */ @Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { Boolean value = this.children[0].getValue(state, Boolean.class); if (value == null) { throw new SpelEvaluationException(getChild(0).getStartPosition(), SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean"); } TypedValue result = this.children[value ? 1 : 2].getValueInternal(state); computeExitTypeDescriptor(); return result; }
@Test public void mixingItUp_propertyAccessIndexerOpLtTernaryRootNull() throws Exception { Payload payload = new Payload(); expression = parser.parseExpression("DR[0].three"); Object v = expression.getValue(payload); assertEquals("Lorg/springframework/expression/spel/SpelCompilationCoverageTests$Three", getAst().getExitDescriptor()); Expression expression = parser.parseExpression("DR[0].three.four lt 0.1d?#root:null"); v = expression.getValue(payload); SpelExpression sExpr = (SpelExpression) expression; Ternary ternary = (Ternary) sExpr.getAST(); OpLT oplt = (OpLT) ternary.getChild(0); CompoundExpression cExpr = (CompoundExpression) oplt.getLeftOperand(); String cExprExitDescriptor = cExpr.getExitDescriptor(); assertEquals("D", cExprExitDescriptor); assertEquals("Z", oplt.getExitDescriptor()); assertCanCompile(expression); Object vc = expression.getValue(payload); assertEquals(payload, v); assertEquals(payload,vc); payload.DR[0].three.four = 0.13d; vc = expression.getValue(payload); assertNull(vc); }
@Override public void generateCode(MethodVisitor mv, CodeFlow cf) { computeExitTypeDescriptor(); cf.enterCompilationScope(); this.children[0].generateCode(mv, cf);
eatToken(TokenKind.COLON); SpelNodeImpl ifFalseExprValue = eatExpression(); return new Ternary(toPos(t), expr, ifTrueExprValue, ifFalseExprValue);
/** * Evaluate the condition and if true evaluate the first alternative, otherwise * evaluate the second alternative. * @param state the expression state * @throws EvaluationException if the condition does not evaluate correctly to * a boolean or there is a problem executing the chosen alternative */ @Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { Boolean value = this.children[0].getValue(state, Boolean.class); if (value == null) { throw new SpelEvaluationException(getChild(0).getStartPosition(), SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean"); } TypedValue result = this.children[value ? 1 : 2].getValueInternal(state); computeExitTypeDescriptor(); return result; }