Search in sources :

Example 31 with ParserConfiguration

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

the class CoreConfidenceTests method testUnwantedImport.

public void testUnwantedImport() {
    ParserConfiguration conf = new ParserConfiguration();
    conf.addPackageImport("java.util");
    conf.addPackageImport("org.mvel2.tests.core.res");
    ParserContext pctx = new ParserContext(conf);
    MVEL.analysisCompile("ScenarioType.Set.ADD", pctx);
    assertNull(conf.getImports().get("Set"));
}
Also used : ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 32 with ParserConfiguration

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

the class CoreConfidenceTests method testBigDecimalOutput.

public void testBigDecimalOutput() {
    String str = "import java.math.BigDecimal; BigDecimal test = new BigDecimal(\"50000\"); System.out.println(test / new BigDecimal(\"1.13\"));";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.setStrictTypeEnforcement(true);
    ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
    MVEL.executeExpression(stmt, new HashMap());
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 33 with ParserConfiguration

use of org.mule.mvel2.ParserConfiguration in project kie-wb-common by kiegroup.

the class DataEnumLoader method loadEnum.

private Map<String, String[]> loadEnum(String mvelSource, ClassLoader classLoader, MVELEvaluator mvelEvaluator) {
    if (mvelSource == null || (mvelSource.trim().equals(""))) {
        return Collections.emptyMap();
    }
    if (mvelSource.startsWith("=")) {
        mvelSource = mvelSource.substring(1);
    } else {
        mvelSource = "[ " + addCommasForNewLines(mvelSource) + " ]";
    }
    final Object mvelData;
    try {
        final ParserConfiguration pconf = new ParserConfiguration();
        final ParserContext pctx = new ParserContext(pconf);
        pconf.setClassLoader(classLoader);
        final Serializable compiled = MVEL.compileExpression(mvelSource, pctx);
        mvelData = mvelEvaluator.executeExpression(compiled, new HashMap<String, Object>());
    } catch (RuntimeException e) {
        addError("Unable to load enumeration data.");
        addError(e.getMessage());
        addError("Error type: " + e.getClass().getName());
        return Collections.emptyMap();
    }
    if (!(mvelData instanceof Map<?, ?>)) {
        addError("The expression is not a map, it is a " + mvelData.getClass().getName());
        return Collections.emptyMap();
    }
    @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) mvelData;
    Map<String, String[]> newMap = new HashMap<>();
    for (Map.Entry<String, Object> entry : map.entrySet()) {
        String key = makeEnumKey(entry.getKey());
        validateKey(key);
        Object list = entry.getValue();
        if (!(list instanceof List<?> || list instanceof String)) {
            if (list == null) {
                addError("The item with " + key + " is null.");
            } else {
                addError("The item with " + key + " is not a list or a string, it is a " + list.getClass().getName());
            }
            return Collections.emptyMap();
        } else if (list instanceof String) {
            newMap.put(key, new String[] { (String) list });
        } else {
            List<?> items = (List<?>) list;
            String[] newItems = new String[items.size()];
            for (int i = 0; i < items.size(); i++) {
                Object listItem = items.get(i);
                if (!(listItem instanceof String)) {
                    newItems[i] = listItem.toString();
                } else {
                    newItems[i] = (String) listItem;
                }
            }
            newMap.put(key, newItems);
        }
    }
    return newMap;
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) ParserConfiguration(org.mvel2.ParserConfiguration) ArrayList(java.util.ArrayList) List(java.util.List) ParserContext(org.mvel2.ParserContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 34 with ParserConfiguration

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

the class MVELObjectModelResolver method getInstance.

@Override
public Object getInstance(ObjectModel model, ClassLoader cl, Map<String, Object> contextParams) {
    Object instance = null;
    InternalRuntimeManager manager = null;
    if (contextParams.containsKey("runtimeManager")) {
        manager = (InternalRuntimeManager) contextParams.get("runtimeManager");
        instance = manager.getCacheManager().get(model.getIdentifier());
        if (instance != null) {
            return instance;
        }
    }
    ParserConfiguration config = new ParserConfiguration();
    config.setClassLoader(cl);
    ParserContext ctx = new ParserContext(config);
    if (contextParams != null) {
        for (Map.Entry<String, Object> entry : contextParams.entrySet()) {
            ctx.addVariable(entry.getKey(), entry.getValue().getClass());
        }
    }
    Object compiledExpression = MVEL.compileExpression(model.getIdentifier(), ctx);
    instance = MVELSafeHelper.getEvaluator().executeExpression(compiledExpression, contextParams);
    if (manager != null && instance instanceof Cacheable) {
        manager.getCacheManager().add(model.getIdentifier(), instance);
    }
    return instance;
}
Also used : InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) Cacheable(org.kie.internal.runtime.Cacheable) ParserContext(org.mvel2.ParserContext) Map(java.util.Map) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 35 with ParserConfiguration

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

the class MVELLifeCycleManager method eval.

public static Object eval(String str, Map<String, Object> vars) {
    ParserConfiguration pconf = new ParserConfiguration();
    pconf.addPackageImport("org.kie.internal.task.api.model");
    pconf.addPackageImport("org.jbpm.services.task");
    pconf.addPackageImport("org.jbpm.services.task.impl.model");
    pconf.addPackageImport("org.jbpm.services.task.query");
    pconf.addPackageImport("org.jbpm.services.task.internals.lifecycle");
    pconf.addImport(Status.class);
    pconf.addImport(Allowed.class);
    pconf.addPackageImport("java.util");
    ParserContext context = new ParserContext(pconf);
    Serializable s = MVEL.compileExpression(str.trim(), context);
    if (vars != null) {
        return MVELSafeHelper.getEvaluator().executeExpression(s, vars);
    } else {
        return MVELSafeHelper.getEvaluator().executeExpression(s);
    }
}
Also used : Serializable(java.io.Serializable) 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