Search in sources :

Example 71 with VariableResolverFactory

use of org.mvel2.integration.VariableResolverFactory in project mvel by mikebrock.

the class ASMAccessorOptimizer method _initJIT.

/**
 * Does all the boilerplate for initiating the JIT.
 */
private void _initJIT() {
    if (isAdvancedDebugging()) {
        buildLog = new StringAppender();
    }
    cw = new ClassWriter(ClassWriter.COMPUTE_MAXS + ClassWriter.COMPUTE_FRAMES);
    synchronized (Runtime.getRuntime()) {
        cw.visit(OPCODES_VERSION, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, className = "ASMAccessorImpl_" + valueOf(cw.hashCode()).replaceAll("\\-", "_") + (System.currentTimeMillis() / 10) + ((int) Math.random() * 100), null, "java/lang/Object", new String[] { NAMESPACE + "compiler/Accessor" });
    }
    MethodVisitor m = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
    m.visitCode();
    m.visitVarInsn(Opcodes.ALOAD, 0);
    m.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
    m.visitInsn(RETURN);
    m.visitMaxs(1, 1);
    m.visitEnd();
    (mv = cw.visitMethod(ACC_PUBLIC, "getValue", "(Ljava/lang/Object;Ljava/lang/Object;L" + NAMESPACE + "integration/VariableResolverFactory;)Ljava/lang/Object;", null, null)).visitCode();
}
Also used : ClassWriter(org.mvel2.asm.ClassWriter) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 72 with VariableResolverFactory

use of org.mvel2.integration.VariableResolverFactory in project mvel by mikebrock.

the class ASMAccessorOptimizer method writeOutNullHandler.

private void writeOutNullHandler(Member member, int type) {
    assert debug("DUP");
    mv.visitInsn(DUP);
    Label j = new Label();
    assert debug("IFNONNULL : jump");
    mv.visitJumpInsn(IFNONNULL, j);
    assert debug("POP");
    mv.visitInsn(POP);
    assert debug("ALOAD 0");
    mv.visitVarInsn(ALOAD, 0);
    if (type == 0) {
        this.propNull = true;
        assert debug("GETFIELD 'nullPropertyHandler'");
        mv.visitFieldInsn(GETFIELD, className, "nullPropertyHandler", "L" + NAMESPACE + "integration/PropertyHandler;");
    } else {
        this.methNull = true;
        assert debug("GETFIELD 'nullMethodHandler'");
        mv.visitFieldInsn(GETFIELD, className, "nullMethodHandler", "L" + NAMESPACE + "integration/PropertyHandler;");
    }
    assert debug("LDC '" + member.getName() + "'");
    mv.visitLdcInsn(member.getName());
    assert debug("ALOAD 1");
    mv.visitVarInsn(ALOAD, 1);
    assert debug("ALOAD 3");
    mv.visitVarInsn(ALOAD, 3);
    assert debug("INVOKEINTERFACE PropertyHandler.getProperty");
    mv.visitMethodInsn(INVOKEINTERFACE, NAMESPACE + "integration/PropertyHandler", "getProperty", "(Ljava/lang/String;Ljava/lang/Object;L" + NAMESPACE + "integration/VariableResolverFactory;)Ljava/lang/Object;");
    assert debug("LABEL:jump");
    mv.visitLabel(j);
}
Also used : Label(org.mvel2.asm.Label)

Example 73 with VariableResolverFactory

use of org.mvel2.integration.VariableResolverFactory in project mvel by mikebrock.

the class ReflectiveAccessorOptimizer method compileConstructor.

