org.springframework.expression.spel.support.BooleanTypedValue Java Examples

The following examples show how to use org.springframework.expression.spel.support.BooleanTypedValue. 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: OperatorBetween.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Returns a boolean based on whether a value is in the range expressed. The first
 * operand is any value whilst the second is a list of two values - those two values
 * being the bounds allowed for the first operand (inclusive).
 * @param state the expression state
 * @return true if the left operand is in the range specified, false otherwise
 * @throws EvaluationException if there is a problem evaluating the expression
 */
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object left = getLeftOperand().getValueInternal(state).getValue();
	Object right = getRightOperand().getValueInternal(state).getValue();
	if (!(right instanceof List) || ((List<?>) right).size() != 2) {
		throw new SpelEvaluationException(getRightOperand().getStartPosition(),
				SpelMessage.BETWEEN_RIGHT_OPERAND_MUST_BE_TWO_ELEMENT_LIST);
	}

	List<?> list = (List<?>) right;
	Object low = list.get(0);
	Object high = list.get(1);
	TypeComparator comp = state.getTypeComparator();
	try {
		return BooleanTypedValue.forValue(comp.compare(left, low) >= 0 && comp.compare(left, high) <= 0);
	}
	catch (SpelEvaluationException ex) {
		ex.setPosition(getStartPosition());
		throw ex;
	}
}
 
Example #2
Source File: OperatorInstanceof.java    From spring4-understanding with Apache License 2.0 6 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 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 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;
	this.exitTypeDescriptor = "Z";
	return result;
}
 
Example #3
Source File: OperatorBetween.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a boolean based on whether a value is in the range expressed. The first
 * operand is any value whilst the second is a list of two values - those two values
 * being the bounds allowed for the first operand (inclusive).
 * @param state the expression state
 * @return true if the left operand is in the range specified, false otherwise
 * @throws EvaluationException if there is a problem evaluating the expression
 */
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object left = getLeftOperand().getValueInternal(state).getValue();
	Object right = getRightOperand().getValueInternal(state).getValue();
	if (!(right instanceof List) || ((List<?>) right).size() != 2) {
		throw new SpelEvaluationException(getRightOperand().getStartPosition(),
				SpelMessage.BETWEEN_RIGHT_OPERAND_MUST_BE_TWO_ELEMENT_LIST);
	}

	List<?> list = (List<?>) right;
	Object low = list.get(0);
	Object high = list.get(1);
	TypeComparator comp = state.getTypeComparator();
	try {
		return BooleanTypedValue.forValue(comp.compare(left, low) >= 0 && comp.compare(left, high) <= 0);
	}
	catch (SpelEvaluationException ex) {
		ex.setPosition(getStartPosition());
		throw ex;
	}
}
 
Example #4
Source File: OperatorBetween.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns a boolean based on whether a value is in the range expressed. The first
 * operand is any value whilst the second is a list of two values - those two values
 * being the bounds allowed for the first operand (inclusive).
 * @param state the expression state
 * @return true if the left operand is in the range specified, false otherwise
 * @throws EvaluationException if there is a problem evaluating the expression
 */
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object left = getLeftOperand().getValueInternal(state).getValue();
	Object right = getRightOperand().getValueInternal(state).getValue();
	if (!(right instanceof List) || ((List<?>) right).size() != 2) {
		throw new SpelEvaluationException(getRightOperand().getStartPosition(),
				SpelMessage.BETWEEN_RIGHT_OPERAND_MUST_BE_TWO_ELEMENT_LIST);
	}

	List<?> list = (List<?>) right;
	Object low = list.get(0);
	Object high = list.get(1);
	TypeComparator comp = state.getTypeComparator();
	try {
		return BooleanTypedValue.forValue(comp.compare(left, low) >= 0 && comp.compare(left, high) <= 0);
	}
	catch (SpelEvaluationException ex) {
		ex.setPosition(getStartPosition());
		throw ex;
	}
}
 
