Search in sources :

Example 36 with ParserContext

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

the class NewObjectNode method getReducedValueAccelerated.

public Object getReducedValueAccelerated(Object ctx, Object thisValue, VariableResolverFactory factory) {
    if (newObjectOptimizer == null) {
        if (egressType == null) {
            if (factory != null && factory.isResolveable(typeDescr.getClassName())) {
                try {
                    egressType = (Class) factory.getVariableResolver(typeDescr.getClassName()).getValue();
                    rewriteClassReferenceToFQCN(COMPILE_IMMEDIATE);
                    if (typeDescr.isArray()) {
                        try {
                            egressType = findClass(factory, repeatChar('[', typeDescr.getArrayLength()) + "L" + egressType.getName() + ";", pCtx);
                        } catch (Exception e) {
                        // for now, don't handle this.
                        }
                    }
                } catch (ClassCastException e) {
                    throw new CompileException("cannot construct object: " + typeDescr.getClassName() + " is not a class reference", expr, start, e);
                }
            }
        }
        if (typeDescr.isArray()) {
            return (newObjectOptimizer = new NewObjectArray(getBaseComponentType(egressType.getComponentType()), typeDescr.getCompiledArraySize())).getValue(ctx, thisValue, factory);
        }
        try {
            AccessorOptimizer optimizer = getThreadAccessorOptimizer();
            ParserContext pCtx = this.pCtx;
            if (pCtx == null) {
                pCtx = new ParserContext();
                pCtx.getParserConfiguration().setAllImports(getInjectedImports(factory));
            }
            newObjectOptimizer = optimizer.optimizeObjectCreation(pCtx, name, 0, name.length, ctx, thisValue, factory);
            /**
             * Check to see if the optimizer actually produced the object during optimization.  If so,
             * we return that value now.
             */
            if (optimizer.getResultOptPass() != null) {
                egressType = optimizer.getEgressType();
                return optimizer.getResultOptPass();
            }
        } catch (CompileException e) {
            throw ErrorUtil.rewriteIfNeeded(e, expr, start);
        } finally {
            OptimizerFactory.clearThreadAccessorOptimizer();
        }
    }
    return newObjectOptimizer.getValue(ctx, thisValue, factory);
}
Also used : AccessorOptimizer(org.mvel2.optimizers.AccessorOptimizer) OptimizerFactory.getThreadAccessorOptimizer(org.mvel2.optimizers.OptimizerFactory.getThreadAccessorOptimizer) CompileException(org.mvel2.CompileException) ParserContext(org.mvel2.ParserContext) CompileException(org.mvel2.CompileException)

Example 37 with ParserContext

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

the class CoreConfidenceTests method testParserErrorHandling.

public void testParserErrorHandling() {
    final ParserContext ctx = new ParserContext();
    ExpressionCompiler compiler = new ExpressionCompiler("a[", ctx);
    try {
        compiler.compile();
    } catch (Exception e) {
        return;
    }
    assertTrue(false);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) ParserContext(org.mvel2.ParserContext) CompileException(org.mvel2.CompileException) IOException(java.io.IOException)

Example 38 with ParserContext

use of org.mule.mvel2.ParserContext 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 39 with ParserContext

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

the class CoreConfidenceTests method testFieldNameWithUnderscore.

public void testFieldNameWithUnderscore() {
    final ParserContext parserContext = new ParserContext();
    parserContext.setStrictTypeEnforcement(true);
    parserContext.setStrongTyping(true);
    parserContext.addInput("this", Underscore.class);
    Underscore underscore = new Underscore();
    assertEquals(true, MVEL.executeExpression(MVEL.compileExpression("_id == \"test\"", parserContext), underscore));
}
Also used : ParserContext(org.mvel2.ParserContext)

Example 40 with ParserContext

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

the class CoreConfidenceTests method testRegExWithCast.

public void testRegExWithCast() {
    final ParserContext parserContext = new ParserContext();
    parserContext.setStrongTyping(true);
    parserContext.addInput("this", Foo.class);
    assertEquals(Boolean.class, MVEL.analyze("(String)bar.name ~= '[a-z].+'", parserContext));
}
Also used : ParserContext(org.mvel2.ParserContext)

Aggregations

ParserContext (org.mvel2.ParserContext)340 HashMap (java.util.HashMap)128 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)119 Serializable (java.io.Serializable)82 ParserConfiguration (org.mvel2.ParserConfiguration)70 Map (java.util.Map)64 LinkedHashMap (java.util.LinkedHashMap)62 CompiledExpression (org.mvel2.compiler.CompiledExpression)48 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)42 CompileException (org.mvel2.CompileException)37 Foo (org.mvel2.tests.core.res.Foo)24 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)23 ArrayList (java.util.ArrayList)20 List (java.util.List)19 MapObject (org.mvel2.tests.core.res.MapObject)18 Debugger (org.mvel2.debug.Debugger)15 Frame (org.mvel2.debug.Frame)15 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)14 HashSet (java.util.HashSet)12 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)10