Search in sources :

Example 51 with CompiledExpression

use of org.mule.mvel2.compiler.CompiledExpression in project mule by mulesoft.

the class MvelDataTypeResolver method resolve.

/**
 * Resolves expression data type
 *
 * @param value result of evaluating the expression on the given message
 * @param event event under evaluation
 * @param serializedExpression expression
 * @return the data type corresponding to the given expression if there is an expression resolver able to resolved it. A default
 *         data type for the value if the expression was not resolved.
 */
public DataType resolve(Object value, PrivilegedEvent event, Serializable serializedExpression) {
    DataType result = null;
    if (serializedExpression instanceof CompiledExpression) {
        CompiledExpression compiledExpression = (CompiledExpression) serializedExpression;
        for (ExpressionDataTypeResolver resolver : resolvers) {
            result = resolver.resolve(event, compiledExpression);
            if (result != null) {
                break;
            }
        }
    }
    if (result == null) {
        Class<?> type = value == null ? Object.class : value.getClass();
        result = DataType.fromType(type);
    }
    return result;
}
Also used : DataType(org.mule.runtime.api.metadata.DataType) CompiledExpression(org.mule.mvel2.compiler.CompiledExpression)

Example 52 with CompiledExpression

use of org.mule.mvel2.compiler.CompiledExpression in project mule by mulesoft.

the class PropertyEnricherDataTypePropagatorTestCase method propagatesDataTypeForInlinedSessionProperty.

@Test
public void propagatesDataTypeForInlinedSessionProperty() throws Exception {
    final DataType expectedDataType = DataType.builder().type(String.class).mediaType(JSON).charset(CUSTOM_ENCODING).build();
    MVELExpressionLanguage expressionLanguage = new MVELExpressionLanguage(muleContext);
    final CompiledExpression compiledExpression = (CompiledExpression) compileExpression("foo = 'unused'", new ParserContext(expressionLanguage.getParserConfiguration()));
    PrivilegedEvent event = (PrivilegedEvent) testEvent();
    event.getSession().setProperty("foo", "bar");
    final PrivilegedEvent.Builder builder = PrivilegedEvent.builder(testEvent());
    dataTypePropagator.propagate((PrivilegedEvent) testEvent(), builder, new TypedValue(TEST_MESSAGE, expectedDataType), compiledExpression);
    assertThat(builder.build().getSession().getPropertyDataType("foo"), like(String.class, JSON, CUSTOM_ENCODING));
}
Also used : PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) DataType(org.mule.runtime.api.metadata.DataType) MVELExpressionLanguage(org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguage) ParserContext(org.mule.mvel2.ParserContext) CompiledExpression(org.mule.mvel2.compiler.CompiledExpression) TypedValue(org.mule.runtime.api.metadata.TypedValue) Test(org.junit.Test)

Example 53 with CompiledExpression

use of org.mule.mvel2.compiler.CompiledExpression in project mule by mulesoft.

the class PropertyExpressionDataTypeResolverTestCase method returnsInlineFlowVarDataType.

@Test
public void returnsInlineFlowVarDataType() throws Exception {
    final String expression = "foo";
    final DataType expectedDataType = DataType.builder().type(String.class).mediaType(JSON).charset(CUSTOM_ENCODING).build();
    MVELExpressionLanguage expressionLanguage = new MVELExpressionLanguage(muleContext);
    final CompiledExpression compiledExpression = (CompiledExpression) compileExpression(expression, new ParserContext(expressionLanguage.getParserConfiguration()));
    PrivilegedEvent testEvent = this.<PrivilegedEvent.Builder>getEventBuilder().message(of(TEST_MESSAGE)).addVariable("foo", EXPRESSION_VALUE, expectedDataType).build();
    assertThat(expressionDataTypeResolver.resolve(testEvent, compiledExpression), like(String.class, JSON, CUSTOM_ENCODING));
}
Also used : PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) DataType(org.mule.runtime.api.metadata.DataType) MVELExpressionLanguage(org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguage) ParserContext(org.mule.mvel2.ParserContext) CompiledExpression(org.mule.mvel2.compiler.CompiledExpression) Test(org.junit.Test)

Example 54 with CompiledExpression

use of org.mule.mvel2.compiler.CompiledExpression in project mvel by mikebrock.

the class CoreConfidenceTests method testArray2.

public void testArray2() {
    String ex = " TestHelper.method(1, {\"a\", \"b\"});\n" + " TestHelper.method(2, {new String(\"a\"), new String(\"b\")});\n" + " TestHelper.method(3, {new Fooz(\"a\"), new Fooz(\"b\")});";
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addImport(TestHelper.class);
    ctx.addImport(Fooz.class);
    ExpressionCompiler compiler = new ExpressionCompiler(ex);
    OptimizerFactory.setDefaultOptimizer("ASM");
    CompiledExpression expr = compiler.compile(ctx);
    executeExpression(expr);
    OptimizerFactory.setDefaultOptimizer("reflective");
    expr = compiler.compile(ctx);
    executeExpression(expr);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 55 with CompiledExpression

use of org.mule.mvel2.compiler.CompiledExpression in project mvel by mikebrock.

the class CoreConfidenceTests method testSetAccessorOverloadedEqualsStrictMode.

public void testSetAccessorOverloadedEqualsStrictMode() {
    ParserContext ctx = new ParserContext();
    ctx.setStrongTyping(true);
    ctx.addInput("foo", Foo.class);
    try {
        CompiledExpression expr = new ExpressionCompiler("foo.bar = 0").compile(ctx);
    } catch (CompileException e) {
        // should fail.
        e.printStackTrace();
        return;
    }
    assertTrue(false);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Aggregations

CompiledExpression (org.mvel2.compiler.CompiledExpression)81 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)65 ParserContext (org.mvel2.ParserContext)48 HashMap (java.util.HashMap)20 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)20 Foo (org.mvel2.tests.core.res.Foo)20 Debugger (org.mvel2.debug.Debugger)16 Frame (org.mvel2.debug.Frame)16 CompiledExpression (org.mule.mvel2.compiler.CompiledExpression)15 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)14 DataType (org.mule.runtime.api.metadata.DataType)11 HashSet (java.util.HashSet)10 Test (org.junit.Test)9 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)9 ParserContext (org.mule.mvel2.ParserContext)8 Map (java.util.Map)6 MVELExpressionLanguage (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguage)6 CompileException (org.mvel2.CompileException)6 ASTNode (org.mvel2.ast.ASTNode)6 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)6