Package org.springframework.expression.spel.support

Examples of org.springframework.expression.spel.support.StandardEvaluationContext


    }

    //this method was roughly based on the implementation of:
    //org.springframework.cache.interceptor.LazyParamAwareEvaluationContext#loadArgsAsVariables(...)
    private EvaluationContext getEvaluationContext(Class<?> targetClass, Method method, Object[] args) {
        EvaluationContext context = new StandardEvaluationContext();
        if (ArrayUtils.isEmpty(args)) {
            return context;
        }

        Method targetMethod = getTargetMethod(targetClass, method);
        for (int i = 0; i < args.length; i++) {
            context.setVariable("p" + i, args[i]);
        }

        String[] parameterNames = paramNameDiscoverer.getParameterNames(targetMethod);
        if (parameterNames != null) {
            for (int i = 0; i < parameterNames.length; i++) {
                context.setVariable(parameterNames[i], args[i]);
            }
        }

        return context;
    }
View Full Code Here


    if (!containsExpression(query)) {
      return query;
    }

    StandardEvaluationContext evalContext = new StandardEvaluationContext();
    evalContext.setVariable(ENTITY_NAME, metadata.getEntityName());

    SpelExpressionParser parser = new SpelExpressionParser();
    Expression expr = parser.parseExpression(query, ParserContext.TEMPLATE_EXPRESSION);

    Object result = expr.getValue(evalContext, String.class);
View Full Code Here

  @Test
  public void testBytesNotCopied() throws Exception {
    Expression expression = new SpelExpressionParser().parseExpression("@service.handleBytes(#root)");
    byte[] bytes = new byte[100];
    StandardEvaluationContext evaluationContext = new StandardEvaluationContext(bytes);
    evaluationContext.setBeanResolver(new BeanFactoryResolver(this.beanFactory));
    byte[] outBytes = expression.getValue(evaluationContext, byte[].class);
    assertSame(bytes, outBytes);
  }
View Full Code Here

*/
public class ReproTests {

  @Test
  public void repro() {
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setBeanResolver(new MyBeanResolver());
    Expression expr = new SpelExpressionParser().parseRaw("@dummyAction.isZero(@dummyBean.getCalls())");
    boolean value = expr.getValue(context, boolean.class);
    System.out.println(value);
  }
View Full Code Here

  public SitemapNode getNode(String id) { return nodes.get(id); }
 
  public String resolve(String exprStr, Map<String, Object> context) {
    log.debug("Resolving expression: {}", exprStr);
    Expression expr = exprParser.parseExpression(exprStr);
    EvaluationContext evalContext = new StandardEvaluationContext(context);
    return (String) expr.getValue(evalContext);
  }
View Full Code Here

  public SpelEvaluatingMethodInterceptor(MethodInterceptor delegate, Object target, BeanFactory beanFactory) {

    Assert.notNull(delegate, "Delegate MethodInterceptor must not be null!");
    Assert.notNull(target, "TargetObject must not be null!");

    StandardEvaluationContext evaluationContext = new StandardEvaluationContext(new TargetWrapper(target));

    if (beanFactory != null) {
      evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
    }

    this.evaluationContext = evaluationContext;
    this.parser = new SpelExpressionParser();
    this.parserContext = new TemplateParserContext();
View Full Code Here

    }
    return EVAL_PAGE;
  }

  private EvaluationContext createEvaluationContext(PageContext pageContext) {
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.addPropertyAccessor(new JspPropertyAccessor(pageContext));
    context.addPropertyAccessor(new MapAccessor());
    context.addPropertyAccessor(new EnvironmentAccessor());
    context.setBeanResolver(new BeanFactoryResolver(getRequestContext().getWebApplicationContext()));
    ConversionService conversionService = getConversionService(pageContext);
    if (conversionService != null) {
      context.setTypeConverter(new StandardTypeConverter(conversionService));
    }
    return context;
  }
View Full Code Here

      Expression expr = this.expressionCache.get(value);
      if (expr == null) {
        expr = this.expressionParser.parseExpression(value, this.beanExpressionParserContext);
        this.expressionCache.put(value, expr);
      }
      StandardEvaluationContext sec = this.evaluationCache.get(evalContext);
      if (sec == null) {
        sec = new StandardEvaluationContext();
        sec.setRootObject(evalContext);
        sec.addPropertyAccessor(new BeanExpressionContextAccessor());
        sec.addPropertyAccessor(new BeanFactoryAccessor());
        sec.addPropertyAccessor(new MapAccessor());
        sec.addPropertyAccessor(new EnvironmentAccessor());
        sec.setBeanResolver(new BeanFactoryResolver(evalContext.getBeanFactory()));
        sec.setTypeLocator(new StandardTypeLocator(evalContext.getBeanFactory().getBeanClassLoader()));
        ConversionService conversionService = evalContext.getBeanFactory().getConversionService();
        if (conversionService != null) {
          sec.setTypeConverter(new StandardTypeConverter(conversionService));
        }
        customizeEvaluationContext(sec);
        this.evaluationCache.put(evalContext, sec);
      }
      return expr.getValue(sec);
View Full Code Here

    assertEquals("abc",expression.getValue(ctx));
  }
 
  @Test
  public void variableReference_userDefined() throws Exception {
    EvaluationContext ctx = new StandardEvaluationContext();
    ctx.setVariable("target", "abc");
    expression = parser.parseExpression("#target");
    assertEquals("abc",expression.getValue(ctx));
    assertCanCompile(expression);
    assertEquals("abc",expression.getValue(ctx))
    ctx.setVariable("target", "123");
    assertEquals("123",expression.getValue(ctx))
    ctx.setVariable("target", 42);
    try {
      assertEquals(42,expression.getValue(ctx));
      fail();
    } catch (SpelEvaluationException see) {
      assertTrue(see.getCause() instanceof ClassCastException);
    }
 
    ctx.setVariable("target", "abc");
    expression = parser.parseExpression("#target.charAt(0)");
    assertEquals('a',expression.getValue(ctx));
    assertCanCompile(expression);
    assertEquals('a',expression.getValue(ctx))
    ctx.setVariable("target", "1");
    assertEquals('1',expression.getValue(ctx))
    ctx.setVariable("target", 42);
    try {
      assertEquals('4',expression.getValue(ctx));
      fail();
    } catch (SpelEvaluationException see) {
      assertTrue(see.getCause() instanceof ClassCastException);
View Full Code Here

  public void failsWhenSettingContextForExpression_SPR12326() {
      SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, this
              .getClass().getClassLoader()));
      Person3 person = new Person3("foo", 1);
      SpelExpression expression = parser.parseRaw("#it?.age?.equals([0])");
      StandardEvaluationContext context = new StandardEvaluationContext(new Object[] { 1 });
      context.setVariable("it", person);
      expression.setEvaluationContext(context);
      assertTrue(expression.getValue(Boolean.class));
      assertTrue(expression.getValue(Boolean.class));      
      assertCanCompile(expression);
      assertTrue(expression.getValue(Boolean.class));
View Full Code Here

TOP

Related Classes of org.springframework.expression.spel.support.StandardEvaluationContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.