Search in sources :

Example 71 with ParserConfiguration

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

the class EnumDropdownServiceImpl method loadDropDownExpression.

protected String[] loadDropDownExpression(final ClassLoader classLoader, final MVELEvaluator mvelEvaluator, final String[] valuePairs, String expression) {
    try {
        final Map<String, String> context = new HashMap<String, String>();
        for (final String valuePair : valuePairs) {
            if (valuePair == null) {
                return new String[0];
            }
            String[] pair = valuePair.split("=");
            if (pair.length == 1) {
                String[] swap = new String[2];
                swap[0] = pair[0];
                swap[1] = "";
                pair = swap;
            }
            context.put(pair[0], pair[1]);
        }
        // first interpolate the pairs
        expression = (String) TemplateRuntime.eval(expression, context);
        // now we can eval it for real...
        final ParserConfiguration pconf = new ParserConfiguration();
        final ParserContext pctx = new ParserContext(pconf);
        pconf.setClassLoader(classLoader);
        final Serializable compiled = MVEL.compileExpression(expression, pctx);
        Object result = mvelEvaluator.executeExpression(compiled, new HashMap<String, Object>());
        // Handle result of evaluation
        if (result instanceof String[]) {
            return (String[]) result;
        } else if (result instanceof List) {
            List l = (List) result;
            String[] xs = new String[l.size()];
            for (int i = 0; i < xs.length; i++) {
                Object el = l.get(i);
                xs[i] = el.toString();
            }
            return xs;
        } else {
            return null;
        }
    } catch (Exception e) {
        throw ExceptionUtilities.handleException(e);
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) List(java.util.List) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 72 with ParserConfiguration

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

the class ASTNode method optimize.

private Object optimize(Object ctx, Object thisValue, VariableResolverFactory factory) {
    if ((fields & DEOP) != 0) {
        fields ^= DEOP;
    }
    AccessorOptimizer optimizer;
    Object retVal = null;
    if ((fields & NOJIT) != 0 || factory != null && factory.isResolveable(nameCache)) {
        optimizer = getAccessorCompiler(SAFE_REFLECTIVE);
    } else {
        optimizer = getDefaultAccessorCompiler();
    }
    ParserContext pCtx;
    if ((fields & PCTX_STORED) != 0) {
        pCtx = (ParserContext) literal;
    } else {
        pCtx = new ParserContext(new ParserConfiguration(getInjectedImports(factory), null));
    }
    try {
        pCtx.optimizationNotify();
        setAccessor(optimizer.optimizeAccessor(pCtx, expr, start, offset, ctx, thisValue, factory, true, egressType));
    } catch (OptimizationNotSupported ne) {
        setAccessor((optimizer = getAccessorCompiler(SAFE_REFLECTIVE)).optimizeAccessor(pCtx, expr, start, offset, ctx, thisValue, factory, true, null));
    }
    if (accessor == null) {
        return get(expr, start, offset, ctx, factory, thisValue);
    }
    if (retVal == null) {
        retVal = optimizer.getResultOptPass();
    }
    if (egressType == null) {
        egressType = optimizer.getEgressType();
    }
    return retVal;
}
Also used : AccessorOptimizer(org.mvel2.optimizers.AccessorOptimizer) ParserContext(org.mvel2.ParserContext) OptimizationNotSupported(org.mvel2.optimizers.OptimizationNotSupported) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 73 with ParserConfiguration

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

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)

Example 74 with ParserConfiguration

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

the class CoreConfidenceTests method testStrTriangleEqualsEquals.

public void testStrTriangleEqualsEquals() {
    MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = true;
    try {
        ParserConfiguration pconf = new ParserConfiguration();
        ParserContext pctx = new ParserContext(pconf);
        pctx.addInput("this", Triangle.class);
        pctx.setStrongTyping(true);
        String str = "this.strLabel == this";
        try {
            ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
            fail("should have failed");
        } catch (CompileException e) {
            System.out.println();
            return;
        }
    } finally {
        MVEL.COMPILER_OPT_ALLOW_NAKED_METH_CALL = false;
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement)

Example 75 with ParserConfiguration

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

the class CoreConfidenceTests method testStrictModeAddAll.

public void testStrictModeAddAll() {
    String str = "list.addAll( o );";
    ParserConfiguration pconf = new ParserConfiguration();
    ParserContext pctx = new ParserContext(pconf);
    pctx.setStrongTyping(true);
    pctx.addInput("o", Object.class);
    pctx.addInput("list", ArrayList.class);
    try {
        ExecutableStatement stmt = (ExecutableStatement) MVEL.compileExpression(str, pctx);
        fail("This should not compileShared, as o is not of a type Collection");
    } catch (Exception e) {
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) IOException(java.io.IOException)

Aggregations

ParserConfiguration (org.mvel2.ParserConfiguration)95 ParserContext (org.mvel2.ParserContext)88 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)48 HashMap (java.util.HashMap)43 LinkedHashMap (java.util.LinkedHashMap)30 Map (java.util.Map)27 Serializable (java.io.Serializable)12 CompileException (org.mvel2.CompileException)9 MVEL.evalToBoolean (org.mvel2.MVEL.evalToBoolean)9 IOException (java.io.IOException)7 MapObject (org.mvel2.tests.core.res.MapObject)7 ParserConfiguration (org.mule.mvel2.ParserConfiguration)6 Method (java.lang.reflect.Method)5 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)5 MVELExpressionLanguageContext (org.mule.runtime.core.internal.el.mvel.MVELExpressionLanguageContext)5 RecognitionException (org.antlr.runtime.RecognitionException)4 PropertyAccessException (org.mvel2.PropertyAccessException)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3