Announcement Announcement Module
Collapse
No announcement yet.
Does Spring Expression Language support IN operator? Similar to SQL IN clause Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Does Spring Expression Language support IN operator? Similar to SQL IN clause

    public class Security {
    private secTyp1;
    public Security (String aSecTyp1) {
    secTyp1 = aSecTyp1;
    }
    }

    I am trying to check if "BOND" is part of the list.

    Security security = new Security("BOND");
    StandardEvaluationContext context = new StandardEvaluationContext(security);
    ExpressionParser parser = new SpelExpressionParser();
    // This should return true
    boolean result = parser.parseExpression("secTyp1 **IN** {'BOND','SWPI'}").getValue(context, Boolean.class);
    // This should return false
    result = parser.parseExpression("secTyp1 **IN** {'FUT','SWPI'}").getValue(context, Boolean.class);


    org.springframework.expression.spel.SpelParseExcep tion: EL1041Epos 8): After parsing a valid expression, there is still more data in the expression: 'IN'
    at org.springframework.expression.spel.standard.Inter nalSpelExpressionParser.doParseExpression(Internal SpelExpressionParser.java:118)
    at org.springframework.expression.spel.standard.SpelE xpressionParser.doParseExpression(SpelExpressionPa rser.java:56)
    at org.springframework.expression.spel.standard.SpelE xpressionParser.doParseExpression(SpelExpressionPa rser.java:1)
    at org.springframework.expression.common.TemplateAwar eExpressionParser.parseExpression(TemplateAwareExp ressionParser.java:66)
    at org.springframework.expression.common.TemplateAwar eExpressionParser.parseExpression(TemplateAwareExp ressionParser.java:56)
    at com.rules.AssignableSecurityRule.evaluateInConditi on(AssignableSecurityRule.java:48)
    at com.rules.AssignableSecurityRuleTest.testINConditi on(AssignableSecurityRuleTest.java:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runRefle ctiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallabl e.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExpl osively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod .evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.e valuate(RunBefores.java:28)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgn ored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild( BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner. java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRu nner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentR unner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRu nner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRu nner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.ja va:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestR eference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecutio n.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:197)
    Last edited by kirankvs; Jul 22nd, 2011, 07:17 AM. Reason: Disable smileys

  • #2
    Hi kirankvs,

    Looking at SpEL grammar, it seems it doesn't support 'IN' operator directly. However, there are some ways to work around this limitation; perhaps the easiest one is to use List#contains. Take a look at the following sample code:

    Code:
    ExpressionParser parser = new SpelExpressionParser();
    StandardEvaluationContext context = new StandardEvaluationContext();
    Security security = new Security("BOND");
    context.setVariable("sec", security);
    boolean result = (boolean) parser.parseExpression("{'BOND','SWPI'}.contains(#sec.secTyp)").getValue(context, Boolean.class); 
    System.out.println(result); // will print 'true'
    result = (boolean) parser.parseExpression("{'FUT','SWPI'}.contains(#sec.secTyp)").getValue(context, Boolean.class); 
    System.out.println(result); // will print 'false'
    Hope this helps.

    Comment

    Working...
    X