use of org.springframework.expression.spel.standard.SpelExpression in project spring-data-keyvalue by spring-projects.
the class SpelPropertyComparator method compare.
/*
* (non-Javadoc)
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
@SuppressWarnings("ConstantConditions")
@Override
public int compare(T arg1, T arg2) {
SpelExpression expressionToUse = getExpression();
expressionToUse.getEvaluationContext().setVariable("arg1", arg1);
expressionToUse.getEvaluationContext().setVariable("arg2", arg2);
return expressionToUse.getValue(Integer.class) * (asc ? 1 : -1);
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-data-keyvalue by spring-projects.
the class KeyValuePartTreeQuery method prepareQuery.
@SuppressWarnings({ "rawtypes", "unchecked" })
protected KeyValueQuery<?> prepareQuery(KeyValueQuery<?> instance, Object[] parameters) {
ParametersParameterAccessor accessor = new ParametersParameterAccessor(getQueryMethod().getParameters(), parameters);
Object criteria = instance.getCriteria();
if (criteria instanceof SpelCriteria || criteria instanceof SpelExpression) {
SpelExpression spelExpression = getSpelExpression(criteria);
EvaluationContext context = this.evaluationContextProvider.getEvaluationContext(getQueryMethod().getParameters(), parameters);
criteria = new SpelCriteria(spelExpression, context);
}
KeyValueQuery<?> query = new KeyValueQuery(criteria);
Pageable pageable = accessor.getPageable();
Sort sort = accessor.getSort();
query.setOffset(pageable.toOptional().map(Pageable::getOffset).orElse(-1L));
if (pageable.isPaged()) {
query.setRows(pageable.getPageSize());
} else if (instance.getRows() >= 0) {
query.setRows(instance.getRows());
}
query.setSort(sort.isUnsorted() ? instance.getSort() : sort);
return query;
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class EvaluationTests method testTypeReferencesAndQualifiedIdentifierCaching.
@Test
public void testTypeReferencesAndQualifiedIdentifierCaching() {
SpelExpression e = (SpelExpression) parser.parseExpression("T(java.lang.String)");
assertThat(e.isWritable(new StandardEvaluationContext())).isFalse();
assertThat(e.toStringAST()).isEqualTo("T(java.lang.String)");
assertThat(e.getValue(Class.class)).isEqualTo(String.class);
// use cached QualifiedIdentifier:
assertThat(e.toStringAST()).isEqualTo("T(java.lang.String)");
assertThat(e.getValue(Class.class)).isEqualTo(String.class);
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class OperatorTests method testOperatorNames.
@Test
void testOperatorNames() {
Operator node = getOperatorNode((SpelExpression) parser.parseExpression("1==3"));
assertThat(node.getOperatorName()).isEqualTo("==");
node = getOperatorNode((SpelExpression) parser.parseExpression("1!=3"));
assertThat(node.getOperatorName()).isEqualTo("!=");
node = getOperatorNode((SpelExpression) parser.parseExpression("3/3"));
assertThat(node.getOperatorName()).isEqualTo("/");
node = getOperatorNode((SpelExpression) parser.parseExpression("3+3"));
assertThat(node.getOperatorName()).isEqualTo("+");
node = getOperatorNode((SpelExpression) parser.parseExpression("3-3"));
assertThat(node.getOperatorName()).isEqualTo("-");
node = getOperatorNode((SpelExpression) parser.parseExpression("3<4"));
assertThat(node.getOperatorName()).isEqualTo("<");
node = getOperatorNode((SpelExpression) parser.parseExpression("3<=4"));
assertThat(node.getOperatorName()).isEqualTo("<=");
node = getOperatorNode((SpelExpression) parser.parseExpression("3*4"));
assertThat(node.getOperatorName()).isEqualTo("*");
node = getOperatorNode((SpelExpression) parser.parseExpression("3%4"));
assertThat(node.getOperatorName()).isEqualTo("%");
node = getOperatorNode((SpelExpression) parser.parseExpression("3>=4"));
assertThat(node.getOperatorName()).isEqualTo(">=");
node = getOperatorNode((SpelExpression) parser.parseExpression("3 between 4"));
assertThat(node.getOperatorName()).isEqualTo("between");
node = getOperatorNode((SpelExpression) parser.parseExpression("3 ^ 4"));
assertThat(node.getOperatorName()).isEqualTo("^");
}
use of org.springframework.expression.spel.standard.SpelExpression in project spring-framework by spring-projects.
the class OperatorTests method testPlus.
@Test
void testPlus() {
evaluate("7 + 2", "9", Integer.class);
evaluate("3.0f + 5.0f", 8.0f, Float.class);
evaluate("3.0d + 5.0d", 8.0d, Double.class);
evaluate("3 + new java.math.BigDecimal('5')", new BigDecimal("8"), BigDecimal.class);
evaluate("'ab' + 2", "ab2", String.class);
evaluate("2 + 'a'", "2a", String.class);
evaluate("'ab' + null", "abnull", String.class);
evaluate("null + 'ab'", "nullab", String.class);
// AST:
SpelExpression expr = (SpelExpression) parser.parseExpression("+3");
assertThat(expr.toStringAST()).isEqualTo("+3");
expr = (SpelExpression) parser.parseExpression("2+3");
assertThat(expr.toStringAST()).isEqualTo("(2 + 3)");
// use as a unary operator
evaluate("+5d", 5d, Double.class);
evaluate("+5L", 5L, Long.class);
evaluate("+5", 5, Integer.class);
evaluate("+new java.math.BigDecimal('5')", new BigDecimal("5"), BigDecimal.class);
evaluateAndCheckError("+'abc'", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);
// string concatenation
evaluate("'abc'+'def'", "abcdef", String.class);
evaluate("5 + new Integer('37')", 42, Integer.class);
}
Aggregations