Search in sources :

Example 16 with EvaluationException

use of cn.taketoday.expression.EvaluationException in project today-infrastructure by TAKETODAY.

the class IndexingTests method indexIntoGenericPropertyContainingNullList.

@Test
void indexIntoGenericPropertyContainingNullList() {
    SpelParserConfiguration configuration = new SpelParserConfiguration(true, true);
    SpelExpressionParser parser = new SpelExpressionParser(configuration);
    Expression expression = parser.parseExpression("property");
    assertThat(expression.getValueTypeDescriptor(this).toString()).isEqualTo("@cn.taketoday.expression.spel.IndexingTests$FieldAnnotation java.lang.Object");
    assertThat(expression.getValue(this)).isEqualTo(property);
    expression = parser.parseExpression("property[0]");
    try {
        assertThat(expression.getValue(this)).isEqualTo("bar");
    } catch (EvaluationException ex) {
        assertThat(ex.getMessage().startsWith("EL1027E")).isTrue();
    }
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Expression(cn.taketoday.expression.Expression) EvaluationException(cn.taketoday.expression.EvaluationException) Test(org.junit.jupiter.api.Test)

Example 17 with EvaluationException

use of cn.taketoday.expression.EvaluationException in project today-infrastructure by TAKETODAY.

the class IndexingTests method indexIntoGenericPropertyContainingGrowingList2.

@Test
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");
    assertThat(expression.getValueTypeDescriptor(this).toString()).isEqualTo("java.util.ArrayList<?>");
    assertThat(expression.getValue(this)).isEqualTo(property2);
    expression = parser.parseExpression("property2[0]");
    try {
        assertThat(expression.getValue(this)).isEqualTo("bar");
    } catch (EvaluationException ex) {
        assertThat(ex.getMessage().startsWith("EL1053E")).isTrue();
    }
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Expression(cn.taketoday.expression.Expression) ArrayList(java.util.ArrayList) EvaluationException(cn.taketoday.expression.EvaluationException) Test(org.junit.jupiter.api.Test)

Example 18 with EvaluationException

use of cn.taketoday.expression.EvaluationException in project today-infrastructure by TAKETODAY.

the class IndexingTests method indexIntoGenericPropertyContainingGrowingList.

@Test
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");
    assertThat(expression.getValueTypeDescriptor(this).toString()).isEqualTo("@cn.taketoday.expression.spel.IndexingTests$FieldAnnotation java.util.ArrayList<?>");
    assertThat(expression.getValue(this)).isEqualTo(property);
    expression = parser.parseExpression("property[0]");
    try {
        assertThat(expression.getValue(this)).isEqualTo("bar");
    } catch (EvaluationException ex) {
        assertThat(ex.getMessage().startsWith("EL1053E")).isTrue();
    }
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Expression(cn.taketoday.expression.Expression) ArrayList(java.util.ArrayList) EvaluationException(cn.taketoday.expression.EvaluationException) Test(org.junit.jupiter.api.Test)

Example 19 with EvaluationException

use of cn.taketoday.expression.EvaluationException in project today-infrastructure by TAKETODAY.

the class IndexingTests method setGenericPropertyContainingListAutogrow.

@Test
void setGenericPropertyContainingListAutogrow() {
    List<Integer> property = new ArrayList<>();
    this.property = property;
    SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
    Expression expression = parser.parseExpression("property");
    assertThat(expression.getValueTypeDescriptor(this).toString()).isEqualTo("@cn.taketoday.expression.spel.IndexingTests$FieldAnnotation java.util.ArrayList<?>");
    assertThat(expression.getValue(this)).isEqualTo(property);
    expression = parser.parseExpression("property[0]");
    try {
        expression.setValue(this, "4");
    } catch (EvaluationException ex) {
        assertThat(ex.getMessage().startsWith("EL1053E")).isTrue();
    }
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) Expression(cn.taketoday.expression.Expression) ArrayList(java.util.ArrayList) EvaluationException(cn.taketoday.expression.EvaluationException) Test(org.junit.jupiter.api.Test)

Example 20 with EvaluationException

use of cn.taketoday.expression.EvaluationException in project today-infrastructure by TAKETODAY.

the class ExpressionLanguageScenarioTests method testScenario_RegisteringJavaMethodsAsFunctionsAndCallingThem.

/**
 * Scenario: using your own java methods and calling them from the expression
 */
@Test
public void testScenario_RegisteringJavaMethodsAsFunctionsAndCallingThem() throws SecurityException, NoSuchMethodException {
    try {
        // Create a parser
        SpelExpressionParser parser = new SpelExpressionParser();
        // Use the standard evaluation context
        StandardEvaluationContext ctx = new StandardEvaluationContext();
        ctx.registerFunction("repeat", ExpressionLanguageScenarioTests.class.getDeclaredMethod("repeat", String.class));
        Expression expr = parser.parseRaw("#repeat('hello')");
        Object value = expr.getValue(ctx);
        assertThat(value).isEqualTo("hellohello");
    } catch (EvaluationException | ParseException ex) {
        throw new AssertionError(ex.getMessage(), ex);
    }
}
Also used : SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Expression(cn.taketoday.expression.Expression) EvaluationException(cn.taketoday.expression.EvaluationException) ParseException(cn.taketoday.expression.ParseException) Test(org.junit.jupiter.api.Test)

Aggregations

EvaluationException (cn.taketoday.expression.EvaluationException)22 Expression (cn.taketoday.expression.Expression)14 Test (org.junit.jupiter.api.Test)14 SpelExpressionParser (cn.taketoday.expression.spel.standard.SpelExpressionParser)12 ArrayList (java.util.ArrayList)10 MethodParameter (cn.taketoday.core.MethodParameter)6 TypeDescriptor (cn.taketoday.core.TypeDescriptor)6 ParseException (cn.taketoday.expression.ParseException)6 TypeConverter (cn.taketoday.expression.TypeConverter)6 AccessException (cn.taketoday.expression.AccessException)4 SpelEvaluationException (cn.taketoday.expression.spel.SpelEvaluationException)4 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)4 Nullable (cn.taketoday.lang.Nullable)4 MethodFilter (cn.taketoday.expression.MethodFilter)2 OperatorOverloader (cn.taketoday.expression.OperatorOverloader)2 TypedValue (cn.taketoday.expression.TypedValue)2 Constructor (java.lang.reflect.Constructor)2 Method (java.lang.reflect.Method)2 LinkedHashSet (java.util.LinkedHashSet)2