@Nullable protected final <T> T getValue(ExpressionState state, Class<T> desiredReturnType) throws EvaluationException { return ExpressionUtils.convertTypedValue(state.getEvaluationContext(), getValueInternal(state), desiredReturnType); }
@Override public void setValue(ExpressionState state, @Nullable Object newValue) throws EvaluationException { writeProperty(state.getActiveContextObject(), state.getEvaluationContext(), this.name, newValue); }
@Override public boolean isWritable(ExpressionState state) throws EvaluationException { return isWritableProperty(this.name, state.getActiveContextObject(), state.getEvaluationContext()); }
@Override public ValueRef getValueRef(ExpressionState state) throws SpelEvaluationException { if (this.name.equals(THIS)) { return new ValueRef.TypedValueHolderValueRef(state.getActiveContextObject(),this); } if (this.name.equals(ROOT)) { return new ValueRef.TypedValueHolderValueRef(state.getRootContextObject(),this); } TypedValue result = state.lookupVariable(this.name); // a null value will mean either the value was null or the variable was not found return new VariableRef(this.name,result,state.getEvaluationContext()); }
@Override public ValueRef getValueRef(ExpressionState state) throws EvaluationException { return new AccessorLValue(this, state.getActiveContextObject(), state.getEvaluationContext(), state.getConfiguration().isAutoGrowNullReferences()); }
public MethodValueRef(ExpressionState state, Object[] arguments) { this.evaluationContext = state.getEvaluationContext(); this.value = state.getActiveContextObject().getValue(); this.targetType = state.getActiveContextObject().getTypeDescriptor(); this.arguments = arguments; }
@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()); } }
/** * 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()); }
/** * Go through the list of registered constructor resolvers and see if any can find a * constructor that takes the specified set of arguments. * @param typeName the type trying to be constructed * @param argumentTypes the types of the arguments supplied that the constructor must take * @param state the current state of the expression * @return a reusable ConstructorExecutor that can be invoked to run the constructor or null * @throws SpelEvaluationException if there is a problem locating the constructor */ private ConstructorExecutor findExecutorForConstructor(String typeName, List<TypeDescriptor> argumentTypes, ExpressionState state) throws SpelEvaluationException { EvaluationContext evalContext = state.getEvaluationContext(); List<ConstructorResolver> ctorResolvers = evalContext.getConstructorResolvers(); for (ConstructorResolver ctorResolver : ctorResolvers) { try { ConstructorExecutor ce = ctorResolver.resolve(state.getEvaluationContext(), typeName, argumentTypes); if (ce != null) { return ce; } } catch (AccessException ex) { throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typeName, FormatHelper.formatMethodForMessage("", argumentTypes)); } } throw new SpelEvaluationException(getStartPosition(), SpelMessage.CONSTRUCTOR_NOT_FOUND, typeName, FormatHelper.formatMethodForMessage("", argumentTypes)); }
@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; }
@Override public TypedValue getValueInternal(ExpressionState state) throws EvaluationException { EvaluationContext evaluationContext = state.getEvaluationContext(); Object value = state.getActiveContextObject().getValue(); TypeDescriptor targetType = state.getActiveContextObject().getTypeDescriptor(); Object[] arguments = getArguments(state); TypedValue result = getValueInternal(evaluationContext, value, targetType, arguments); updateExitTypeDescriptor(); return result; }
@Test public void testConstruction() { EvaluationContext context = TestScenarioCreator.getTestEvaluationContext(); ExpressionState state = new ExpressionState(context); assertEquals(context, state.getEvaluationContext()); }
@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)); }
@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)); }
checkCompile(expressionState); return ExpressionUtils.convertTypedValue( expressionState.getEvaluationContext(), typedResultValue, expectedResultType);
@Test public void testComparator() { ExpressionState state = getState(); assertEquals(state.getEvaluationContext().getTypeComparator(), state.getTypeComparator()); }
@Test public void testPropertyAccessors() { ExpressionState state = getState(); assertEquals(state.getEvaluationContext().getPropertyAccessors(), state.getPropertyAccessors()); }
checkCompile(expressionState); return ExpressionUtils.convertTypedValue( expressionState.getEvaluationContext(), typedResultValue, expectedResultType);
@Test public void testRootContextObject() { ExpressionState state = getState(); assertEquals(Inventor.class, state.getRootContextObject().getValue().getClass()); // although the root object is being set on the evaluation context, the value in the 'state' remains what it was when constructed ((StandardEvaluationContext) state.getEvaluationContext()).setRootObject(null); assertEquals(Inventor.class, state.getRootContextObject().getValue().getClass()); // assertEquals(null, state.getRootContextObject().getValue()); state = new ExpressionState(new StandardEvaluationContext()); assertEquals(TypedValue.NULL, state.getRootContextObject()); ((StandardEvaluationContext) state.getEvaluationContext()).setRootObject(null); assertEquals(null, state.getRootContextObject().getValue()); }
@Test public void testTypeLocator() throws EvaluationException { ExpressionState state = getState(); assertNotNull(state.getEvaluationContext().getTypeLocator()); assertEquals(Integer.class, state.findType("java.lang.Integer")); try { state.findType("someMadeUpName"); fail("Should have failed to find it"); } catch (EvaluationException ee) { SpelEvaluationException sEx = (SpelEvaluationException)ee; assertEquals(SpelMessage.TYPE_NOT_FOUND, sEx.getMessageCode()); } }