Java Code Examples for org.springframework.expression.TypedValue#getValue()

The following examples show how to use org.springframework.expression.TypedValue#getValue() . 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: PropertyOrFieldReference.java    From java-technology-stack with MIT License 6 votes vote down vote up
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;
}
 
Example 2
Source File: ExpressionUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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, Class<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 (ClassUtils.isAssignableValue(targetType, value)) {
		return (T) value;
	}
	throw new EvaluationException("Cannot convert value '" + value + "' to type '" + targetType.getName() + "'");
}
 
Example 3
Source File: ExpressionUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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, Class<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 (ClassUtils.isAssignableValue(targetType, value)) {
		return (T) value;
	}
	throw new EvaluationException("Cannot convert value '" + value + "' to type '" + targetType.getName() + "'");
}
 
Example 4
Source File: ExpressionUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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 Class<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 (ClassUtils.isAssignableValue(targetType, value)) {
		return (T) value;
	}
	throw new EvaluationException("Cannot convert value '" + value + "' to type '" + targetType.getName() + "'");
}
 
Example 5
Source File: FunctionReference.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 6
Source File: FunctionReference.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	TypedValue value = state.lookupVariable(this.name);
	if (value == TypedValue.NULL) {
		throw new SpelEvaluationException(getStartPosition(), SpelMessage.FUNCTION_NOT_DEFINED, this.name);
	}
	if (!(value.getValue() instanceof Method)) {
		// Possibly a static Java method registered as a function
		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 7
Source File: ExpressionUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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 Class<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 (ClassUtils.isAssignableValue(targetType, value)) {
		return (T) value;
	}
	throw new EvaluationException("Cannot convert value '" + value + "' to type '" + targetType.getName() + "'");
}
 
Example 8
Source File: PropertyOrFieldReference.java    From spring-analysis-note with MIT License 6 votes vote down vote up
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;
}
 
Example 9
Source File: FunctionReference.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	TypedValue value = state.lookupVariable(this.name);
	if (value == TypedValue.NULL) {
		throw new SpelEvaluationException(getStartPosition(), SpelMessage.FUNCTION_NOT_DEFINED, this.name);
	}
	if (!(value.getValue() instanceof Method)) {
		// Possibly a static Java method registered as a function
		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 10
Source File: ExpressionUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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;
}
 
Example 11
Source File: OperatorInstanceof.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Compare the left operand to see it is an instance of the type specified as the
 * right operand. The right operand must be a class.
 * @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 Class)) {
		throw new SpelEvaluationException(getRightOperand().getStartPosition(),
				SpelMessage.INSTANCEOF_OPERATOR_NEEDS_CLASS_OPERAND,
				(rightValue == null ? "null" : rightValue.getClass().getName()));
	}
	Class<?> rightClass = (Class<?>) rightValue;
	if (leftValue == null) {
		result = BooleanTypedValue.FALSE;  // null is not an instanceof anything
	}
	else {
		result = BooleanTypedValue.forValue(rightClass.isAssignableFrom(leftValue.getClass()));
	}
	this.type = rightClass;
	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;
}
 
Example 12
Source File: ExpressionUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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;
}
 
Example 13
Source File: SyncData.java    From syncer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public String toString() {
  TypedValue rootObject = context != null ? context.getRootObject() : TypedValue.NULL;
  return "Meta{" +
      "dataId=" + dataId +
      ", context=" + (rootObject.getValue() != null ? ((SyncData) rootObject.getValue()).inner == this : null) +
      ", connectionIdentifier='" + connectionIdentifier + '\'' +
      '}';
}
 
Example 14
Source File: OperatorInstanceof.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Compare the left operand to see it is an instance of the type specified as the
 * right operand. The right operand must be a class.
 * @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 Class)) {
		throw new SpelEvaluationException(getRightOperand().getStartPosition(),
				SpelMessage.INSTANCEOF_OPERATOR_NEEDS_CLASS_OPERAND,
				(rightValue == null ? "null" : rightValue.getClass().getName()));
	}
	Class<?> rightClass = (Class<?>) rightValue;
	if (leftValue == null) {
		result = BooleanTypedValue.FALSE;  // null is not an instanceof anything
	}
	else {
		result = BooleanTypedValue.forValue(rightClass.isAssignableFrom(leftValue.getClass()));
	}
	this.type = rightClass;
	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;
}
 
Example 15
Source File: OperatorInstanceof.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compare the left operand to see it is an instance of the type specified as the
 * right operand. The right operand must be a class.
 * @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 Class)) {
		throw new SpelEvaluationException(getRightOperand().getStartPosition(),
				SpelMessage.INSTANCEOF_OPERATOR_NEEDS_CLASS_OPERAND,
				(rightValue == null ? "null" : rightValue.getClass().getName()));
	}
	Class<?> rightClass = (Class<?>) rightValue;
	if (leftValue == null) {
		result = BooleanTypedValue.FALSE;  // null is not an instanceof anything
	}
	else {
		result = BooleanTypedValue.forValue(rightClass.isAssignableFrom(leftValue.getClass()));
	}
	this.type = rightClass;
	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;
}
 
Example 16
Source File: ExpressionState.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public Object convertValue(TypedValue value, TypeDescriptor targetTypeDescriptor) throws EvaluationException {
	Object val = value.getValue();
	return this.relatedContext.getTypeConverter().convertValue(val, TypeDescriptor.forObject(val), targetTypeDescriptor);
}
 
Example 17
Source File: OpPlus.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@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.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			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.convertNumberToTargetClass(leftNumber, BigInteger.class);
			BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
			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);
}
 
Example 18
Source File: ExpressionState.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Nullable
public Object convertValue(TypedValue value, TypeDescriptor targetTypeDescriptor) throws EvaluationException {
	Object val = value.getValue();
	return this.relatedContext.getTypeConverter().convertValue(
			val, TypeDescriptor.forObject(val), targetTypeDescriptor);
}
 
Example 19
Source File: OpPlus.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@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.convertNumberToTargetClass(leftNumber, BigDecimal.class);
			BigDecimal rightBigDecimal = NumberUtils.convertNumberToTargetClass(rightNumber, BigDecimal.class);
			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.convertNumberToTargetClass(leftNumber, BigInteger.class);
			BigInteger rightBigInteger = NumberUtils.convertNumberToTargetClass(rightNumber, BigInteger.class);
			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);
}
 
Example 20
Source File: ExpressionState.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public Object convertValue(TypedValue value, TypeDescriptor targetTypeDescriptor) throws EvaluationException {
	Object val = value.getValue();
	return this.relatedContext.getTypeConverter().convertValue(val, TypeDescriptor.forObject(val), targetTypeDescriptor);
}