org.springframework.expression.spel.ExpressionState Java Examples
The following examples show how to use
org.springframework.expression.spel.ExpressionState.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OpPlusTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void test_binaryPlusWithTime_ToString() { ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext()); Time time = new Time(new Date().getTime()); VariableReference var = new VariableReference("timeVar", -1); var.setValue(expressionState, time); StringLiteral n2 = new StringLiteral("\" is now\"", -1, "\" is now\""); OpPlus o = new OpPlus(-1, var, n2); TypedValue value = o.getValueInternal(expressionState); assertEquals(String.class, value.getTypeDescriptor().getObjectType()); assertEquals(String.class, value.getTypeDescriptor().getType()); assertEquals(time + " is now", value.getValue()); }
Example #2
Source File: OperatorBetween.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * Returns a boolean based on whether a value is in the range expressed. The first * operand is any value whilst the second is a list of two values - those two values * being the bounds allowed for the first operand (inclusive). * @param state the expression state * @return true if the left operand is in the range specified, false otherwise * @throws EvaluationException if there is a problem evaluating the expression */ @Override public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException { Object left = getLeftOperand().getValueInternal(state).getValue(); Object right = getRightOperand().getValueInternal(state).getValue(); if (!(right instanceof List) || ((List<?>) right).size() != 2) { throw new SpelEvaluationException(getRightOperand().getStartPosition(), SpelMessage.BETWEEN_RIGHT_OPERAND_MUST_BE_TWO_ELEMENT_LIST); } List<?> list = (List<?>) right; Object low = list.get(0); Object high = list.get(1); TypeComparator comp = state.getTypeComparator(); try { return BooleanTypedValue.forValue(comp.compare(left, low) >= 0 && comp.compare(left, high) <= 0); } catch (SpelEvaluationException ex) { ex.setPosition(getStartPosition()); throw ex; } }
Example #3
Source File: FunctionReference.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { TypedValue value = state.lookupVariable(this.name); if (value == null) { throw new SpelEvaluationException(getStartPosition(), SpelMessage.FUNCTION_NOT_DEFINED, this.name); } // Two possibilities: a lambda function or a Java static method registered as a function if (!(value.getValue() instanceof Method)) { throw new SpelEvaluationException( SpelMessage.FUNCTION_REFERENCE_CANNOT_BE_INVOKED, this.name, value.getClass()); } try { return executeFunctionJLRMethod(state, (Method) value.getValue()); } catch (SpelEvaluationException ex) { ex.setPosition(getStartPosition()); throw ex; } }
Example #4
Source File: SpelExpression.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Override public Object getValue(EvaluationContext context) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); if (compiledAst!= null) { try { TypedValue contextRoot = context == null ? null : context.getRootObject(); return this.compiledAst.getValue(contextRoot != null ? contextRoot.getValue() : null, context); } catch (Throwable ex) { // If running in mixed mode, revert to interpreted if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) { this.interpretedCount = 0; this.compiledAst = null; } 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); Object result = this.ast.getValue(expressionState); checkCompile(expressionState); return result; }
Example #5
Source File: SpelExpression.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Override public Object getValue(EvaluationContext context, Object rootObject) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); if (this.compiledAst != null) { try { return this.compiledAst.getValue(rootObject,context); } catch (Throwable ex) { // If running in mixed mode, revert to interpreted if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) { this.interpretedCount = 0; this.compiledAst = null; } else { // Running in SpelCompilerMode.immediate mode - propagate exception to caller throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION); } } } ExpressionState expressionState = new ExpressionState(context, toTypedValue(rootObject), this.configuration); Object result = this.ast.getValue(expressionState); checkCompile(expressionState); return result; }
Example #6
Source File: BeanReference.java From lams with GNU General Public License v2.0 | 6 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { BeanResolver beanResolver = state.getEvaluationContext().getBeanResolver(); if (beanResolver == null) { throw new SpelEvaluationException( getStartPosition(), SpelMessage.NO_BEAN_RESOLVER_REGISTERED, this.beanName); } try { return new TypedValue(beanResolver.resolve(state.getEvaluationContext(), this.beanName)); } catch (AccessException ex) { throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_BEAN_RESOLUTION, this.beanName, ex.getMessage()); } }
Example #7
Source File: BeanReference.java From spring-analysis-note with MIT License | 6 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { BeanResolver beanResolver = state.getEvaluationContext().getBeanResolver(); if (beanResolver == null) { throw new SpelEvaluationException( getStartPosition(), SpelMessage.NO_BEAN_RESOLVER_REGISTERED, this.beanName); } try { return new TypedValue(beanResolver.resolve(state.getEvaluationContext(), this.beanName)); } catch (AccessException ex) { throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_BEAN_RESOLUTION, this.beanName, ex.getMessage()); } }
Example #8
Source File: SpelExpression.java From java-technology-stack with MIT License | 6 votes |
@Override @Nullable public Object getValue() throws EvaluationException { if (this.compiledAst != null) { try { EvaluationContext context = getEvaluationContext(); return this.compiledAst.getValue(context.getRootObject().getValue(), context); } catch (Throwable ex) { // If running in mixed mode, revert to interpreted if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) { this.interpretedCount = 0; this.compiledAst = null; } else { // Running in SpelCompilerMode.immediate mode - propagate exception to caller throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION); } } } ExpressionState expressionState = new ExpressionState(getEvaluationContext(), this.configuration); Object result = this.ast.getValue(expressionState); checkCompile(expressionState); return result; }
Example #9
Source File: SpelExpression.java From lams with GNU General Public License v2.0 | 6 votes |
@Override public Object getValue(EvaluationContext context, Object rootObject) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); if (this.compiledAst != null) { try { return this.compiledAst.getValue(rootObject,context); } catch (Throwable ex) { // If running in mixed mode, revert to interpreted if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) { this.interpretedCount = 0; this.compiledAst = null; } else { // Running in SpelCompilerMode.immediate mode - propagate exception to caller throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION); } } } ExpressionState expressionState = new ExpressionState(context, toTypedValue(rootObject), this.configuration); Object result = this.ast.getValue(expressionState); checkCompile(expressionState); return result; }
Example #10
Source File: SpelExpression.java From spring-analysis-note with MIT License | 6 votes |
@Override @Nullable public Object getValue(Object rootObject) throws EvaluationException { if (this.compiledAst != null) { try { return this.compiledAst.getValue(rootObject, getEvaluationContext()); } catch (Throwable ex) { // If running in mixed mode, revert to interpreted if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) { this.interpretedCount.set(0); this.compiledAst = null; } else { // Running in SpelCompilerMode.immediate mode - propagate exception to caller throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION); } } } ExpressionState expressionState = new ExpressionState(getEvaluationContext(), toTypedValue(rootObject), this.configuration); Object result = this.ast.getValue(expressionState); checkCompile(expressionState); return result; }
Example #11
Source File: SpelExpression.java From spring-analysis-note with MIT License | 5 votes |
@SuppressWarnings("unchecked") @Override @Nullable public <T> T getValue(EvaluationContext context, @Nullable Class<T> expectedResultType) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); if (this.compiledAst != null) { try { Object result = this.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.interpretedCount.set(0); this.compiledAst = null; } 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); }
Example #12
Source File: SpelExpression.java From spring-analysis-note with MIT License | 5 votes |
@SuppressWarnings("unchecked") @Override @Nullable public <T> T getValue(@Nullable Class<T> expectedResultType) throws EvaluationException { if (this.compiledAst != null) { try { EvaluationContext context = getEvaluationContext(); Object result = this.compiledAst.getValue(context.getRootObject().getValue(), context); if (expectedResultType == null) { return (T) result; } else { return ExpressionUtils.convertTypedValue( getEvaluationContext(), new TypedValue(result), expectedResultType); } } catch (Throwable ex) { // If running in mixed mode, revert to interpreted if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) { this.interpretedCount.set(0); this.compiledAst = null; } else { // Running in SpelCompilerMode.immediate mode - propagate exception to caller throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION); } } } ExpressionState expressionState = new ExpressionState(getEvaluationContext(), this.configuration); TypedValue typedResultValue = this.ast.getTypedValue(expressionState); checkCompile(expressionState); return ExpressionUtils.convertTypedValue( expressionState.getEvaluationContext(), typedResultValue, expectedResultType); }
Example #13
Source File: SpelExpression.java From spring-analysis-note with MIT License | 5 votes |
@Override @Nullable public TypeDescriptor getValueTypeDescriptor(Object rootObject) throws EvaluationException { ExpressionState expressionState = new ExpressionState(getEvaluationContext(), toTypedValue(rootObject), this.configuration); return this.ast.getValueInternal(expressionState).getTypeDescriptor(); }
Example #14
Source File: Ternary.java From java-technology-stack with MIT License | 5 votes |
/** * 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; }
Example #15
Source File: SpelExpression.java From spring-analysis-note with MIT License | 5 votes |
@SuppressWarnings("unchecked") @Override @Nullable public <T> T getValue(EvaluationContext context, Object rootObject, @Nullable Class<T> expectedResultType) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); if (this.compiledAst != null) { try { Object result = this.compiledAst.getValue(rootObject, 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.interpretedCount.set(0); this.compiledAst = null; } else { // Running in SpelCompilerMode.immediate mode - propagate exception to caller throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION); } } } ExpressionState expressionState = new ExpressionState(context, toTypedValue(rootObject), this.configuration); TypedValue typedResultValue = this.ast.getTypedValue(expressionState); checkCompile(expressionState); return ExpressionUtils.convertTypedValue(context, typedResultValue, expectedResultType); }
Example #16
Source File: OpEQ.java From java-technology-stack with MIT License | 5 votes |
@Override public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException { Object left = getLeftOperand().getValueInternal(state).getValue(); Object right = getRightOperand().getValueInternal(state).getValue(); this.leftActualDescriptor = CodeFlow.toDescriptorFromObject(left); this.rightActualDescriptor = CodeFlow.toDescriptorFromObject(right); return BooleanTypedValue.forValue(equalityCheck(state.getEvaluationContext(), left, right)); }
Example #17
Source File: SpelExpression.java From spring-analysis-note with MIT License | 5 votes |
@Override @Nullable public Object getValue(EvaluationContext context) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); if (this.compiledAst != null) { try { return this.compiledAst.getValue(context.getRootObject().getValue(), context); } catch (Throwable ex) { // If running in mixed mode, revert to interpreted if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) { this.interpretedCount.set(0); this.compiledAst = null; } 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); Object result = this.ast.getValue(expressionState); checkCompile(expressionState); return result; }
Example #18
Source File: CompoundExpression.java From java-technology-stack with MIT License | 5 votes |
/** * 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; }
Example #19
Source File: OpEQ.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException { Object left = getLeftOperand().getValueInternal(state).getValue(); Object right = getRightOperand().getValueInternal(state).getValue(); this.leftActualDescriptor = CodeFlow.toDescriptorFromObject(left); this.rightActualDescriptor = CodeFlow.toDescriptorFromObject(right); return BooleanTypedValue.forValue( equalityCheck(state.getEvaluationContext(), left, right)); }
Example #20
Source File: MethodReference.java From spring-analysis-note with MIT License | 5 votes |
private Object[] getArguments(ExpressionState state) { Object[] arguments = new Object[getChildCount()]; for (int i = 0; i < arguments.length; i++) { // Make the root object the active context again for evaluating the parameter expressions try { state.pushActiveContextObject(state.getScopeRootContextObject()); arguments[i] = this.children[i].getValueInternal(state).getValue(); } finally { state.popActiveContextObject(); } } return arguments; }
Example #21
Source File: OpPlus.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Convert operand value to string using registered converter or using * {@code toString} method. * @param value typed value to be converted * @param state expression state * @return {@code TypedValue} instance converted to {@code String} */ private static String convertTypedValueToString(TypedValue value, ExpressionState state) { TypeConverter typeConverter = state.getEvaluationContext().getTypeConverter(); TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(String.class); if (typeConverter.canConvert(value.getTypeDescriptor(), typeDescriptor)) { return String.valueOf(typeConverter.convertValue(value.getValue(), value.getTypeDescriptor(), typeDescriptor)); } return String.valueOf(value.getValue()); }
Example #22
Source File: ConstructorReference.java From spring4-understanding with Apache License 2.0 | 5 votes |
private void populateShortArray(ExpressionState state, Object newArray, TypeConverter typeConverter, InlineList initializer) { short[] newShortArray = (short[]) newArray; for (int i = 0; i < newShortArray.length; i++) { TypedValue typedValue = initializer.getChild(i).getTypedValue(state); newShortArray[i] = ExpressionUtils.toShort(typeConverter, typedValue); } }
Example #23
Source File: SpelExpression.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public Object getValue(EvaluationContext context) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); if (this.compiledAst != null) { try { TypedValue contextRoot = context.getRootObject(); return this.compiledAst.getValue(contextRoot.getValue(), context); } catch (Throwable ex) { // If running in mixed mode, revert to interpreted if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) { this.interpretedCount = 0; this.compiledAst = null; } 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); Object result = this.ast.getValue(expressionState); checkCompile(expressionState); return result; }
Example #24
Source File: OperatorNot.java From java-technology-stack with MIT License | 5 votes |
@Override public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException { try { Boolean value = this.children[0].getValue(state, Boolean.class); if (value == null) { throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean"); } return BooleanTypedValue.forValue(!value); } catch (SpelEvaluationException ex) { ex.setPosition(getChild(0).getStartPosition()); throw ex; } }
Example #25
Source File: Elvis.java From spring-analysis-note with MIT License | 5 votes |
/** * 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 (!StringUtils.isEmpty(value.getValue())) { return value; } else { TypedValue result = this.children[1].getValueInternal(state); computeExitTypeDescriptor(); return result; } }
Example #26
Source File: PropertyOrFieldReference.java From java-technology-stack with MIT License | 5 votes |
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { TypedValue tv = getValueInternal(state.getActiveContextObject(), state.getEvaluationContext(), state.getConfiguration().isAutoGrowNullReferences()); PropertyAccessor accessorToUse = this.cachedReadAccessor; if (accessorToUse instanceof CompilablePropertyAccessor) { CompilablePropertyAccessor accessor = (CompilablePropertyAccessor) accessorToUse; setExitTypeDescriptor(CodeFlow.toDescriptor(accessor.getPropertyType())); } return tv; }
Example #27
Source File: Ternary.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * 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; }
Example #28
Source File: FunctionReference.java From spring-analysis-note with MIT License | 5 votes |
/** * Compute the arguments to the function, they are the children of this expression node. * @return an array of argument values for the function call */ private Object[] getArguments(ExpressionState state) throws EvaluationException { // Compute arguments to the function Object[] arguments = new Object[getChildCount()]; for (int i = 0; i < arguments.length; i++) { arguments[i] = this.children[i].getValueInternal(state).getValue(); } return arguments; }
Example #29
Source File: OpNE.java From spring-analysis-note with MIT License | 5 votes |
@Override public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException { Object leftValue = getLeftOperand().getValueInternal(state).getValue(); Object rightValue = getRightOperand().getValueInternal(state).getValue(); this.leftActualDescriptor = CodeFlow.toDescriptorFromObject(leftValue); this.rightActualDescriptor = CodeFlow.toDescriptorFromObject(rightValue); return BooleanTypedValue.forValue(!equalityCheck(state.getEvaluationContext(), leftValue, rightValue)); }
Example #30
Source File: OpAnd.java From java-technology-stack with MIT License | 5 votes |
private boolean getBooleanValue(ExpressionState state, SpelNodeImpl operand) { try { Boolean value = operand.getValue(state, Boolean.class); assertValueNotNull(value); return value; } catch (SpelEvaluationException ex) { ex.setPosition(operand.getStartPosition()); throw ex; } }