@SuppressWarnings({ "WeakerAccess" })
public AccessorNode compileConstructor(char[] expression, Object ctx, VariableResolverFactory vars) throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, NoSuchMethodException {
    String[] cnsRes = captureContructorAndResidual(expression, start, length);
    List<char[]> constructorParms = parseMethodOrConstructor(cnsRes[0].toCharArray());
    if (constructorParms != null) {
        String s = new String(subset(expression, 0, ArrayTools.findFirst('(', start, length, expression)));
        Class cls = ParseTools.findClass(vars, s, pCtx);
        ExecutableStatement[] cStmts = new ExecutableStatement[constructorParms.size()];
        for (int i = 0; i < constructorParms.size(); i++) {
            cStmts[i] = (ExecutableStatement) subCompileExpression(constructorParms.get(i), pCtx);
        }
        Object[] parms = new Object[constructorParms.size()];
        for (int i = 0; i < constructorParms.size(); i++) {
            parms[i] = cStmts[i].getValue(ctx, vars);
        }
        Constructor cns = getBestConstructorCandidate(parms, cls, pCtx.isStrongTyping());
        if (cns == null) {
            StringBuilder error = new StringBuilder();
            for (int i = 0; i < parms.length; i++) {
                error.append(parms[i].getClass().getName());
                if (i + 1 < parms.length)
                    error.append(", ");
            }
            throw new CompileException("unable to find constructor: " + cls.getName() + "(" + error.toString() + ")", this.expr, this.start);
        }
        for (int i = 0; i < parms.length; i++) {
            // noinspection unchecked
            parms[i] = convert(parms[i], cns.getParameterTypes()[i]);
        }
        AccessorNode ca = new ConstructorAccessor(cns, cStmts);
        if (cnsRes.length > 1) {
            ReflectiveAccessorOptimizer compiledOptimizer = new ReflectiveAccessorOptimizer(pCtx, cnsRes[1].toCharArray(), 0, cnsRes[1].length(), cns.newInstance(parms), ctx, vars);
            compiledOptimizer.ingressType = cns.getDeclaringClass();
            compiledOptimizer.setRootNode(ca);
            compiledOptimizer.compileGetChain();
            ca = compiledOptimizer.getRootNode();
            this.val = compiledOptimizer.getResultOptPass();
        }
        return ca;
    } else {
        Constructor<?> cns = Class.forName(new String(expression), true, Thread.currentThread().getContextClassLoader()).getConstructor(EMPTYCLS);
        AccessorNode ca = new ConstructorAccessor(cns, null);
        if (cnsRes.length > 1) {
            // noinspection NullArgumentToVariableArgMethod
            ReflectiveAccessorOptimizer compiledOptimizer = new ReflectiveAccessorOptimizer(getCurrentThreadParserContext(), cnsRes[1].toCharArray(), 0, cnsRes[1].length(), cns.newInstance(null), ctx, vars);
            compiledOptimizer.setRootNode(ca);
            compiledOptimizer.compileGetChain();
            ca = compiledOptimizer.getRootNode();
            this.val = compiledOptimizer.getResultOptPass();
        }
        return ca;
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) AccessorNode(org.mvel2.compiler.AccessorNode)

Example 74 with VariableResolverFactory

use of org.mvel2.integration.VariableResolverFactory in project mvel by mikebrock.

the class DynamicFunctionAccessor method getValue.

public Object getValue(Object ctx, Object elCtx, VariableResolverFactory variableFactory) {
    Object[] parms = null;
    Function function = (Function) ctx;
    if (parameters != null && parameters.length != 0) {
        parms = new Object[parameters.length];
        for (int i = 0; i < parms.length; i++) {
            parms[i] = parameters[i].getValue(ctx, elCtx, variableFactory);
        }
    }
    if (nextNode != null) {
        return nextNode.getValue(function.call(ctx, elCtx, variableFactory, parms), elCtx, variableFactory);
    } else {
        return function.call(ctx, elCtx, variableFactory, parms);
    }
}
Also used : Function(org.mvel2.ast.Function)

Example 75 with VariableResolverFactory

use of org.mvel2.integration.VariableResolverFactory in project mvel by mikebrock.

the class BaseVariableResolverFactory method appendFactory.

public void appendFactory(VariableResolverFactory resolverFactory) {
    if (nextFactory == null) {
        nextFactory = resolverFactory;
    } else {
        VariableResolverFactory vrf = nextFactory;
        while (vrf.getNextFactory() != null) {
            vrf = vrf.getNextFactory();
        }
        vrf.setNextFactory(nextFactory);
    }
}
Also used : VariableResolverFactory(org.mvel2.integration.VariableResolverFactory)

Aggregations

VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)79 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)41 HashMap (java.util.HashMap)33 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)33 VariableResolver (org.mvel2.integration.VariableResolver)32 ParserContext (org.mvel2.ParserContext)24 Serializable (java.io.Serializable)23 CompileException (org.mvel2.CompileException)23 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)14 AccessorOptimizer (org.mvel2.optimizers.AccessorOptimizer)14 Map (java.util.Map)13 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)12 IndexedVariableResolverFactory (org.mvel2.integration.impl.IndexedVariableResolverFactory)12 List (java.util.List)11 CompiledExpression (org.mvel2.compiler.CompiledExpression)11 ASTNode (org.mvel2.ast.ASTNode)10 Foo (org.mvel2.tests.core.res.Foo)10 MapObject (org.mvel2.tests.core.res.MapObject)10 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9