Search in sources :

Example 36 with SpelExpression

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);
}
Also used : SpelExpression(org.springframework.expression.spel.standard.SpelExpression)

Example 37 with SpelExpression

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;
}
Also used : SpelCriteria(org.springframework.data.keyvalue.core.SpelCriteria) Pageable(org.springframework.data.domain.Pageable) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Sort(org.springframework.data.domain.Sort) EvaluationContext(org.springframework.expression.EvaluationContext) KeyValueQuery(org.springframework.data.keyvalue.core.query.KeyValueQuery)

Example 38 with SpelExpression

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);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Example 39 with SpelExpression

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("^");
}
Also used : Operator(org.springframework.expression.spel.ast.Operator) SpelExpression(org.springframework.expression.spel.standard.SpelExpression) Test(org.junit.jupiter.api.Test)

Example 40 with SpelExpression

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);
}
Also used : SpelExpression(org.springframework.expression.spel.standard.SpelExpression) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Aggregations

SpelExpression (org.springframework.expression.spel.standard.SpelExpression)56 Test (org.junit.jupiter.api.Test)30 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)27 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)18 Test (org.junit.Test)15 Expression (org.springframework.expression.Expression)8 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)6 ArrayList (java.util.ArrayList)5 Map (java.util.Map)4 EvaluationContext (org.springframework.expression.EvaluationContext)4 HashMap (java.util.HashMap)3 List (java.util.List)3 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)3 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)3 ClientHttpRequestFactory (org.springframework.http.client.ClientHttpRequestFactory)3 SimpleClientHttpRequestFactory (org.springframework.http.client.SimpleClientHttpRequestFactory)3 HttpRequestExecutingMessageHandler (org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler)3 Method (java.lang.reflect.Method)2 BigDecimal (java.math.BigDecimal)2 LinkedHashMap (java.util.LinkedHashMap)2