Search in sources :

Example 1 with OperatorOverloader

use of cn.taketoday.expression.OperatorOverloader in project today-infrastructure by TAKETODAY.

the class StandardComponentsTests method testStandardOperatorOverloader.

@Test
public void testStandardOperatorOverloader() throws EvaluationException {
    OperatorOverloader oo = new StandardOperatorOverloader();
    assertThat(oo.overridesOperation(Operation.ADD, null, null)).isFalse();
    assertThatExceptionOfType(EvaluationException.class).isThrownBy(() -> oo.operate(Operation.ADD, 2, 3));
}
Also used : EvaluationException(cn.taketoday.expression.EvaluationException) OperatorOverloader(cn.taketoday.expression.OperatorOverloader) Test(org.junit.jupiter.api.Test)

Example 2 with OperatorOverloader

use of cn.taketoday.expression.OperatorOverloader in project today-framework by TAKETODAY.

the class StandardComponentsTests method testStandardOperatorOverloader.

@Test
public void testStandardOperatorOverloader() throws EvaluationException {
    OperatorOverloader oo = new StandardOperatorOverloader();
    assertThat(oo.overridesOperation(Operation.ADD, null, null)).isFalse();
    assertThatExceptionOfType(EvaluationException.class).isThrownBy(() -> oo.operate(Operation.ADD, 2, 3));
}
Also used : EvaluationException(cn.taketoday.expression.EvaluationException) OperatorOverloader(cn.taketoday.expression.OperatorOverloader) Test(org.junit.jupiter.api.Test)

Example 3 with OperatorOverloader

use of cn.taketoday.expression.OperatorOverloader in project today-framework by TAKETODAY.

the class ExpressionState method operate.

public TypedValue operate(Operation op, @Nullable Object left, @Nullable Object right) throws EvaluationException {
    OperatorOverloader overloader = this.relatedContext.getOperatorOverloader();
    if (overloader.overridesOperation(op, left, right)) {
        Object returnValue = overloader.operate(op, left, right);
        return new TypedValue(returnValue);
    } else {
        String leftType = (left == null ? "null" : left.getClass().getName());
        String rightType = (right == null ? "null" : right.getClass().getName());
        throw new SpelEvaluationException(SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES, op, leftType, rightType);
    }
}
Also used : OperatorOverloader(cn.taketoday.expression.OperatorOverloader) TypedValue(cn.taketoday.expression.TypedValue)

Example 4 with OperatorOverloader

use of cn.taketoday.expression.OperatorOverloader in project today-infrastructure by TAKETODAY.

the class ExpressionState method operate.

public TypedValue operate(Operation op, @Nullable Object left, @Nullable Object right) throws EvaluationException {
    OperatorOverloader overloader = this.relatedContext.getOperatorOverloader();
    if (overloader.overridesOperation(op, left, right)) {
        Object returnValue = overloader.operate(op, left, right);
        return new TypedValue(returnValue);
    } else {
        String leftType = (left == null ? "null" : left.getClass().getName());
        String rightType = (right == null ? "null" : right.getClass().getName());
        throw new SpelEvaluationException(SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES, op, leftType, rightType);
    }
}
Also used : OperatorOverloader(cn.taketoday.expression.OperatorOverloader) TypedValue(cn.taketoday.expression.TypedValue)

Aggregations

OperatorOverloader (cn.taketoday.expression.OperatorOverloader)4 EvaluationException (cn.taketoday.expression.EvaluationException)2 TypedValue (cn.taketoday.expression.TypedValue)2 Test (org.junit.jupiter.api.Test)2