Example #5
Source File: OperatorBetween.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Returns a boolean based on whether a value is in the range expressed. The first
 * operand is any value whilst the second is a list of two values - those two values
 * being the bounds allowed for the first operand (inclusive).
 * @param state the expression state
 * @return true if the left operand is in the range specified, false otherwise
 * @throws EvaluationException if there is a problem evaluating the expression
 */
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object left = getLeftOperand().getValueInternal(state).getValue();
	Object right = getRightOperand().getValueInternal(state).getValue();
	if (!(right instanceof List) || ((List<?>) right).size() != 2) {
		throw new SpelEvaluationException(getRightOperand().getStartPosition(),
				SpelMessage.BETWEEN_RIGHT_OPERAND_MUST_BE_TWO_ELEMENT_LIST);
	}

	List<?> list = (List<?>) right;
	Object low = list.get(0);
	Object high = list.get(1);
	TypeComparator comp = state.getTypeComparator();
	try {
		return BooleanTypedValue.forValue(comp.compare(left, low) >= 0 && comp.compare(left, high) <= 0);
	}
	catch (SpelEvaluationException ex) {
		ex.setPosition(getStartPosition());
		throw ex;
	}
}
 
Example #6
Source File: OperatorNot.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	try {
		Boolean value = this.children[0].getValue(state, Boolean.class);
		if (value == null) {
			throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
		}
		return BooleanTypedValue.forValue(!value);
	}
	catch (SpelEvaluationException ex) {
		ex.setPosition(getChild(0).getStartPosition());
		throw ex;
	}
}
 
Example #7
Source File: BooleanLiteral.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	if (this.value == BooleanTypedValue.TRUE) {
		mv.visitLdcInsn(1);
	}
	else {
		mv.visitLdcInsn(0);
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example #8
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 #9
Source File: OpNE.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object left = getLeftOperand().getValueInternal(state).getValue();
	Object right = getRightOperand().getValueInternal(state).getValue();
	leftActualDescriptor = CodeFlow.toDescriptorFromObject(left);
	rightActualDescriptor = CodeFlow.toDescriptorFromObject(right);
	return BooleanTypedValue.forValue(!equalityCheck(state, left, right));
}
 
Example #10
Source File: OpAnd.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	if (!getBooleanValue(state, getLeftOperand())) {
		// no need to evaluate right operand
		return BooleanTypedValue.FALSE;
	}
	return BooleanTypedValue.forValue(getBooleanValue(state, getRightOperand()));
}
 
Example #11
Source File: OperatorNot.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	try {
		Boolean value = this.children[0].getValue(state, Boolean.class);
		if (value == null) {
			throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
		}
		return BooleanTypedValue.forValue(!value);
	}
	catch (SpelEvaluationException ex) {
		ex.setPosition(getChild(0).getStartPosition());
		throw ex;
	}
}
 
Example #12
Source File: OpOr.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	if (getBooleanValue(state, getLeftOperand())) {
		// no need to evaluate right operand
		return BooleanTypedValue.TRUE;
	}
	return BooleanTypedValue.forValue(getBooleanValue(state, getRightOperand()));
}
 
Example #13
Source File: OperatorNot.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	try {
		Boolean value = this.children[0].getValue(state, Boolean.class);
		if (value == null) {
			throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
		}
		return BooleanTypedValue.forValue(!value);
	}
	catch (SpelEvaluationException ex) {
		ex.setPosition(getChild(0).getStartPosition());
		throw ex;
	}
}
 
Example #14
Source File: OpNE.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object left = getLeftOperand().getValueInternal(state).getValue();
	Object right = getRightOperand().getValueInternal(state).getValue();
	this.leftActualDescriptor = CodeFlow.toDescriptorFromObject(left);
	this.rightActualDescriptor = CodeFlow.toDescriptorFromObject(right);
	return BooleanTypedValue.forValue(
			!equalityCheck(state.getEvaluationContext(), left, right));
}
 
