@Override public String toString() { return "TypedValue: '" + this.value + "' of [" + getTypeDescriptor() + "]"; }
@Override public boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof TypedValue)) { return false; } TypedValue otherTv = (TypedValue) other; // Avoid TypeDescriptor initialization if not necessary return (ObjectUtils.nullSafeEquals(this.value, otherTv.value) && ((this.typeDescriptor == null && otherTv.typeDescriptor == null) || ObjectUtils.nullSafeEquals(getTypeDescriptor(), otherTv.getTypeDescriptor()))); }
@Override @Nullable public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); ExpressionState expressionState = new ExpressionState(context, this.configuration); return this.ast.getValueInternal(expressionState).getTypeDescriptor(); }
@Override @Nullable public Class<?> getValueType(EvaluationContext context) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); ExpressionState expressionState = new ExpressionState(context, this.configuration); TypeDescriptor typeDescriptor = this.ast.getValueInternal(expressionState).getTypeDescriptor(); return (typeDescriptor != null ? typeDescriptor.getType() : null); }
@Override @Nullable public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, Object rootObject) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); ExpressionState expressionState = new ExpressionState(context, toTypedValue(rootObject), this.configuration); return this.ast.getValueInternal(expressionState).getTypeDescriptor(); }
@SuppressWarnings("unchecked") private static <T> T convertValue(TypeConverter typeConverter, TypedValue typedValue, Class<T> targetType) { Object result = typeConverter.convertValue(typedValue.getValue(), typedValue.getTypeDescriptor(), TypeDescriptor.valueOf(targetType)); if (result == null) { throw new IllegalStateException("Null conversion result for value [" + typedValue.getValue() + "]"); } return (T) result; }
@Override @Nullable public TypeDescriptor getValueTypeDescriptor(Object rootObject) throws EvaluationException { ExpressionState expressionState = new ExpressionState(getEvaluationContext(), toTypedValue(rootObject), this.configuration); return this.ast.getValueInternal(expressionState).getTypeDescriptor(); }
@Override @Nullable public Class<?> getValueType(EvaluationContext context, Object rootObject) throws EvaluationException { ExpressionState expressionState = new ExpressionState(context, toTypedValue(rootObject), this.configuration); TypeDescriptor typeDescriptor = this.ast.getValueInternal(expressionState).getTypeDescriptor(); return (typeDescriptor != null ? typeDescriptor.getType() : null); }
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 { 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 test_binaryPlusWithTime_ToString() { ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext()); Time time = new Time(new Date().getTime()); VariableReference var = new VariableReference("timeVar", -1, -1); var.setValue(expressionState, time); StringLiteral n2 = new StringLiteral("\" is now\"", -1, -1, "\" is now\""); OpPlus o = new OpPlus(-1, -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()); }
@Test public void projectionWithArray() throws Exception { Expression expression = new SpelExpressionParser().parseRaw("#testArray.![wrapper.value]"); EvaluationContext context = new StandardEvaluationContext(); context.setVariable("testArray", IntegerTestBean.createArray()); Object value = expression.getValue(context); assertTrue(value.getClass().isArray()); TypedValue typedValue = new TypedValue(value); assertEquals(Number.class, typedValue.getTypeDescriptor().getElementTypeDescriptor().getType()); Number[] array = (Number[]) value; assertEquals(3, array.length); assertEquals(new Integer(5), array[0]); assertEquals(5.9f, array[1]); assertEquals(new Integer(7), array[2]); }
@Test public void test_binaryPlusWithLeftStringOperand() { ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext()); StringLiteral n1 = new StringLiteral("\"number is \"", -1, -1, "\"number is \""); LongLiteral n2 = new LongLiteral("123", -1, -1, 123); OpPlus o = new OpPlus(-1, -1, n1, n2); TypedValue value = o.getValueInternal(expressionState); assertEquals(String.class, value.getTypeDescriptor().getObjectType()); assertEquals(String.class, value.getTypeDescriptor().getType()); assertEquals("number is 123", value.getValue()); }
@Test public void SPR10162_onlyBridgeMethod() throws Exception { ReflectivePropertyAccessor accessor = new ReflectivePropertyAccessor(); StandardEvaluationContext context = new StandardEvaluationContext(); Object target = new OnlyBridgeMethod(); TypedValue value = accessor.read(context, target, "property"); assertEquals(Integer.class, value.getTypeDescriptor().getType()); }
@Test public void test_binaryPlusWithStringOperands() { ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext()); StringLiteral n1 = new StringLiteral("\"foo\"", -1, -1, "\"foo\""); StringLiteral n2 = new StringLiteral("\"bar\"", -1, -1, "\"bar\""); OpPlus o = new OpPlus(-1, -1, n1, n2); TypedValue value = o.getValueInternal(expressionState); assertEquals(String.class, value.getTypeDescriptor().getObjectType()); assertEquals(String.class, value.getTypeDescriptor().getType()); assertEquals("foobar", value.getValue()); }
@Test public void test_binaryPlusWithRightStringOperand() { ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext()); LongLiteral n1 = new LongLiteral("123", -1, -1, 123); StringLiteral n2 = new StringLiteral("\" is a number\"", -1, -1, "\" is a number\""); OpPlus o = new OpPlus(-1, -1, n1, n2); TypedValue value = o.getValueInternal(expressionState); assertEquals(String.class, value.getTypeDescriptor().getObjectType()); assertEquals(String.class, value.getTypeDescriptor().getType()); assertEquals("123 is a number", value.getValue()); }
@Test public void SPR9994_bridgeMethods() throws Exception { ReflectivePropertyAccessor accessor = new ReflectivePropertyAccessor(); StandardEvaluationContext context = new StandardEvaluationContext(); Object target = new GenericImplementation(); TypedValue value = accessor.read(context, target, "property"); assertEquals(Integer.class, value.getTypeDescriptor().getType()); }
@Test public void propertyAccessWithInstanceMethodResolverAndTypedRootObject() { Person target = new Person("p1"); EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding(). withInstanceMethods().withTypedRootObject(target, TypeDescriptor.valueOf(Object.class)).build(); assertEquals("1", parser.parseExpression("name.substring(1)").getValue(context, target)); assertSame(target, context.getRootObject().getValue()); assertSame(Object.class, context.getRootObject().getTypeDescriptor().getType()); }
@Test public void testVariables() { ExpressionState state = getState(); TypedValue typedValue = state.lookupVariable("foo"); assertEquals(TypedValue.NULL, typedValue); state.setVariable("foo",34); typedValue = state.lookupVariable("foo"); assertEquals(34, typedValue.getValue()); assertEquals(Integer.class, typedValue.getTypeDescriptor().getType()); state.setVariable("foo","abc"); typedValue = state.lookupVariable("foo"); assertEquals("abc", typedValue.getValue()); assertEquals(String.class, typedValue.getTypeDescriptor().getType()); }
@Test public void testRootObjectConstructor() { EvaluationContext ctx = getContext(); // TypedValue root = ctx.getRootObject(); // supplied should override root on context ExpressionState state = new ExpressionState(ctx,new TypedValue("i am a string")); TypedValue stateRoot = state.getRootContextObject(); assertEquals(String.class, stateRoot.getTypeDescriptor().getType()); assertEquals("i am a string", stateRoot.getValue()); }