Search in sources :

Example 6 with ParserConfiguration

use of org.mule.mvel2.ParserConfiguration in project drools by kiegroup.

the class MVELDialectRuntimeData method getParserConfiguration.

public ParserConfiguration getParserConfiguration() {
    if (parserConfiguration == null) {
        ClassLoader packageClassLoader = getPackageClassLoader();
        String key = null;
        Object value = null;
        try {
            // First replace fields and method tokens with actual instances
            for (Entry<String, Object> entry : this.imports.entrySet()) {
                key = entry.getKey();
                value = entry.getValue();
                if (entry.getValue() instanceof String) {
                    String str = (String) value;
                    // @TODO MVEL doesn't yet support importing of fields
                    if (str.startsWith("m:")) {
                        Class cls = packageClassLoader.loadClass(str.substring(2));
                        for (Method method : cls.getDeclaredMethods()) {
                            if (method.getName().equals(key)) {
                                entry.setValue(method);
                                break;
                            }
                        }
                    } else {
                        Class cls = packageClassLoader.loadClass(str);
                        entry.setValue(cls);
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException("Unable to resolve method of field: " + key + " - " + value, e);
        }
        final ParserConfiguration conf = new ParserConfiguration();
        conf.setImports(this.imports);
        conf.setPackageImports(this.packageImports);
        conf.setClassLoader(packageClassLoader);
        this.parserConfiguration = conf;
    }
    return this.parserConfiguration;
}
Also used : Method(java.lang.reflect.Method) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 7 with ParserConfiguration

use of org.mule.mvel2.ParserConfiguration in project mvel by mvel.

the class CoreConfidenceTests method testContextFieldNotFound.

public void testContextFieldNotFound() {
    String str = "'stilton'.equals( type );";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.addInput("this", Cheese.class);
    pctx.setStrictTypeEnforcement(true);
    pctx.setStrongTyping(true);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    MVEL.executeExpression(stmt, new Cheese(), new HashMap());
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Cheese(org.mvel2.tests.core.res.Cheese) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 8 with ParserConfiguration

use of org.mule.mvel2.ParserConfiguration in project mvel by mvel.

the class CoreConfidenceTests method testNestedNumInMapKey.

public void testNestedNumInMapKey() {
    String str = "objectKeyMaptributes[Triangle.Foo.OBTUSE]";
    ParserConfiguration pconf = new ParserConfiguration();
    pconf.addImport("Triangle", Triangle.class);
    ParserContext pctx = new ParserContext(pconf);
    pctx.addInput("this", Person.class);
    pctx.setStrongTyping(true);
    Foo foo = new Foo();
    Person p = new Person();
    Map<Object, Foo> map = new HashMap<Object, Foo>();
    map.put(Triangle.Foo.OBTUSE, foo);
    p.setObjectKeyMaptributes(map);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    Object o = MVEL.executeExpression(stmt, p, new HashMap());
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Foo(org.mvel2.tests.core.res.Foo) MapObject(org.mvel2.tests.core.res.MapObject) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 9 with ParserConfiguration

use of org.mule.mvel2.ParserConfiguration in project mvel by mvel.

the class CoreConfidenceTests method testMethodCallWithSpaces.

public void testMethodCallWithSpaces() {
    String[] str = new String[] { "Foo f = new Foo(); f.setBoolTest( true )   ; f.isBoolTest()", "Foo f = new Foo(); f . setBoolTest( true ) ; f.isBoolTest()", "Foo f = new Foo(); f. setBoolTest( true )  ; f.isBoolTest()", "Foo f = new Foo(); f .setBoolTest( true )  ; f.isBoolTest()", "Foo f = new Foo(); f.boolTest = true   ; f.isBoolTest()", "Foo f = new Foo(); f . boolTest = true ; f.isBoolTest()", "Foo f = new Foo(); f. boolTest = true  ; f.isBoolTest()", "Foo f = new Foo(); f .boolTest = true  ; f.isBoolTest()" };
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("this", Bar.class);
    pctx.addImport(Foo.class);
    List<String> errors = new ArrayList<String>();
    for (String s : str) {
        try {
            ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(s, pctx);
            Bar ctx = new Bar();
            Boolean result = (Boolean) MVEL.executeExpression(stmt, ctx, new HashMap());
            assertTrue(result);
        } catch (Exception e) {
            e.printStackTrace();
            errors.add("**** Error on expression: " + s + "\n" + e.getMessage());
        }
    }
    assertTrue(errors.toString(), errors.isEmpty());
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) Bar(org.mvel2.tests.core.res.Bar) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ParserContext(org.mvel2.ParserContext) CompileException(org.mvel2.CompileException) IOException(java.io.IOException) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 10 with ParserConfiguration

use of org.mule.mvel2.ParserConfiguration in project mvel by mvel.

the class CoreConfidenceTests method testNestedClassWithNestedGenericsOnNakedMethod.

public void testNestedClassWithNestedGenericsOnNakedMethod() {
    String str = "deliveries.size";
    MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.addInput("this", Triangle.class);
    pctx.setStrongTyping(true);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    assertEquals(Integer.valueOf(0), (Integer) MVEL.executeExpression(stmt, new Triangle(), new HashMap()));
    str = "deliveries.size == 0";
    stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    assertTrue((Boolean) MVEL.executeExpression(stmt, new Triangle(), new HashMap()));
    MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = false;
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Aggregations

ParserConfiguration (org.mvel2.ParserConfiguration)74 ParserContext (org.mvel2.ParserContext)70 HashMap (java.util.HashMap)39 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)32 LinkedHashMap (java.util.LinkedHashMap)29 Map (java.util.Map)23 Serializable (java.io.Serializable)10 MVELExpressionLanguageContext (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageContext)7 CompileException (org.mvel2.CompileException)7 ParserConfiguration (org.mule.mvel2.ParserConfiguration)6 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)5 MapObject (org.mvel2.tests.core.res.MapObject)5 Method (java.lang.reflect.Method)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Before (org.junit.Before)3 CachedMapVariableResolverFactory (org.mule.mvel2.integration.impl.CachedMapVariableResolverFactory)3 MVELExpressionExecutor (org.mule.runtime.core.internal.el.mvel.MVELExpressionExecutor)3 Bar (org.mvel2.tests.core.res.Bar)3