Example #15
Source File: OpEQ.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object left = getLeftOperand().getValueInternal(state).getValue();
	Object right = getRightOperand().getValueInternal(state).getValue();
	this.leftActualDescriptor = CodeFlow.toDescriptorFromObject(left);
	this.rightActualDescriptor = CodeFlow.toDescriptorFromObject(right);
	return BooleanTypedValue.forValue(equalityCheck(state.getEvaluationContext(), left, right));
}
 
Example #16
Source File: OpOr.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	if (getBooleanValue(state, getLeftOperand())) {
		// no need to evaluate right operand
		return BooleanTypedValue.TRUE;
	}
	return BooleanTypedValue.forValue(getBooleanValue(state, getRightOperand()));
}
 
Example #17
Source File: OpOr.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	if (getBooleanValue(state, getLeftOperand())) {
		// no need to evaluate right operand
		return BooleanTypedValue.TRUE;
	}
	return BooleanTypedValue.forValue(getBooleanValue(state, getRightOperand()));
}
 
Example #18
Source File: BooleanLiteral.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	if (this.value == BooleanTypedValue.TRUE) {
		mv.visitLdcInsn(1);		
	}
	else {
		mv.visitLdcInsn(0);
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example #19
Source File: OpEQ.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object left = getLeftOperand().getValueInternal(state).getValue();
	Object right = getRightOperand().getValueInternal(state).getValue();
	this.leftActualDescriptor = CodeFlow.toDescriptorFromObject(left);
	this.rightActualDescriptor = CodeFlow.toDescriptorFromObject(right);
	return BooleanTypedValue.forValue(
			equalityCheck(state.getEvaluationContext(), left, right));
}
 
Example #20
Source File: OpAnd.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	if (getBooleanValue(state, getLeftOperand()) == false) {
		// no need to evaluate right operand
		return BooleanTypedValue.FALSE;
	}
	return BooleanTypedValue.forValue(getBooleanValue(state, getRightOperand()));
}
 
Example #21
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 #22
Source File: OperatorMatches.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check the first operand matches the regex specified as the second operand.
 * @param state the expression state
 * @return {@code true} if the first operand matches the regex specified as the
 * second operand, otherwise {@code false}
 * @throws EvaluationException if there is a problem evaluating the expression
 * (e.g. the regex is invalid)
 */
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	SpelNodeImpl leftOp = getLeftOperand();
	SpelNodeImpl rightOp = getRightOperand();
	Object left = leftOp.getValue(state, String.class);
	Object right = getRightOperand().getValueInternal(state).getValue();

	if (!(left instanceof String)) {
		throw new SpelEvaluationException(leftOp.getStartPosition(),
				SpelMessage.INVALID_FIRST_OPERAND_FOR_MATCHES_OPERATOR, left);
	}
	if (!(right instanceof String)) {
		throw new SpelEvaluationException(rightOp.getStartPosition(),
				SpelMessage.INVALID_SECOND_OPERAND_FOR_MATCHES_OPERATOR, right);
	}

	try {
		String leftString = (String) left;
		String rightString = (String) right;
		Pattern pattern = this.patternCache.get(rightString);
		if (pattern == null) {
			pattern = Pattern.compile(rightString);
			this.patternCache.putIfAbsent(rightString, pattern);
		}
		Matcher matcher = pattern.matcher(leftString);
		return BooleanTypedValue.forValue(matcher.matches());
	}
	catch (PatternSyntaxException ex) {
		throw new SpelEvaluationException(rightOp.getStartPosition(), ex, SpelMessage.INVALID_PATTERN, right);
	}
}
 
Example #23
Source File: BooleanLiteral.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void generateCode(MethodVisitor mv, CodeFlow cf) {
	if (this.value == BooleanTypedValue.TRUE) {
		mv.visitLdcInsn(1);		
	}
	else {
		mv.visitLdcInsn(0);
	}
	cf.pushDescriptor(this.exitTypeDescriptor);
}
 
