Search in sources :

Example 36 with Expression

use of org.springframework.expression.Expression in project uPortal by Jasig.

the class PortalSpELServiceImpl method getValue.

@Override
public String getValue(String expressionString, Object spelEnvironment) {
    final StandardEvaluationContext context = new StandardEvaluationContext(spelEnvironment);
    context.setBeanResolver(this.beanResolver);
    final Expression expression = this.parseCachedExpression(expressionString, TemplateParserContext.INSTANCE);
    return expression.getValue(context, String.class);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Expression(org.springframework.expression.Expression)

Example 37 with Expression

use of org.springframework.expression.Expression in project uPortal by Jasig.

the class PortletSpELServiceImpl method parseExpression.

@Override
public Expression parseExpression(String expressionString) throws ParseException {
    if (this.expressionCache == null) {
        return parseExpression(expressionString, TemplateParserContext.INSTANCE);
    }
    Element element = this.expressionCache.get(expressionString);
    if (element != null) {
        return (Expression) element.getObjectValue();
    }
    final Expression expression = parseExpression(expressionString, TemplateParserContext.INSTANCE);
    element = new Element(expressionString, expression);
    this.expressionCache.put(element);
    return expression;
}
Also used : Expression(org.springframework.expression.Expression) Element(net.sf.ehcache.Element)

Example 38 with Expression

use of org.springframework.expression.Expression in project uPortal by Jasig.

the class RoleBasedBackgroundSetSelectionStrategy method evaluateImagePath.

/*
     * Implementation
     */
private String evaluateImagePath(String pathBeforeProcessing) {
    final Expression x = portalSpELService.parseExpression(pathBeforeProcessing, PortalSpELServiceImpl.TemplateParserContext.INSTANCE);
    final String rslt = x.getValue(evaluationContext, String.class);
    return rslt;
}
Also used : Expression(org.springframework.expression.Expression)

Example 39 with Expression

use of org.springframework.expression.Expression in project spring-framework by spring-projects.

the class CachedExpressionEvaluator method getExpression.

/**
	 * Return the {@link Expression} for the specified SpEL value
	 * <p>Parse the expression if it hasn't been already.
	 * @param cache the cache to use
	 * @param elementKey the element on which the expression is defined
	 * @param expression the expression to parse
	 */
protected Expression getExpression(Map<ExpressionKey, Expression> cache, AnnotatedElementKey elementKey, String expression) {
    ExpressionKey expressionKey = createKey(elementKey, expression);
    Expression expr = cache.get(expressionKey);
    if (expr == null) {
        expr = getParser().parseExpression(expression);
        cache.put(expressionKey, expr);
    }
    return expr;
}
Also used : Expression(org.springframework.expression.Expression)

Example 40 with Expression

use of org.springframework.expression.Expression in project spring-framework by spring-projects.

the class AbstractExpressionTests method evaluate.

/**
	 * Evaluate an expression and check that the actual result matches the
	 * expectedValue and the class of the result matches the expectedClassOfResult.
	 * This method can also check if the expression is writable (for example,
	 * it is a variable or property reference).
	 * @param expression the expression to evaluate
	 * @param expectedValue the expected result for evaluating the expression
	 * @param expectedClassOfResult the expected class of the evaluation result
	 * @param shouldBeWritable should the parsed expression be writable?
	 */
public void evaluate(String expression, Object expectedValue, Class<?> expectedClassOfResult, boolean shouldBeWritable) {
    Expression expr = parser.parseExpression(expression);
    if (expr == null) {
        fail("Parser returned null for expression");
    }
    if (DEBUG) {
        SpelUtilities.printAbstractSyntaxTree(System.out, expr);
    }
    Object value = expr.getValue(eContext);
    if (value == null) {
        if (expectedValue == null) {
            // no point doing other checks
            return;
        }
        assertEquals("Expression returned null value, but expected '" + expectedValue + "'", expectedValue, null);
    }
    Class<? extends Object> resultType = value.getClass();
    if (expectedValue instanceof String) {
        assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue, AbstractExpressionTests.stringValueOf(value));
    } else {
        assertEquals("Did not get expected value for expression '" + expression + "'.", expectedValue, value);
    }
    assertEquals("Type of the result was not as expected.  Expected '" + expectedClassOfResult + "' but result was of type '" + resultType + "'", expectedClassOfResult.equals(resultType), true);
    boolean isWritable = expr.isWritable(eContext);
    if (isWritable != shouldBeWritable) {
        if (shouldBeWritable)
            fail("Expected the expression to be writable but it is not");
        else
            fail("Expected the expression to be readonly but it is not");
    }
}
Also used : Expression(org.springframework.expression.Expression)

Aggregations

Expression (org.springframework.expression.Expression)299 Test (org.junit.Test)228 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)179 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)159 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)114 ExpressionParser (org.springframework.expression.ExpressionParser)78 EvaluationContext (org.springframework.expression.EvaluationContext)53 ArrayList (java.util.ArrayList)32 HashMap (java.util.HashMap)19 CompoundExpression (org.springframework.expression.spel.ast.CompoundExpression)18 EvaluationException (org.springframework.expression.EvaluationException)17 Authentication (org.springframework.security.core.Authentication)16 Map (java.util.Map)14 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)14 List (java.util.List)13 LinkedHashMap (java.util.LinkedHashMap)11 ParseException (org.springframework.expression.ParseException)11 OAuth2Request (org.springframework.security.oauth2.provider.OAuth2Request)10 MethodInvocation (org.aopalliance.intercept.MethodInvocation)9 SimpleMethodInvocation (org.springframework.security.util.SimpleMethodInvocation)9