/** * Create a parser with default settings. */ public SpelExpressionParser() { this.configuration = new SpelParserConfiguration(); }
public ExpressionState(EvaluationContext context, TypedValue rootObject) { this(context, rootObject, new SpelParserConfiguration(false, false)); }
/** * Create a new {@code StandardBeanExpressionResolver} with the given bean class loader, * using it as the basis for expression compilation. * @param beanClassLoader the factory's bean class loader */ public StandardBeanExpressionResolver(@Nullable ClassLoader beanClassLoader) { this.expressionParser = new SpelExpressionParser(new SpelParserConfiguration(null, beanClassLoader)); }
public ExpressionState(EvaluationContext context) { this(context, context.getRootObject(), new SpelParserConfiguration(false, false)); }
@Test(expected = SpelEvaluationException.class) public void testCreateObjectsOnAttemptToReferenceNull() { TestClass testClass = new TestClass(); StandardEvaluationContext ctx = new StandardEvaluationContext(testClass); ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true)); Object o = parser.parseExpression("wibble.bar").getValue(ctx); assertEquals("hello", o); o = parser.parseExpression("wibble").getValue(ctx); assertNotNull(o); o = parser.parseExpression("wibble2.bar").getValue(ctx); }
@Test(expected = SpelEvaluationException.class) public void testCreateMapsOnAttemptToIndexNull01() { TestClass testClass = new TestClass(); StandardEvaluationContext ctx = new StandardEvaluationContext(testClass); ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true)); Object o = parser.parseExpression("map['a']").getValue(ctx); assertNull(o); o = parser.parseExpression("map").getValue(ctx); assertNotNull(o); o = parser.parseExpression("map2['a']").getValue(ctx); // map2 should be null, there is no setter }
@Test public void testCompiledExpressionForProxy_SPR16191() { SpelExpressionParser expressionParser = new SpelExpressionParser(new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, null)); Expression expression = expressionParser.parseExpression("#target.process(#root)"); VarargsReceiver receiver = new VarargsReceiver(); VarargsInterface proxy = (VarargsInterface) Proxy.newProxyInstance( getClass().getClassLoader(), new Class<?>[] {VarargsInterface.class}, (proxy1, method, args) -> method.invoke(receiver, args)); StandardEvaluationContext evaluationContext = new StandardEvaluationContext(); evaluationContext.setVariable("target", proxy); String result = expression.getValue(evaluationContext, "foo", String.class); result = expression.getValue(evaluationContext, "foo", String.class); assertEquals("OK", result); }
@Test public void increment01root() { Integer i = 42; StandardEvaluationContext ctx = new StandardEvaluationContext(i); ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true)); Expression e = parser.parseExpression("#this++"); assertEquals(42,i.intValue()); try { e.getValue(ctx, Integer.class); fail(); } catch (SpelEvaluationException see) { assertEquals(SpelMessage.NOT_ASSIGNABLE, see.getMessageCode()); } }
@Test public void decrement01root() { Integer i = 42; StandardEvaluationContext ctx = new StandardEvaluationContext(i); ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true)); Expression e = parser.parseExpression("#this--"); assertEquals(42, i.intValue()); try { e.getValue(ctx, Integer.class); fail(); } catch (SpelEvaluationException see) { assertEquals(SpelMessage.NOT_ASSIGNABLE, see.getMessageCode()); } }
@Test public void setPropertyContainingMapAutoGrow() { SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, false)); Expression expression = parser.parseExpression("parameterizedMap"); assertEquals("java.util.Map<java.lang.Integer, java.lang.Integer>", expression.getValueTypeDescriptor(this).toString()); assertEquals(property, expression.getValue(this)); expression = parser.parseExpression("parameterizedMap['9']"); assertEquals(null, expression.getValue(this)); expression.setValue(this, "37"); assertEquals(37, expression.getValue(this)); }
@Test public void incdecTogether() { Spr9751 helper = new Spr9751(); StandardEvaluationContext ctx = new StandardEvaluationContext(helper); ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true)); Expression e; // index1 is 2 at the start - the 'intArray[#root.index1++]' should not be evaluated twice! // intArray[2] is 3 e = parser.parseExpression("intArray[#root.index1++]++"); e.getValue(ctx, Integer.class); assertEquals(3, helper.index1); assertEquals(4, helper.intArray[2]); // index1 is 3 intArray[3] is 4 e = parser.parseExpression("intArray[#root.index1++]--"); assertEquals(4, e.getValue(ctx, Integer.class).intValue()); assertEquals(4, helper.index1); assertEquals(3, helper.intArray[3]); // index1 is 4, intArray[3] is 3 e = parser.parseExpression("intArray[--#root.index1]++"); assertEquals(3, e.getValue(ctx, Integer.class).intValue()); assertEquals(3, helper.index1); assertEquals(4, helper.intArray[3]); }
@Test public void decrement04() { Integer i = 42; StandardEvaluationContext ctx = new StandardEvaluationContext(i); ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true)); try { Expression e = parser.parseExpression("--1"); e.getValue(ctx, Integer.class); fail(); } catch (SpelEvaluationException see) { assertEquals(SpelMessage.NOT_ASSIGNABLE, see.getMessageCode()); } try { Expression e = parser.parseExpression("1--"); e.getValue(ctx, Integer.class); fail(); } catch (SpelEvaluationException see) { assertEquals(SpelMessage.NOT_ASSIGNABLE, see.getMessageCode()); } }
@Test public void SPR10452() { SpelParserConfiguration configuration = new SpelParserConfiguration(false, false); ExpressionParser parser = new SpelExpressionParser(configuration); StandardEvaluationContext context = new StandardEvaluationContext(); Expression spel = parser.parseExpression("#enumType.values()"); context.setVariable("enumType", ABC.class); Object result = spel.getValue(context); assertNotNull(result); assertTrue(result.getClass().isArray()); assertEquals(ABC.A, Array.get(result, 0)); assertEquals(ABC.B, Array.get(result, 1)); assertEquals(ABC.C, Array.get(result, 2)); context.setVariable("enumType", XYZ.class); result = spel.getValue(context); assertNotNull(result); assertTrue(result.getClass().isArray()); assertEquals(XYZ.X, Array.get(result, 0)); assertEquals(XYZ.Y, Array.get(result, 1)); assertEquals(XYZ.Z, Array.get(result, 2)); }
@Test public void setGenericPropertyContainingListAutogrow() { List<Integer> property = new ArrayList<>(); this.property = property; SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true)); Expression expression = parser.parseExpression("property"); assertEquals("@org.springframework.expression.spel.IndexingTests$FieldAnnotation java.util.ArrayList<?>", expression.getValueTypeDescriptor(this).toString()); assertEquals(property, expression.getValue(this)); expression = parser.parseExpression("property[0]"); try { expression.setValue(this, "4"); } catch (EvaluationException ex) { assertTrue(ex.getMessage().startsWith("EL1053E")); } }
@Test public void limitCollectionGrowing() { TestClass instance = new TestClass(); StandardEvaluationContext ctx = new StandardEvaluationContext(instance); SpelExpressionParser parser = new SpelExpressionParser( new SpelParserConfiguration(true, true, 3)); Expression e = parser.parseExpression("foo[2]"); e.setValue(ctx, "2"); assertThat(instance.getFoo().size(), equalTo(3)); e = parser.parseExpression("foo[3]"); try { e.setValue(ctx, "3"); } catch (SpelEvaluationException see) { assertEquals(SpelMessage.UNABLE_TO_GROW_COLLECTION, see.getMessageCode()); assertThat(instance.getFoo().size(), equalTo(3)); } }
@Test public void indexIntoGenericPropertyContainingGrowingList() { List<String> property = new ArrayList<>(); this.property = property; SpelParserConfiguration configuration = new SpelParserConfiguration(true, true); SpelExpressionParser parser = new SpelExpressionParser(configuration); Expression expression = parser.parseExpression("property"); assertEquals("@org.springframework.expression.spel.IndexingTests$FieldAnnotation java.util.ArrayList<?>", expression.getValueTypeDescriptor(this).toString()); assertEquals(property, expression.getValue(this)); expression = parser.parseExpression("property[0]"); try { assertEquals("bar", expression.getValue(this)); } catch (EvaluationException ex) { assertTrue(ex.getMessage().startsWith("EL1053E")); } }
@Test public void indexIntoGenericPropertyContainingGrowingList2() { List<String> property2 = new ArrayList<>(); this.property2 = property2; SpelParserConfiguration configuration = new SpelParserConfiguration(true, true); SpelExpressionParser parser = new SpelExpressionParser(configuration); Expression expression = parser.parseExpression("property2"); assertEquals("java.util.ArrayList<?>", expression.getValueTypeDescriptor(this).toString()); assertEquals(property2, expression.getValue(this)); expression = parser.parseExpression("property2[0]"); try { assertEquals("bar", expression.getValue(this)); } catch (EvaluationException ex) { assertTrue(ex.getMessage().startsWith("EL1053E")); } }
@Test public void indexIntoGenericPropertyContainingNullList() { SpelParserConfiguration configuration = new SpelParserConfiguration(true, true); SpelExpressionParser parser = new SpelExpressionParser(configuration); Expression expression = parser.parseExpression("property"); assertEquals("@org.springframework.expression.spel.IndexingTests$FieldAnnotation java.lang.Object", expression.getValueTypeDescriptor(this).toString()); assertEquals(property, expression.getValue(this)); expression = parser.parseExpression("property[0]"); try { assertEquals("bar", expression.getValue(this)); } catch (EvaluationException ex) { assertTrue(ex.getMessage().startsWith("EL1027E")); } }
@Test public void compilationOfBasicNullSafeMethodReference() { SpelExpressionParser parser = new SpelExpressionParser( new SpelParserConfiguration(SpelCompilerMode.OFF, getClass().getClassLoader())); SpelExpression expression = parser.parseRaw("#it?.equals(3)"); StandardEvaluationContext context = new StandardEvaluationContext(new Object[] {1}); context.setVariable("it", 3); expression.setEvaluationContext(context); assertTrue(expression.getValue(Boolean.class)); context.setVariable("it", null); assertNull(expression.getValue(Boolean.class)); assertCanCompile(expression); context.setVariable("it", 3); assertTrue(expression.getValue(Boolean.class)); context.setVariable("it", null); assertNull(expression.getValue(Boolean.class)); }
@Test public void failsWhenSettingContextForExpression_SPR12326() { SpelExpressionParser parser = new SpelExpressionParser( new SpelParserConfiguration(SpelCompilerMode.OFF, 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)); // This will trigger compilation (second usage) assertTrue(expression.getValue(Boolean.class)); context.setVariable("it", null); assertNull(expression.getValue(Boolean.class)); assertCanCompile(expression); context.setVariable("it", person); assertTrue(expression.getValue(Boolean.class)); context.setVariable("it", null); assertNull(expression.getValue(Boolean.class)); }