Search in sources :

Example 1 with ExpressionContext

use of cn.taketoday.expression.ExpressionContext in project today-framework by TAKETODAY.

the class ELProcessorTests method testMethExpr.

@Test
void testMethExpr() {
    MethodExpression meth = null;
    ExpressionContext ctxt = elm.getContext();
    try {
        meth = factory.createMethodExpression(ctxt, "#{str.length}", Object.class, null);
    } catch (NullPointerException ex) {
    // Do nothing
    }
    assertTrue(meth == null);
    meth = factory.createMethodExpression(ctxt, "#{'abc'.length()}", Object.class, null);
    Object result = meth.invoke(ctxt, new Object[] { "abcde" });
    System.out.println("'abc'.length() called, equals " + result);
    assertEquals(result, 3);
}
Also used : ExpressionContext(cn.taketoday.expression.ExpressionContext) MethodExpression(cn.taketoday.expression.MethodExpression) Test(org.junit.jupiter.api.Test)

Example 2 with ExpressionContext

use of cn.taketoday.expression.ExpressionContext in project today-framework by TAKETODAY.

the class ConvertTest method testCustom.

@Test
public void testCustom() {
    elp.getManager().addResolver(new TypeConverter() {

        @Override
        public Object convertToType(ExpressionContext context, Object obj, Class<?> type) {
            if (obj instanceof String && type == MyBean.class) {
                context.setPropertyResolved(true);
                return new MyBean((String) obj);
            }
            return null;
        }
    });
    Object val = elp.getValue("'John Doe'", MyBean.class);
    assertTrue(val instanceof MyBean);
    assertEquals(((MyBean) val).getName(), "John Doe");
}
Also used : TypeConverter(cn.taketoday.expression.TypeConverter) ExpressionContext(cn.taketoday.expression.ExpressionContext) Test(org.junit.jupiter.api.Test)

Aggregations

ExpressionContext (cn.taketoday.expression.ExpressionContext)2 Test (org.junit.jupiter.api.Test)2 MethodExpression (cn.taketoday.expression.MethodExpression)1 TypeConverter (cn.taketoday.expression.TypeConverter)1