Example #24
Source File: OperatorMatches.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Check the first operand matches the regex specified as the second operand.
 * @param state the expression state
 * @return {@code true} if the first operand matches the regex specified as the
 * second operand, otherwise {@code false}
 * @throws EvaluationException if there is a problem evaluating the expression
 * (e.g. the regex is invalid)
 */
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	SpelNodeImpl leftOp = getLeftOperand();
	SpelNodeImpl rightOp = getRightOperand();
	Object left = leftOp.getValue(state, String.class);
	Object right = getRightOperand().getValueInternal(state).getValue();

	if (!(left instanceof String)) {
		throw new SpelEvaluationException(leftOp.getStartPosition(),
				SpelMessage.INVALID_FIRST_OPERAND_FOR_MATCHES_OPERATOR, left);
	}
	if (!(right instanceof String)) {
		throw new SpelEvaluationException(rightOp.getStartPosition(),
				SpelMessage.INVALID_SECOND_OPERAND_FOR_MATCHES_OPERATOR, right);
	}

	try {
		String leftString = (String) left;
		String rightString = (String) right;
		Pattern pattern = this.patternCache.get(rightString);
		if (pattern == null) {
			pattern = Pattern.compile(rightString);
			this.patternCache.putIfAbsent(rightString, pattern);
		}
		Matcher matcher = pattern.matcher(leftString);
		return BooleanTypedValue.forValue(matcher.matches());
	}
	catch (PatternSyntaxException ex) {
		throw new SpelEvaluationException(rightOp.getStartPosition(), ex, SpelMessage.INVALID_PATTERN, right);
	}
}
 
Example #25
Source File: OpEQ.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object left = getLeftOperand().getValueInternal(state).getValue();
	Object right = getRightOperand().getValueInternal(state).getValue();
	this.leftActualDescriptor = CodeFlow.toDescriptorFromObject(left);
	this.rightActualDescriptor = CodeFlow.toDescriptorFromObject(right);
	return BooleanTypedValue.forValue(equalityCheck(state, left, right));
}
 
Example #26
Source File: OpNE.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object leftValue = getLeftOperand().getValueInternal(state).getValue();
	Object rightValue = getRightOperand().getValueInternal(state).getValue();
	this.leftActualDescriptor = CodeFlow.toDescriptorFromObject(leftValue);
	this.rightActualDescriptor = CodeFlow.toDescriptorFromObject(rightValue);
	return BooleanTypedValue.forValue(!equalityCheck(state.getEvaluationContext(), leftValue, rightValue));
}
 
Example #27
Source File: OperatorNot.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	try {
		Boolean value = this.children[0].getValue(state, Boolean.class);
		if (value == null) {
			throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
		}
		return BooleanTypedValue.forValue(!value);
	}
	catch (SpelEvaluationException ex) {
		ex.setPosition(getChild(0).getStartPosition());
		throw ex;
	}
}
 
Example #28
Source File: OpOr.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	if (getBooleanValue(state, getLeftOperand())) {
		// no need to evaluate right operand
		return BooleanTypedValue.TRUE;
	}
	return BooleanTypedValue.forValue(getBooleanValue(state, getRightOperand()));
}
 
Example #29
Source File: OpNE.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object leftValue = getLeftOperand().getValueInternal(state).getValue();
	Object rightValue = getRightOperand().getValueInternal(state).getValue();
	this.leftActualDescriptor = CodeFlow.toDescriptorFromObject(leftValue);
	this.rightActualDescriptor = CodeFlow.toDescriptorFromObject(rightValue);
	return BooleanTypedValue.forValue(!equalityCheck(state.getEvaluationContext(), leftValue, rightValue));
}
 
Example #30
Source File: OpAnd.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	if (!getBooleanValue(state, getLeftOperand())) {
		// no need to evaluate right operand
		return BooleanTypedValue.FALSE;
	}
	return BooleanTypedValue.forValue(getBooleanValue(state, getRightOperand()));
}