Search in sources :

Example 1 with ParserContext

use of org.mvel2.ParserContext in project drools by kiegroup.

the class ResultVerifier method isSuccess.

protected Boolean isSuccess(VerifyField currentField) {
    String s = "__fact__." + currentField.getFieldName() + " " + currentField.getOperator() + " __expected__";
    CompiledExpression expression = new ExpressionCompiler(s, parserContext).compile();
    return (Boolean) MVELSafeHelper.getEvaluator().executeExpression(expression, variables);
}
Also used : ExpressionCompiler(org.mvel2.compiler.ExpressionCompiler) CompiledExpression(org.mvel2.compiler.CompiledExpression)

Example 2 with ParserContext

use of org.mvel2.ParserContext in project drools by kiegroup.

the class DefaultEnumClassBuilder method buildFields.

protected void buildFields(ClassWriter cw, EnumClassDefinition classDef) {
    FieldVisitor fv;
    for (FieldDefinition fld : classDef.getFieldsDefinitions()) {
        fv = cw.visitField(ACC_PRIVATE + ACC_FINAL, fld.getName(), BuildUtils.getTypeDescriptor(fld.getTypeName()), null, null);
        fv.visitEnd();
    }
    {
        fv = cw.visitField(ACC_PRIVATE + ACC_STATIC, "$context", "Lorg/mvel2/ParserContext;", null, null);
        fv.visitEnd();
    }
}
Also used : FieldVisitor(org.mvel2.asm.FieldVisitor)

Example 3 with ParserContext

use of org.mvel2.ParserContext in project drools by kiegroup.

the class DefaultEnumClassBuilder method buildGettersAndSetters.

protected void buildGettersAndSetters(ClassWriter cw, EnumClassDefinition classDef) {
    MethodVisitor mv;
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "values", "()[" + BuildUtils.getTypeDescriptor(classDef.getClassName()), null, null);
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, BuildUtils.getInternalType(classDef.getClassName()), "$VALUES", "[" + BuildUtils.getTypeDescriptor(classDef.getClassName()));
        mv.visitMethodInsn(INVOKEVIRTUAL, "[" + BuildUtils.getTypeDescriptor(classDef.getClassName()), "clone", "()Ljava/lang/Object;");
        mv.visitTypeInsn(CHECKCAST, "[" + BuildUtils.getTypeDescriptor(classDef.getClassName()));
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 0);
        mv.visitEnd();
    }
    for (FieldDefinition fld : classDef.getFieldsDefinitions()) {
        mv = cw.visitMethod(ACC_PUBLIC, BuildUtils.getterName(fld.getName(), fld.getTypeName()), "()" + BuildUtils.getTypeDescriptor(fld.getTypeName()), null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), fld.getName(), BuildUtils.getTypeDescriptor(fld.getTypeName()));
        mv.visitInsn(BuildUtils.returnType(fld.getTypeName()));
        mv.visitMaxs(BuildUtils.sizeOf(fld.getTypeName()), 1);
        mv.visitEnd();
        mv = cw.visitMethod(ACC_PUBLIC, BuildUtils.setterName(fld.getName(), fld.getTypeName()), "(" + BuildUtils.getTypeDescriptor(fld.getTypeName()) + ")V", null, null);
        mv.visitCode();
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 1 + BuildUtils.sizeOf(fld.getTypeName()));
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "get$context", "()Lorg/mvel2/ParserContext;", null, null);
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, BuildUtils.getInternalType(classDef.getName()), "$context", "Lorg/mvel2/ParserContext;");
        mv.visitInsn(ARETURN);
        mv.visitMaxs(1, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "set$context", "(Lorg/mvel2/ParserContext;)V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(PUTSTATIC, BuildUtils.getInternalType(classDef.getName()), "$context", "Lorg/mvel2/ParserContext;");
        mv.visitInsn(RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
    }
}
Also used : MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 4 with ParserContext

use of org.mvel2.ParserContext in project drools by kiegroup.

the class MVELTest method test1.

@Test
public void test1() {
    final ParserContext pc = new ParserContext();
    pc.addInput("x", String.class);
    pc.setStrongTyping(true);
    final Object o = MVEL.compileExpression("x.startsWith('d')", pc);
    final Map vars = new HashMap();
    vars.put("x", "d");
    MVEL.executeExpression(o, vars);
    System.out.println(o);
}
Also used : HashMap(java.util.HashMap) ParserContext(org.mvel2.ParserContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with ParserContext

use of org.mvel2.ParserContext in project drools by kiegroup.

the class QueryElementBuilder method getParserContext.

private ParserContext getParserContext(RuleBuildContext context) {
    MVELDialectRuntimeData data = (MVELDialectRuntimeData) context.getPkg().getDialectRuntimeRegistry().getDialectData("mvel");
    ParserConfiguration conf = data.getParserConfiguration();
    conf.setClassLoader(context.getKnowledgeBuilder().getRootClassLoader());
    return new ParserContext(conf);
}
Also used : MVELDialectRuntimeData(org.drools.core.rule.MVELDialectRuntimeData) ParserContext(org.mvel2.ParserContext) ParserConfiguration(org.mvel2.ParserConfiguration)

Aggregations

ParserContext (org.mvel2.ParserContext)372 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)190 HashMap (java.util.HashMap)140 Serializable (java.io.Serializable)100 ParserConfiguration (org.mvel2.ParserConfiguration)86 Map (java.util.Map)74 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)67 LinkedHashMap (java.util.LinkedHashMap)66 CompiledExpression (org.mvel2.compiler.CompiledExpression)63 CompileException (org.mvel2.CompileException)53 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)31 Foo (org.mvel2.tests.core.res.Foo)27 List (java.util.List)26 DefaultLocalVariableResolverFactory (org.mvel2.integration.impl.DefaultLocalVariableResolverFactory)24 MapObject (org.mvel2.tests.core.res.MapObject)23 ArrayList (java.util.ArrayList)22 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)19 IOException (java.io.IOException)16 MVEL.evalToBoolean (org.mvel2.MVEL.evalToBoolean)16 Debugger (org.mvel2.debug.Debugger)16