Here are the examples of the java api org.springframework.expression.TypedValue.getValue() taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
59 Examples
19
View Source File : ExpressionStateTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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();
replacedertEquals(String.clreplaced, stateRoot.getTypeDescriptor().getType());
replacedertEquals("i am a string", stateRoot.getValue());
}
19
View Source File : ExpressionStateTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void testVariables() {
ExpressionState state = getState();
TypedValue typedValue = state.lookupVariable("foo");
replacedertEquals(TypedValue.NULL, typedValue);
state.setVariable("foo", 34);
typedValue = state.lookupVariable("foo");
replacedertEquals(34, typedValue.getValue());
replacedertEquals(Integer.clreplaced, typedValue.getTypeDescriptor().getType());
state.setVariable("foo", "abc");
typedValue = state.lookupVariable("foo");
replacedertEquals("abc", typedValue.getValue());
replacedertEquals(String.clreplaced, typedValue.getTypeDescriptor().getType());
}
19
View Source File : OpInc.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
SpelNodeImpl operand = getLeftOperand();
ValueRef valueRef = operand.getValueRef(state);
TypedValue typedValue = valueRef.getValue();
Object value = typedValue.getValue();
TypedValue returnValue = typedValue;
TypedValue newValue = null;
if (value instanceof Number) {
Number op1 = (Number) value;
if (op1 instanceof BigDecimal) {
newValue = new TypedValue(((BigDecimal) op1).add(BigDecimal.ONE), typedValue.getTypeDescriptor());
} else if (op1 instanceof Double) {
newValue = new TypedValue(op1.doubleValue() + 1.0d, typedValue.getTypeDescriptor());
} else if (op1 instanceof Float) {
newValue = new TypedValue(op1.floatValue() + 1.0f, typedValue.getTypeDescriptor());
} else if (op1 instanceof BigInteger) {
newValue = new TypedValue(((BigInteger) op1).add(BigInteger.ONE), typedValue.getTypeDescriptor());
} else if (op1 instanceof Long) {
newValue = new TypedValue(op1.longValue() + 1L, typedValue.getTypeDescriptor());
} else if (op1 instanceof Integer) {
newValue = new TypedValue(op1.intValue() + 1, typedValue.getTypeDescriptor());
} else if (op1 instanceof Short) {
newValue = new TypedValue(op1.shortValue() + (short) 1, typedValue.getTypeDescriptor());
} else if (op1 instanceof Byte) {
newValue = new TypedValue(op1.byteValue() + (byte) 1, typedValue.getTypeDescriptor());
} else {
// Unknown Number subtype -> best guess is double increment
newValue = new TypedValue(op1.doubleValue() + 1.0d, typedValue.getTypeDescriptor());
}
}
if (newValue == null) {
try {
newValue = state.operate(Operation.ADD, returnValue.getValue(), 1);
} catch (SpelEvaluationException ex) {
if (ex.getMessageCode() == SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES) {
// This means the operand is not incrementable
throw new SpelEvaluationException(operand.getStartPosition(), SpelMessage.OPERAND_NOT_INCREMENTABLE, operand.toStringAST());
}
throw ex;
}
}
// set the name value
try {
valueRef.setValue(newValue.getValue());
} catch (SpelEvaluationException see) {
// If unable to set the value the operand is not writable (e.g. 1++ )
if (see.getMessageCode() == SpelMessage.SETVALUE_NOT_SUPPORTED) {
throw new SpelEvaluationException(operand.getStartPosition(), SpelMessage.OPERAND_NOT_INCREMENTABLE);
} else {
throw see;
}
}
if (!this.postfix) {
// The return value is the new value, not the original value
returnValue = newValue;
}
return returnValue;
}
19
View Source File : OpDec.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
SpelNodeImpl operand = getLeftOperand();
// The operand is going to be read and then replacedigned to, we don't want to evaluate it twice.
ValueRef lvalue = operand.getValueRef(state);
// operand.getValueInternal(state);
TypedValue operandTypedValue = lvalue.getValue();
Object operandValue = operandTypedValue.getValue();
TypedValue returnValue = operandTypedValue;
TypedValue newValue = null;
if (operandValue instanceof Number) {
Number op1 = (Number) operandValue;
if (op1 instanceof BigDecimal) {
newValue = new TypedValue(((BigDecimal) op1).subtract(BigDecimal.ONE), operandTypedValue.getTypeDescriptor());
} else if (op1 instanceof Double) {
newValue = new TypedValue(op1.doubleValue() - 1.0d, operandTypedValue.getTypeDescriptor());
} else if (op1 instanceof Float) {
newValue = new TypedValue(op1.floatValue() - 1.0f, operandTypedValue.getTypeDescriptor());
} else if (op1 instanceof BigInteger) {
newValue = new TypedValue(((BigInteger) op1).subtract(BigInteger.ONE), operandTypedValue.getTypeDescriptor());
} else if (op1 instanceof Long) {
newValue = new TypedValue(op1.longValue() - 1L, operandTypedValue.getTypeDescriptor());
} else if (op1 instanceof Integer) {
newValue = new TypedValue(op1.intValue() - 1, operandTypedValue.getTypeDescriptor());
} else if (op1 instanceof Short) {
newValue = new TypedValue(op1.shortValue() - (short) 1, operandTypedValue.getTypeDescriptor());
} else if (op1 instanceof Byte) {
newValue = new TypedValue(op1.byteValue() - (byte) 1, operandTypedValue.getTypeDescriptor());
} else {
// Unknown Number subtype -> best guess is double decrement
newValue = new TypedValue(op1.doubleValue() - 1.0d, operandTypedValue.getTypeDescriptor());
}
}
if (newValue == null) {
try {
newValue = state.operate(Operation.SUBTRACT, returnValue.getValue(), 1);
} catch (SpelEvaluationException ex) {
if (ex.getMessageCode() == SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES) {
// This means the operand is not decrementable
throw new SpelEvaluationException(operand.getStartPosition(), SpelMessage.OPERAND_NOT_DECREMENTABLE, operand.toStringAST());
} else {
throw ex;
}
}
}
// set the new value
try {
lvalue.setValue(newValue.getValue());
} catch (SpelEvaluationException see) {
// if unable to set the value the operand is not writable (e.g. 1-- )
if (see.getMessageCode() == SpelMessage.SETVALUE_NOT_SUPPORTED) {
throw new SpelEvaluationException(operand.getStartPosition(), SpelMessage.OPERAND_NOT_DECREMENTABLE);
} else {
throw see;
}
}
if (!this.postfix) {
// the return value is the new value, not the original value
returnValue = newValue;
}
return returnValue;
}
19
View Source File : Assign.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
TypedValue newValue = this.children[1].getValueInternal(state);
getChild(0).setValue(state, newValue.getValue());
return newValue;
}
19
View Source File : SmartEnvironmentAccessorUnitTests.java
License : Apache License 2.0
Project Creator : spring-projects
License : Apache License 2.0
Project Creator : spring-projects
@Test
public void readFromNullTargetObjectIsNullSafeReturnsNull() {
EvaluationContext mockEvaluationContext = mock(EvaluationContext.clreplaced);
TypedValue typedValue = new SmartEnvironmentAccessor().read(mockEvaluationContext, null, "key");
replacedertThat(typedValue).isNotNull();
replacedertThat(typedValue.getValue()).isNull();
verifyNoInteractions(mockEvaluationContext);
}
19
View Source File : SmartEnvironmentAccessorUnitTests.java
License : Apache License 2.0
Project Creator : spring-projects
License : Apache License 2.0
Project Creator : spring-projects
@Test
public void readFromNonEnvironmentReturnsNull() {
EvaluationContext mockEvaluationContext = mock(EvaluationContext.clreplaced);
TypedValue typedValue = new SmartEnvironmentAccessor().read(mockEvaluationContext, new Object(), "key");
replacedertThat(typedValue).isNotNull();
replacedertThat(typedValue.getValue()).isNull();
verifyNoInteractions(mockEvaluationContext);
}
18
View Source File : OpPlusTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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);
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals("123 is a number", value.getValue());
}
18
View Source File : OpPlusTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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);
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals("number is 123", value.getValue());
}
18
View Source File : OpPlusTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void test_binaryPlusWithNumberOperands() {
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
{
RealLiteral n1 = new RealLiteral("123.00", -1, -1, 123.0);
RealLiteral n2 = new RealLiteral("456.00", -1, -1, 456.0);
OpPlus o = new OpPlus(-1, -1, n1, n2);
TypedValue value = o.getValueInternal(expressionState);
replacedertEquals(Double.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(Double.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals(Double.valueOf(123.0 + 456.0), value.getValue());
}
{
LongLiteral n1 = new LongLiteral("123", -1, -1, 123L);
LongLiteral n2 = new LongLiteral("456", -1, -1, 456L);
OpPlus o = new OpPlus(-1, -1, n1, n2);
TypedValue value = o.getValueInternal(expressionState);
replacedertEquals(Long.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(Long.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals(Long.valueOf(123L + 456L), value.getValue());
}
{
IntLiteral n1 = new IntLiteral("123", -1, -1, 123);
IntLiteral n2 = new IntLiteral("456", -1, -1, 456);
OpPlus o = new OpPlus(-1, -1, n1, n2);
TypedValue value = o.getValueInternal(expressionState);
replacedertEquals(Integer.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(Integer.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals(Integer.valueOf(123 + 456), value.getValue());
}
}
18
View Source File : OpPlusTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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);
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals("foobar", value.getValue());
}
18
View Source File : OpPlusTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void test_unaryPlusWithNumberOperand() {
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
{
RealLiteral realLiteral = new RealLiteral("123.00", -1, -1, 123.0);
OpPlus o = new OpPlus(-1, -1, realLiteral);
TypedValue value = o.getValueInternal(expressionState);
replacedertEquals(Double.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(Double.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals(realLiteral.getLiteralValue().getValue(), value.getValue());
}
{
IntLiteral intLiteral = new IntLiteral("123", -1, -1, 123);
OpPlus o = new OpPlus(-1, -1, intLiteral);
TypedValue value = o.getValueInternal(expressionState);
replacedertEquals(Integer.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(Integer.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals(intLiteral.getLiteralValue().getValue(), value.getValue());
}
{
LongLiteral longLiteral = new LongLiteral("123", -1, -1, 123L);
OpPlus o = new OpPlus(-1, -1, longLiteral);
TypedValue value = o.getValueInternal(expressionState);
replacedertEquals(Long.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(Long.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals(longLiteral.getLiteralValue().getValue(), value.getValue());
}
}
18
View Source File : ExpressionState.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Nullable
public Object convertValue(TypedValue value, TypeDescriptor targetTypeDescriptor) throws EvaluationException {
Object val = value.getValue();
return this.relatedContext.getTypeConverter().convertValue(val, TypeDescriptor.forObject(val), targetTypeDescriptor);
}
18
View Source File : OperatorInstanceof.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
/**
* Compare the left operand to see it is an instance of the type specified as the
* right operand. The right operand must be a clreplaced.
* @param state the expression state
* @return {@code true} if the left operand is an instanceof of the right operand,
* otherwise {@code false}
* @throws EvaluationException if there is a problem evaluating the expression
*/
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
SpelNodeImpl rightOperand = getRightOperand();
TypedValue left = getLeftOperand().getValueInternal(state);
TypedValue right = rightOperand.getValueInternal(state);
Object leftValue = left.getValue();
Object rightValue = right.getValue();
BooleanTypedValue result;
if (rightValue == null || !(rightValue instanceof Clreplaced)) {
throw new SpelEvaluationException(getRightOperand().getStartPosition(), SpelMessage.INSTANCEOF_OPERATOR_NEEDS_CLreplaced_OPERAND, (rightValue == null ? "null" : rightValue.getClreplaced().getName()));
}
Clreplaced<?> rightClreplaced = (Clreplaced<?>) rightValue;
if (leftValue == null) {
// null is not an instanceof anything
result = BooleanTypedValue.FALSE;
} else {
result = BooleanTypedValue.forValue(rightClreplaced.isreplacedignableFrom(leftValue.getClreplaced()));
}
this.type = rightClreplaced;
if (rightOperand instanceof TypeReference) {
// Can only generate bytecode where the right operand is a direct type reference,
// not if it is indirect (for example when right operand is a variable reference)
this.exitTypeDescriptor = "Z";
}
return result;
}
18
View Source File : ExpressionUtils.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@SuppressWarnings("unchecked")
private static <T> T convertValue(TypeConverter typeConverter, TypedValue typedValue, Clreplaced<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;
}
18
View Source File : SmartEnvironmentAccessorUnitTests.java
License : Apache License 2.0
Project Creator : spring-projects
License : Apache License 2.0
Project Creator : spring-projects
@Test
public void readFromEnvironmentContainingPropertyReturnsValue() {
Environment mockEnvironment = mock(Environment.clreplaced);
EvaluationContext mockEvaluationContext = mock(EvaluationContext.clreplaced);
doReturn("test").when(mockEnvironment).getProperty(eq("key"));
TypedValue typedValue = new SmartEnvironmentAccessor().read(mockEvaluationContext, mockEnvironment, "key");
replacedertThat(typedValue).isNotNull();
replacedertThat(typedValue.getValue()).isEqualTo("test");
verify(mockEnvironment, times(1)).getProperty(eq("key"));
verifyNoMoreInteractions(mockEnvironment);
verifyNoInteractions(mockEvaluationContext);
}
18
View Source File : SmartEnvironmentAccessorUnitTests.java
License : Apache License 2.0
Project Creator : spring-projects
License : Apache License 2.0
Project Creator : spring-projects
@Test
public void readFromEnvironmentCapableObjectContainingPropertyReturnsValue() {
Environment mockEnvironment = mock(Environment.clreplaced);
EnvironmentCapable mockEnvironmentCapable = mock(EnvironmentCapable.clreplaced);
EvaluationContext mockEvaluationContext = mock(EvaluationContext.clreplaced);
doReturn(mockEnvironment).when(mockEnvironmentCapable).getEnvironment();
doReturn("test").when(mockEnvironment).getProperty(eq("key"));
TypedValue typedValue = new SmartEnvironmentAccessor().read(mockEvaluationContext, mockEnvironmentCapable, "key");
replacedertThat(typedValue).isNotNull();
replacedertThat(typedValue.getValue()).isEqualTo("test");
verify(mockEnvironmentCapable, times(1)).getEnvironment();
verify(mockEnvironment, times(1)).getProperty(eq("key"));
verifyNoMoreInteractions(mockEnvironment, mockEnvironmentCapable);
verifyNoInteractions(mockEvaluationContext);
}
18
View Source File : SmartEnvironmentAccessorUnitTests.java
License : Apache License 2.0
Project Creator : spring-projects
License : Apache License 2.0
Project Creator : spring-projects
@Test
public void readFromEnvironmentNotContainingPropertyReturnsNull() {
Environment mockEnvironment = mock(Environment.clreplaced);
EvaluationContext mockEvaluationContext = mock(EvaluationContext.clreplaced);
doReturn(null).when(mockEnvironment).getProperty(any());
TypedValue typedValue = new SmartEnvironmentAccessor().read(mockEvaluationContext, mockEnvironment, "key");
replacedertThat(typedValue).isNotNull();
replacedertThat(typedValue.getValue()).isNull();
verify(mockEnvironment, times(1)).getProperty(eq("key"));
verifyNoMoreInteractions(mockEnvironment);
verifyNoInteractions(mockEvaluationContext);
}
18
View Source File : ExpressionStateTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void testVariables() {
ExpressionState state = getState();
TypedValue typedValue = state.lookupVariable("foo");
replacedertThat(typedValue).isEqualTo(TypedValue.NULL);
state.setVariable("foo", 34);
typedValue = state.lookupVariable("foo");
replacedertThat(typedValue.getValue()).isEqualTo(34);
replacedertThat(typedValue.getTypeDescriptor().getType()).isEqualTo(Integer.clreplaced);
state.setVariable("foo", "abc");
typedValue = state.lookupVariable("foo");
replacedertThat(typedValue.getValue()).isEqualTo("abc");
replacedertThat(typedValue.getTypeDescriptor().getType()).isEqualTo(String.clreplaced);
}
18
View Source File : ExpressionStateTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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();
replacedertThat(stateRoot.getTypeDescriptor().getType()).isEqualTo(String.clreplaced);
replacedertThat(stateRoot.getValue()).isEqualTo("i am a string");
}
18
View Source File : OpPlusTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void test_binaryPlusWithNumberOperands() {
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
{
RealLiteral n1 = new RealLiteral("123.00", -1, -1, 123.0);
RealLiteral n2 = new RealLiteral("456.00", -1, -1, 456.0);
OpPlus o = new OpPlus(-1, -1, n1, n2);
TypedValue value = o.getValueInternal(expressionState);
replacedertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(Double.clreplaced);
replacedertThat(value.getTypeDescriptor().getType()).isEqualTo(Double.clreplaced);
replacedertThat(value.getValue()).isEqualTo(Double.valueOf(123.0 + 456.0));
}
{
LongLiteral n1 = new LongLiteral("123", -1, -1, 123L);
LongLiteral n2 = new LongLiteral("456", -1, -1, 456L);
OpPlus o = new OpPlus(-1, -1, n1, n2);
TypedValue value = o.getValueInternal(expressionState);
replacedertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(Long.clreplaced);
replacedertThat(value.getTypeDescriptor().getType()).isEqualTo(Long.clreplaced);
replacedertThat(value.getValue()).isEqualTo(Long.valueOf(123L + 456L));
}
{
IntLiteral n1 = new IntLiteral("123", -1, -1, 123);
IntLiteral n2 = new IntLiteral("456", -1, -1, 456);
OpPlus o = new OpPlus(-1, -1, n1, n2);
TypedValue value = o.getValueInternal(expressionState);
replacedertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(Integer.clreplaced);
replacedertThat(value.getTypeDescriptor().getType()).isEqualTo(Integer.clreplaced);
replacedertThat(value.getValue()).isEqualTo(Integer.valueOf(123 + 456));
}
}
18
View Source File : OpPlusTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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);
replacedertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.clreplaced);
replacedertThat(value.getTypeDescriptor().getType()).isEqualTo(String.clreplaced);
replacedertThat(value.getValue()).isEqualTo("foobar");
}
18
View Source File : OpPlusTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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);
replacedertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.clreplaced);
replacedertThat(value.getTypeDescriptor().getType()).isEqualTo(String.clreplaced);
replacedertThat(value.getValue()).isEqualTo("number is 123");
}
18
View Source File : OpPlusTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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);
replacedertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.clreplaced);
replacedertThat(value.getTypeDescriptor().getType()).isEqualTo(String.clreplaced);
replacedertThat(value.getValue()).isEqualTo("123 is a number");
}
18
View Source File : OpPlusTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void test_unaryPlusWithNumberOperand() {
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
{
RealLiteral realLiteral = new RealLiteral("123.00", -1, -1, 123.0);
OpPlus o = new OpPlus(-1, -1, realLiteral);
TypedValue value = o.getValueInternal(expressionState);
replacedertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(Double.clreplaced);
replacedertThat(value.getTypeDescriptor().getType()).isEqualTo(Double.clreplaced);
replacedertThat(value.getValue()).isEqualTo(realLiteral.getLiteralValue().getValue());
}
{
IntLiteral intLiteral = new IntLiteral("123", -1, -1, 123);
OpPlus o = new OpPlus(-1, -1, intLiteral);
TypedValue value = o.getValueInternal(expressionState);
replacedertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(Integer.clreplaced);
replacedertThat(value.getTypeDescriptor().getType()).isEqualTo(Integer.clreplaced);
replacedertThat(value.getValue()).isEqualTo(intLiteral.getLiteralValue().getValue());
}
{
LongLiteral longLiteral = new LongLiteral("123", -1, -1, 123L);
OpPlus o = new OpPlus(-1, -1, longLiteral);
TypedValue value = o.getValueInternal(expressionState);
replacedertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(Long.clreplaced);
replacedertThat(value.getTypeDescriptor().getType()).isEqualTo(Long.clreplaced);
replacedertThat(value.getValue()).isEqualTo(longLiteral.getLiteralValue().getValue());
}
}
18
View Source File : OpPlusTests.java
License : MIT License
Project Creator : mindcarver
License : MIT License
Project Creator : mindcarver
@Test
public void test_binaryPlusWithLeftStringOperand() {
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
StringLiteral n1 = new StringLiteral("\"number is \"", -1, "\"number is \"");
LongLiteral n2 = new LongLiteral("123", -1, 123);
OpPlus o = new OpPlus(-1, n1, n2);
TypedValue value = o.getValueInternal(expressionState);
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals("number is 123", value.getValue());
}
18
View Source File : OpPlusTests.java
License : MIT License
Project Creator : mindcarver
License : MIT License
Project Creator : mindcarver
@Test
public void test_binaryPlusWithRightStringOperand() {
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
LongLiteral n1 = new LongLiteral("123", -1, 123);
StringLiteral n2 = new StringLiteral("\" is a number\"", -1, "\" is a number\"");
OpPlus o = new OpPlus(-1, n1, n2);
TypedValue value = o.getValueInternal(expressionState);
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals("123 is a number", value.getValue());
}
18
View Source File : OpPlusTests.java
License : MIT License
Project Creator : mindcarver
License : MIT License
Project Creator : mindcarver
@Test
public void test_binaryPlusWithNumberOperands() {
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
{
RealLiteral n1 = new RealLiteral("123.00", -1, 123.0);
RealLiteral n2 = new RealLiteral("456.00", -1, 456.0);
OpPlus o = new OpPlus(-1, n1, n2);
TypedValue value = o.getValueInternal(expressionState);
replacedertEquals(Double.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(Double.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals(Double.valueOf(123.0 + 456.0), value.getValue());
}
{
LongLiteral n1 = new LongLiteral("123", -1, 123L);
LongLiteral n2 = new LongLiteral("456", -1, 456L);
OpPlus o = new OpPlus(-1, n1, n2);
TypedValue value = o.getValueInternal(expressionState);
replacedertEquals(Long.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(Long.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals(Long.valueOf(123L + 456L), value.getValue());
}
{
IntLiteral n1 = new IntLiteral("123", -1, 123);
IntLiteral n2 = new IntLiteral("456", -1, 456);
OpPlus o = new OpPlus(-1, n1, n2);
TypedValue value = o.getValueInternal(expressionState);
replacedertEquals(Integer.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(Integer.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals(Integer.valueOf(123 + 456), value.getValue());
}
}
18
View Source File : OpPlusTests.java
License : MIT License
Project Creator : mindcarver
License : MIT License
Project Creator : mindcarver
@Test
public void test_binaryPlusWithStringOperands() {
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
StringLiteral n1 = new StringLiteral("\"foo\"", -1, "\"foo\"");
StringLiteral n2 = new StringLiteral("\"bar\"", -1, "\"bar\"");
OpPlus o = new OpPlus(-1, n1, n2);
TypedValue value = o.getValueInternal(expressionState);
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals("foobar", value.getValue());
}
18
View Source File : OpPlusTests.java
License : MIT License
Project Creator : mindcarver
License : MIT License
Project Creator : mindcarver
@Test
public void test_unaryPlusWithNumberOperand() {
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
{
RealLiteral realLiteral = new RealLiteral("123.00", -1, 123.0);
OpPlus o = new OpPlus(-1, realLiteral);
TypedValue value = o.getValueInternal(expressionState);
replacedertEquals(Double.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(Double.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals(realLiteral.getLiteralValue().getValue(), value.getValue());
}
{
IntLiteral intLiteral = new IntLiteral("123", -1, 123);
OpPlus o = new OpPlus(-1, intLiteral);
TypedValue value = o.getValueInternal(expressionState);
replacedertEquals(Integer.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(Integer.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals(intLiteral.getLiteralValue().getValue(), value.getValue());
}
{
LongLiteral longLiteral = new LongLiteral("123", -1, 123L);
OpPlus o = new OpPlus(-1, longLiteral);
TypedValue value = o.getValueInternal(expressionState);
replacedertEquals(Long.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(Long.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals(longLiteral.getLiteralValue().getValue(), value.getValue());
}
}
18
View Source File : ExpressionState.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
public Object convertValue(TypedValue value, TypeDescriptor targetTypeDescriptor) throws EvaluationException {
Object val = value.getValue();
return this.relatedContext.getTypeConverter().convertValue(val, TypeDescriptor.forObject(val), targetTypeDescriptor);
}
18
View Source File : OperatorInstanceof.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
/**
* Compare the left operand to see it is an instance of the type specified as the
* right operand. The right operand must be a clreplaced.
* @param state the expression state
* @return true if the left operand is an instanceof of the right operand, otherwise
* false
* @throws EvaluationException if there is a problem evaluating the expression
*/
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
TypedValue left = getLeftOperand().getValueInternal(state);
TypedValue right = getRightOperand().getValueInternal(state);
Object leftValue = left.getValue();
Object rightValue = right.getValue();
BooleanTypedValue result = null;
if (rightValue == null || !(rightValue instanceof Clreplaced<?>)) {
throw new SpelEvaluationException(getRightOperand().getStartPosition(), SpelMessage.INSTANCEOF_OPERATOR_NEEDS_CLreplaced_OPERAND, (rightValue == null ? "null" : rightValue.getClreplaced().getName()));
}
Clreplaced<?> rightClreplaced = (Clreplaced<?>) rightValue;
if (leftValue == null) {
// null is not an instanceof anything
result = BooleanTypedValue.FALSE;
} else {
result = BooleanTypedValue.forValue(rightClreplaced.isreplacedignableFrom(leftValue.getClreplaced()));
}
this.type = rightClreplaced;
this.exitTypeDescriptor = "Z";
return result;
}
17
View Source File : OpPlusTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@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);
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals(time + " is now", value.getValue());
}
17
View Source File : ExpressionUtils.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
/**
* Determines if there is a type converter available in the specified context and
* attempts to use it to convert the supplied value to the specified type. Throws an
* exception if conversion is not possible.
* @param context the evaluation context that may define a type converter
* @param typedValue the value to convert and a type descriptor describing it
* @param targetType the type to attempt conversion to
* @return the converted value
* @throws EvaluationException if there is a problem during conversion or conversion
* of the value to the specified type is not supported
*/
@SuppressWarnings("unchecked")
@Nullable
public static <T> T convertTypedValue(@Nullable EvaluationContext context, TypedValue typedValue, @Nullable Clreplaced<T> targetType) {
Object value = typedValue.getValue();
if (targetType == null) {
return (T) value;
}
if (context != null) {
return (T) context.getTypeConverter().convertValue(value, typedValue.getTypeDescriptor(), TypeDescriptor.valueOf(targetType));
}
if (ClreplacedUtils.isreplacedignableValue(targetType, value)) {
return (T) value;
}
throw new EvaluationException("Cannot convert value '" + value + "' to type '" + targetType.getName() + "'");
}
17
View Source File : OpPlusTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@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);
replacedertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.clreplaced);
replacedertThat(value.getTypeDescriptor().getType()).isEqualTo(String.clreplaced);
replacedertThat(value.getValue()).isEqualTo((time + " is now"));
}
17
View Source File : OpPlusTests.java
License : MIT License
Project Creator : mindcarver
License : MIT License
Project Creator : mindcarver
@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);
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals(time + " is now", value.getValue());
}
17
View Source File : ExpressionUtils.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
/**
* Determines if there is a type converter available in the specified context and
* attempts to use it to convert the supplied value to the specified type. Throws an
* exception if conversion is not possible.
* @param context the evaluation context that may define a type converter
* @param typedValue the value to convert and a type descriptor describing it
* @param targetType the type to attempt conversion to
* @return the converted value
* @throws EvaluationException if there is a problem during conversion or conversion
* of the value to the specified type is not supported
*/
@SuppressWarnings("unchecked")
public static <T> T convertTypedValue(EvaluationContext context, TypedValue typedValue, Clreplaced<T> targetType) {
Object value = typedValue.getValue();
if (targetType == null) {
return (T) value;
}
if (context != null) {
return (T) context.getTypeConverter().convertValue(value, typedValue.getTypeDescriptor(), TypeDescriptor.valueOf(targetType));
}
if (ClreplacedUtils.isreplacedignableValue(targetType, value)) {
return (T) value;
}
throw new EvaluationException("Cannot convert value '" + value + "' to type '" + targetType.getName() + "'");
}
16
View Source File : VariableReference.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Override
public TypedValue getValueInternal(ExpressionState state) throws SpelEvaluationException {
if (this.name.equals(THIS)) {
return state.getActiveContextObject();
}
if (this.name.equals(ROOT)) {
TypedValue result = state.getRootContextObject();
this.exitTypeDescriptor = CodeFlow.toDescriptorFromObject(result.getValue());
return result;
}
TypedValue result = state.lookupVariable(this.name);
Object value = result.getValue();
if (value == null || !Modifier.isPublic(value.getClreplaced().getModifiers())) {
// If the type is not public then when generateCode produces a checkcast to it
// then an IllegalAccessError will occur.
// If resorting to Object isn't sufficient, the hierarchy could be traversed for
// the first public type.
this.exitTypeDescriptor = "Ljava/lang/Object";
} else {
this.exitTypeDescriptor = CodeFlow.toDescriptorFromObject(value);
}
// a null value will mean either the value was null or the variable was not found
return result;
}
15
View Source File : OpPlusTests.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Test
public void test_binaryPlusWithTimeConverted() {
SimpleDateFormat format = new SimpleDateFormat("hh :--: mm :--: ss", Locale.ENGLISH);
GenericConversionService conversionService = new GenericConversionService();
conversionService.addConverter(Time.clreplaced, String.clreplaced, format::format);
StandardEvaluationContext evaluationContextConverter = new StandardEvaluationContext();
evaluationContextConverter.setTypeConverter(new StandardTypeConverter(conversionService));
ExpressionState expressionState = new ExpressionState(evaluationContextConverter);
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);
replacedertThat(value.getTypeDescriptor().getObjectType()).isEqualTo(String.clreplaced);
replacedertThat(value.getTypeDescriptor().getType()).isEqualTo(String.clreplaced);
replacedertThat(value.getValue()).isEqualTo((format.format(time) + " is now"));
}
13
View Source File : OpPlusTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void test_binaryPlusWithTimeConverted() {
SimpleDateFormat format = new SimpleDateFormat("hh :--: mm :--: ss", Locale.ENGLISH);
GenericConversionService conversionService = new GenericConversionService();
conversionService.addConverter(Time.clreplaced, String.clreplaced, format::format);
StandardEvaluationContext evaluationContextConverter = new StandardEvaluationContext();
evaluationContextConverter.setTypeConverter(new StandardTypeConverter(conversionService));
ExpressionState expressionState = new ExpressionState(evaluationContextConverter);
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);
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals(format.format(time) + " is now", value.getValue());
}
13
View Source File : PropertyOrFieldReference.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
public boolean isWritableProperty(String name, TypedValue contextObject, EvaluationContext evalContext) throws EvaluationException {
Object value = contextObject.getValue();
if (value != null) {
List<PropertyAccessor> accessorsToTry = getPropertyAccessorsToTry(contextObject.getValue(), evalContext.getPropertyAccessors());
for (PropertyAccessor accessor : accessorsToTry) {
try {
if (accessor.canWrite(evalContext, value, name)) {
return true;
}
} catch (AccessException ex) {
// let others try
}
}
}
return false;
}
13
View Source File : Projection.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Override
protected ValueRef getValueRef(ExpressionState state) throws EvaluationException {
TypedValue op = state.getActiveContextObject();
Object operand = op.getValue();
boolean operandIsArray = ObjectUtils.isArray(operand);
// TypeDescriptor operandTypeDescriptor = op.getTypeDescriptor();
// When the input is a map, we push a special context object on the stack
// before calling the specified operation. This special context object
// has two fields 'key' and 'value' that refer to the map entries key
// and value, and they can be referenced in the operation
// eg. {'a':'y','b':'n'}.![value=='y'?key:null]" == ['a', null]
if (operand instanceof Map) {
Map<?, ?> mapData = (Map<?, ?>) operand;
List<Object> result = new ArrayList<>();
for (Map.Entry<?, ?> entry : mapData.entrySet()) {
try {
state.pushActiveContextObject(new TypedValue(entry));
state.enterScope();
result.add(this.children[0].getValueInternal(state).getValue());
} finally {
state.popActiveContextObject();
state.exitScope();
}
}
// TODO unable to build correct type descriptor
return new ValueRef.TypedValueHolderValueRef(new TypedValue(result), this);
}
if (operand instanceof Iterable || operandIsArray) {
Iterable<?> data = (operand instanceof Iterable ? (Iterable<?>) operand : Arrays.asList(ObjectUtils.toObjectArray(operand)));
List<Object> result = new ArrayList<>();
int idx = 0;
Clreplaced<?> arrayElementType = null;
for (Object element : data) {
try {
state.pushActiveContextObject(new TypedValue(element));
state.enterScope("index", idx);
Object value = this.children[0].getValueInternal(state).getValue();
if (value != null && operandIsArray) {
arrayElementType = determineCommonType(arrayElementType, value.getClreplaced());
}
result.add(value);
} finally {
state.exitScope();
state.popActiveContextObject();
}
idx++;
}
if (operandIsArray) {
if (arrayElementType == null) {
arrayElementType = Object.clreplaced;
}
Object resultArray = Array.newInstance(arrayElementType, result.size());
System.arraycopy(result.toArray(), 0, resultArray, 0, result.size());
return new ValueRef.TypedValueHolderValueRef(new TypedValue(resultArray), this);
}
return new ValueRef.TypedValueHolderValueRef(new TypedValue(result), this);
}
if (operand == null) {
if (this.nullSafe) {
return ValueRef.NullValueRef.INSTANCE;
}
throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROJECTION_NOT_SUPPORTED_ON_TYPE, "null");
}
throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROJECTION_NOT_SUPPORTED_ON_TYPE, operand.getClreplaced().getName());
}
13
View Source File : OpPlus.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
SpelNodeImpl leftOp = getLeftOperand();
if (this.children.length < 2) {
// if only one operand, then this is unary plus
Object operandOne = leftOp.getValueInternal(state).getValue();
if (operandOne instanceof Number) {
if (operandOne instanceof Double) {
this.exitTypeDescriptor = "D";
} else if (operandOne instanceof Float) {
this.exitTypeDescriptor = "F";
} else if (operandOne instanceof Long) {
this.exitTypeDescriptor = "J";
} else if (operandOne instanceof Integer) {
this.exitTypeDescriptor = "I";
}
return new TypedValue(operandOne);
}
return state.operate(Operation.ADD, operandOne, null);
}
TypedValue operandOneValue = leftOp.getValueInternal(state);
Object leftOperand = operandOneValue.getValue();
TypedValue operandTwoValue = getRightOperand().getValueInternal(state);
Object rightOperand = operandTwoValue.getValue();
if (leftOperand instanceof Number && rightOperand instanceof Number) {
Number leftNumber = (Number) leftOperand;
Number rightNumber = (Number) rightOperand;
if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClreplaced(leftNumber, BigDecimal.clreplaced);
BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClreplaced(rightNumber, BigDecimal.clreplaced);
return new TypedValue(leftBigDecimal.add(rightBigDecimal));
} else if (leftNumber instanceof Double || rightNumber instanceof Double) {
this.exitTypeDescriptor = "D";
return new TypedValue(leftNumber.doubleValue() + rightNumber.doubleValue());
} else if (leftNumber instanceof Float || rightNumber instanceof Float) {
this.exitTypeDescriptor = "F";
return new TypedValue(leftNumber.floatValue() + rightNumber.floatValue());
} else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClreplaced(leftNumber, BigInteger.clreplaced);
BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClreplaced(rightNumber, BigInteger.clreplaced);
return new TypedValue(leftBigInteger.add(rightBigInteger));
} else if (leftNumber instanceof Long || rightNumber instanceof Long) {
this.exitTypeDescriptor = "J";
return new TypedValue(leftNumber.longValue() + rightNumber.longValue());
} else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) {
this.exitTypeDescriptor = "I";
return new TypedValue(leftNumber.intValue() + rightNumber.intValue());
} else {
// Unknown Number subtypes -> best guess is double addition
return new TypedValue(leftNumber.doubleValue() + rightNumber.doubleValue());
}
}
if (leftOperand instanceof String && rightOperand instanceof String) {
this.exitTypeDescriptor = "Ljava/lang/String";
return new TypedValue((String) leftOperand + rightOperand);
}
if (leftOperand instanceof String) {
return new TypedValue(leftOperand + (rightOperand == null ? "null" : convertTypedValueToString(operandTwoValue, state)));
}
if (rightOperand instanceof String) {
return new TypedValue((leftOperand == null ? "null" : convertTypedValueToString(operandOneValue, state)) + rightOperand);
}
return state.operate(Operation.ADD, leftOperand, rightOperand);
}
13
View Source File : Projection.java
License : Apache License 2.0
Project Creator : SourceHot
License : Apache License 2.0
Project Creator : SourceHot
@Override
protected ValueRef getValueRef(ExpressionState state) throws EvaluationException {
TypedValue op = state.getActiveContextObject();
Object operand = op.getValue();
boolean operandIsArray = ObjectUtils.isArray(operand);
// TypeDescriptor operandTypeDescriptor = op.getTypeDescriptor();
// When the input is a map, we push a special context object on the stack
// before calling the specified operation. This special context object
// has two fields 'key' and 'value' that refer to the map entries key
// and value, and they can be referenced in the operation
// eg. {'a':'y','b':'n'}.![value=='y'?key:null]" == ['a', null]
if (operand instanceof Map) {
Map<?, ?> mapData = (Map<?, ?>) operand;
List<Object> result = new ArrayList<>();
for (Map.Entry<?, ?> entry : mapData.entrySet()) {
try {
state.pushActiveContextObject(new TypedValue(entry));
state.enterScope();
result.add(this.children[0].getValueInternal(state).getValue());
} finally {
state.popActiveContextObject();
state.exitScope();
}
}
// TODO unable to build correct type descriptor
return new ValueRef.TypedValueHolderValueRef(new TypedValue(result), this);
}
if (operand instanceof Iterable || operandIsArray) {
Iterable<?> data = (operand instanceof Iterable ? (Iterable<?>) operand : Arrays.asList(ObjectUtils.toObjectArray(operand)));
List<Object> result = new ArrayList<>();
Clreplaced<?> arrayElementType = null;
for (Object element : data) {
try {
state.pushActiveContextObject(new TypedValue(element));
state.enterScope("index", result.size());
Object value = this.children[0].getValueInternal(state).getValue();
if (value != null && operandIsArray) {
arrayElementType = determineCommonType(arrayElementType, value.getClreplaced());
}
result.add(value);
} finally {
state.exitScope();
state.popActiveContextObject();
}
}
if (operandIsArray) {
if (arrayElementType == null) {
arrayElementType = Object.clreplaced;
}
Object resultArray = Array.newInstance(arrayElementType, result.size());
System.arraycopy(result.toArray(), 0, resultArray, 0, result.size());
return new ValueRef.TypedValueHolderValueRef(new TypedValue(resultArray), this);
}
return new ValueRef.TypedValueHolderValueRef(new TypedValue(result), this);
}
if (operand == null) {
if (this.nullSafe) {
return ValueRef.NullValueRef.INSTANCE;
}
throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROJECTION_NOT_SUPPORTED_ON_TYPE, "null");
}
throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROJECTION_NOT_SUPPORTED_ON_TYPE, operand.getClreplaced().getName());
}
13
View Source File : OpPlusTests.java
License : MIT License
Project Creator : mindcarver
License : MIT License
Project Creator : mindcarver
@Test
public void test_binaryPlusWithTimeConverted() {
final SimpleDateFormat format = new SimpleDateFormat("hh :--: mm :--: ss", Locale.ENGLISH);
GenericConversionService conversionService = new GenericConversionService();
conversionService.addConverter(new Converter<Time, String>() {
@Override
public String convert(Time source) {
return format.format(source);
}
});
StandardEvaluationContext evaluationContextConverter = new StandardEvaluationContext();
evaluationContextConverter.setTypeConverter(new StandardTypeConverter(conversionService));
ExpressionState expressionState = new ExpressionState(evaluationContextConverter);
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);
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getObjectType());
replacedertEquals(String.clreplaced, value.getTypeDescriptor().getType());
replacedertEquals(format.format(time) + " is now", value.getValue());
}
13
View Source File : PropertyOrFieldReference.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
public boolean isWritableProperty(String name, TypedValue contextObject, EvaluationContext evalContext) throws EvaluationException {
List<PropertyAccessor> accessorsToTry = getPropertyAccessorsToTry(contextObject.getValue(), evalContext.getPropertyAccessors());
if (accessorsToTry != null) {
for (PropertyAccessor accessor : accessorsToTry) {
try {
if (accessor.canWrite(evalContext, contextObject.getValue(), name)) {
return true;
}
} catch (AccessException ex) {
// let others try
}
}
}
return false;
}
13
View Source File : Projection.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Override
protected ValueRef getValueRef(ExpressionState state) throws EvaluationException {
TypedValue op = state.getActiveContextObject();
Object operand = op.getValue();
boolean operandIsArray = ObjectUtils.isArray(operand);
// TypeDescriptor operandTypeDescriptor = op.getTypeDescriptor();
// When the input is a map, we push a special context object on the stack
// before calling the specified operation. This special context object
// has two fields 'key' and 'value' that refer to the map entries key
// and value, and they can be referenced in the operation
// eg. {'a':'y','b':'n'}.![value=='y'?key:null]" == ['a', null]
if (operand instanceof Map) {
Map<?, ?> mapData = (Map<?, ?>) operand;
List<Object> result = new ArrayList<Object>();
for (Map.Entry<?, ?> entry : mapData.entrySet()) {
try {
state.pushActiveContextObject(new TypedValue(entry));
state.enterScope();
result.add(this.children[0].getValueInternal(state).getValue());
} finally {
state.popActiveContextObject();
state.exitScope();
}
}
// TODO unable to build correct type descriptor
return new ValueRef.TypedValueHolderValueRef(new TypedValue(result), this);
}
if (operand instanceof Iterable || operandIsArray) {
Iterable<?> data = (operand instanceof Iterable ? (Iterable<?>) operand : Arrays.asList(ObjectUtils.toObjectArray(operand)));
List<Object> result = new ArrayList<Object>();
int idx = 0;
Clreplaced<?> arrayElementType = null;
for (Object element : data) {
try {
state.pushActiveContextObject(new TypedValue(element));
state.enterScope("index", idx);
Object value = this.children[0].getValueInternal(state).getValue();
if (value != null && operandIsArray) {
arrayElementType = determineCommonType(arrayElementType, value.getClreplaced());
}
result.add(value);
} finally {
state.exitScope();
state.popActiveContextObject();
}
idx++;
}
if (operandIsArray) {
if (arrayElementType == null) {
arrayElementType = Object.clreplaced;
}
Object resultArray = Array.newInstance(arrayElementType, result.size());
System.arraycopy(result.toArray(), 0, resultArray, 0, result.size());
return new ValueRef.TypedValueHolderValueRef(new TypedValue(resultArray), this);
}
return new ValueRef.TypedValueHolderValueRef(new TypedValue(result), this);
}
if (operand == null) {
if (this.nullSafe) {
return ValueRef.NullValueRef.INSTANCE;
}
throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROJECTION_NOT_SUPPORTED_ON_TYPE, "null");
}
throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROJECTION_NOT_SUPPORTED_ON_TYPE, operand.getClreplaced().getName());
}
13
View Source File : OpPlus.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
SpelNodeImpl leftOp = getLeftOperand();
SpelNodeImpl rightOp = getRightOperand();
if (rightOp == null) {
// if only one operand, then this is unary plus
Object operandOne = leftOp.getValueInternal(state).getValue();
if (operandOne instanceof Number) {
if (operandOne instanceof Double) {
this.exitTypeDescriptor = "D";
} else if (operandOne instanceof Float) {
this.exitTypeDescriptor = "F";
} else if (operandOne instanceof Long) {
this.exitTypeDescriptor = "J";
} else if (operandOne instanceof Integer) {
this.exitTypeDescriptor = "I";
}
return new TypedValue(operandOne);
}
return state.operate(Operation.ADD, operandOne, null);
}
TypedValue operandOneValue = leftOp.getValueInternal(state);
Object leftOperand = operandOneValue.getValue();
TypedValue operandTwoValue = rightOp.getValueInternal(state);
Object rightOperand = operandTwoValue.getValue();
if (leftOperand instanceof Number && rightOperand instanceof Number) {
Number leftNumber = (Number) leftOperand;
Number rightNumber = (Number) rightOperand;
if (leftNumber instanceof BigDecimal || rightNumber instanceof BigDecimal) {
BigDecimal leftBigDecimal = NumberUtils.convertNumberToTargetClreplaced(leftNumber, BigDecimal.clreplaced);
BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClreplaced(rightNumber, BigDecimal.clreplaced);
return new TypedValue(leftBigDecimal.add(rightBigDecimal));
} else if (leftNumber instanceof Double || rightNumber instanceof Double) {
this.exitTypeDescriptor = "D";
return new TypedValue(leftNumber.doubleValue() + rightNumber.doubleValue());
} else if (leftNumber instanceof Float || rightNumber instanceof Float) {
this.exitTypeDescriptor = "F";
return new TypedValue(leftNumber.floatValue() + rightNumber.floatValue());
} else if (leftNumber instanceof BigInteger || rightNumber instanceof BigInteger) {
BigInteger leftBigInteger = NumberUtils.convertNumberToTargetClreplaced(leftNumber, BigInteger.clreplaced);
BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClreplaced(rightNumber, BigInteger.clreplaced);
return new TypedValue(leftBigInteger.add(rightBigInteger));
} else if (leftNumber instanceof Long || rightNumber instanceof Long) {
this.exitTypeDescriptor = "J";
return new TypedValue(leftNumber.longValue() + rightNumber.longValue());
} else if (CodeFlow.isIntegerForNumericOp(leftNumber) || CodeFlow.isIntegerForNumericOp(rightNumber)) {
this.exitTypeDescriptor = "I";
return new TypedValue(leftNumber.intValue() + rightNumber.intValue());
} else {
// Unknown Number subtypes -> best guess is double addition
return new TypedValue(leftNumber.doubleValue() + rightNumber.doubleValue());
}
}
if (leftOperand instanceof String && rightOperand instanceof String) {
this.exitTypeDescriptor = "Ljava/lang/String";
return new TypedValue((String) leftOperand + rightOperand);
}
if (leftOperand instanceof String) {
return new TypedValue(leftOperand + (rightOperand == null ? "null" : convertTypedValueToString(operandTwoValue, state)));
}
if (rightOperand instanceof String) {
return new TypedValue((leftOperand == null ? "null" : convertTypedValueToString(operandOneValue, state)) + rightOperand);
}
return state.operate(Operation.ADD, leftOperand, rightOperand);
}
13
View Source File : ConstructorReference.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
/**
* Create a new ordinary object and return it.
* @param state the expression state within which this expression is being evaluated
* @return the new object
* @throws EvaluationException if there is a problem creating the object
*/
private TypedValue createNewInstance(ExpressionState state) throws EvaluationException {
Object[] arguments = new Object[getChildCount() - 1];
List<TypeDescriptor> argumentTypes = new ArrayList<TypeDescriptor>(getChildCount() - 1);
for (int i = 0; i < arguments.length; i++) {
TypedValue childValue = this.children[i + 1].getValueInternal(state);
Object value = childValue.getValue();
arguments[i] = value;
argumentTypes.add(TypeDescriptor.forObject(value));
}
ConstructorExecutor executorToUse = this.cachedExecutor;
if (executorToUse != null) {
try {
return executorToUse.execute(state.getEvaluationContext(), arguments);
} catch (AccessException ex) {
// Two reasons this can occur:
// 1. the method invoked actually threw a real exception
// 2. the method invoked was not preplaceded the arguments it expected and has become 'stale'
// In the first case we should not retry, in the second case we should see if there is a
// better suited method.
// To determine which situation it is, the AccessException will contain a cause.
// If the cause is an InvocationTargetException, a user exception was thrown inside the constructor.
// Otherwise the constructor could not be invoked.
if (ex.getCause() instanceof InvocationTargetException) {
// User exception was the root cause - exit now
Throwable rootCause = ex.getCause().getCause();
if (rootCause instanceof RuntimeException) {
throw (RuntimeException) rootCause;
} else {
String typeName = (String) this.children[0].getValueInternal(state).getValue();
throw new SpelEvaluationException(getStartPosition(), rootCause, SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typeName, FormatHelper.formatMethodForMessage("", argumentTypes));
}
}
// At this point we know it wasn't a user problem so worth a retry if a better candidate can be found
this.cachedExecutor = null;
}
}
// Either there was no accessor or it no longer exists
String typeName = (String) this.children[0].getValueInternal(state).getValue();
executorToUse = findExecutorForConstructor(typeName, argumentTypes, state);
try {
this.cachedExecutor = executorToUse;
if (this.cachedExecutor instanceof ReflectiveConstructorExecutor) {
this.exitTypeDescriptor = CodeFlow.toDescriptor(((ReflectiveConstructorExecutor) this.cachedExecutor).getConstructor().getDeclaringClreplaced());
}
return executorToUse.execute(state.getEvaluationContext(), arguments);
} catch (AccessException ex) {
throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typeName, FormatHelper.formatMethodForMessage("", argumentTypes));
}
}
12
View Source File : PropertyOrFieldReference.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
private void writeProperty(TypedValue contextObject, EvaluationContext evalContext, String name, @Nullable Object newValue) throws EvaluationException {
if (contextObject.getValue() == null && this.nullSafe) {
return;
}
if (contextObject.getValue() == null) {
throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL, name);
}
PropertyAccessor accessorToUse = this.cachedWriteAccessor;
if (accessorToUse != null) {
if (evalContext.getPropertyAccessors().contains(accessorToUse)) {
try {
accessorToUse.write(evalContext, contextObject.getValue(), name, newValue);
return;
} catch (Exception ex) {
// This is OK - it may have gone stale due to a clreplaced change,
// let's try to get a new one and call it before giving up...
}
}
this.cachedWriteAccessor = null;
}
List<PropertyAccessor> accessorsToTry = getPropertyAccessorsToTry(contextObject.getValue(), evalContext.getPropertyAccessors());
try {
for (PropertyAccessor accessor : accessorsToTry) {
if (accessor.canWrite(evalContext, contextObject.getValue(), name)) {
this.cachedWriteAccessor = accessor;
accessor.write(evalContext, contextObject.getValue(), name, newValue);
return;
}
}
} catch (AccessException ex) {
throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_PROPERTY_WRITE, name, ex.getMessage());
}
throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE, name, FormatHelper.formatClreplacedNameForMessage(getObjectClreplaced(contextObject.getValue())));
}
See More Examples