Search in sources :

Example 21 with TypedValue

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

the class CompoundExpression method getValueInternal.

/**
 * Evaluates a compound expression. This involves evaluating each piece in turn and the
 * return value from each piece is the active context object for the subsequent piece.
 *
 * @param state the state in which the expression is being evaluated
 * @return the final value from the last piece of the compound expression
 */
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    ValueRef ref = getValueRef(state);
    TypedValue result = ref.getValue();
    this.exitTypeDescriptor = this.children[this.children.length - 1].exitTypeDescriptor;
    return result;
}
Also used : TypedValue(cn.taketoday.expression.TypedValue)

Example 22 with TypedValue

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

the class Elvis method getValueInternal.

/**
 * Evaluate the condition and if not null, return it.
 * If it is null, return the other value.
 *
 * @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 {
    TypedValue value = this.children[0].getValueInternal(state);
    // If this check is changed, the generateCode method will need changing too
    if (value.getValue() != null && !"".equals(value.getValue())) {
        return value;
    } else {
        TypedValue result = this.children[1].getValueInternal(state);
        computeExitTypeDescriptor();
        return result;
    }
}
Also used : TypedValue(cn.taketoday.expression.TypedValue)

Example 23 with TypedValue

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

the class ReflectiveConstructorExecutor method execute.

@Override
public TypedValue execute(EvaluationContext context, Object... arguments) throws AccessException {
    try {
        ReflectionHelper.convertArguments(context.getTypeConverter(), arguments, this.ctor, this.varargsPosition);
        if (this.ctor.isVarArgs()) {
            arguments = ReflectionHelper.setupArgumentsForVarargsInvocation(this.ctor.getParameterTypes(), arguments);
        }
        ReflectionUtils.makeAccessible(this.ctor);
        return new TypedValue(this.ctor.newInstance(arguments));
    } catch (Exception ex) {
        throw new AccessException("Problem invoking constructor: " + this.ctor, ex);
    }
}
Also used : AccessException(cn.taketoday.expression.AccessException) AccessException(cn.taketoday.expression.AccessException) TypedValue(cn.taketoday.expression.TypedValue)

Example 24 with TypedValue

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

the class SpelExpression method getValue.

@SuppressWarnings("unchecked")
@Override
@Nullable
public <T> T getValue(EvaluationContext context, @Nullable Class<T> expectedResultType) throws EvaluationException {
    Assert.notNull(context, "EvaluationContext is required");
    CompiledExpression compiledAst = this.compiledAst;
    if (compiledAst != null) {
        try {
            Object result = compiledAst.getValue(context.getRootObject().getValue(), context);
            if (expectedResultType != null) {
                return ExpressionUtils.convertTypedValue(context, new TypedValue(result), expectedResultType);
            } else {
                return (T) result;
            }
        } catch (Throwable ex) {
            // If running in mixed mode, revert to interpreted
            if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
                this.compiledAst = null;
                this.interpretedCount.set(0);
            } else {
                // Running in SpelCompilerMode.immediate mode - propagate exception to caller
                throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION);
            }
        }
    }
    ExpressionState expressionState = new ExpressionState(context, this.configuration);
    TypedValue typedResultValue = this.ast.getTypedValue(expressionState);
    checkCompile(expressionState);
    return ExpressionUtils.convertTypedValue(context, typedResultValue, expectedResultType);
}
Also used : SpelEvaluationException(cn.taketoday.expression.spel.SpelEvaluationException) ExpressionState(cn.taketoday.expression.spel.ExpressionState) CompiledExpression(cn.taketoday.expression.spel.CompiledExpression) TypedValue(cn.taketoday.expression.TypedValue) Nullable(cn.taketoday.lang.Nullable)

Example 25 with TypedValue

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

the class QualifiedIdentifier method getValueInternal.

@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
    // Cache the concatenation of child identifiers
    if (this.value == null) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < getChildCount(); i++) {
            Object value = this.children[i].getValueInternal(state).getValue();
            if (i > 0 && (value == null || !value.toString().startsWith("$"))) {
                sb.append('.');
            }
            sb.append(value);
        }
        this.value = new TypedValue(sb.toString());
    }
    return this.value;
}
Also used : TypedValue(cn.taketoday.expression.TypedValue)

Aggregations

TypedValue (cn.taketoday.expression.TypedValue)64 Test (org.junit.jupiter.api.Test)34 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)28 ExpressionState (cn.taketoday.expression.spel.ExpressionState)18 SpelEvaluationException (cn.taketoday.expression.spel.SpelEvaluationException)14 EvaluationContext (cn.taketoday.expression.EvaluationContext)12 Expression (cn.taketoday.expression.Expression)8 CompiledExpression (cn.taketoday.expression.spel.CompiledExpression)8 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)8 Nullable (cn.taketoday.lang.Nullable)8 MethodParameter (cn.taketoday.core.MethodParameter)6 TypeDescriptor (cn.taketoday.core.TypeDescriptor)6 AccessException (cn.taketoday.expression.AccessException)6 EvaluationException (cn.taketoday.expression.EvaluationException)4 ReflectivePropertyAccessor (cn.taketoday.expression.spel.support.ReflectivePropertyAccessor)4 Time (java.sql.Time)4 Date (java.util.Date)4 GenericConversionService (cn.taketoday.core.conversion.support.GenericConversionService)2 ExpressionException (cn.taketoday.expression.ExpressionException)2 ExpressionParser (cn.taketoday.expression.ExpressionParser)2