Search in sources :

Example 56 with Type

use of org.mvel2.asm.Type in project drools by kiegroup.

the class MVELBeanCreator method createBean.

@Override
public <T> T createBean(ClassLoader cl, String type, QualifierModel qualifier) throws Exception {
    if (qualifier != null) {
        throw new IllegalArgumentException("Cannot use a qualifier without a CDI container");
    }
    ParserConfiguration config = new ParserConfiguration();
    config.setClassLoader(cl);
    ParserContext ctx = new ParserContext(config);
    if (parameters != null) {
        for (Map.Entry<String, Object> entry : parameters.entrySet()) {
            ctx.addVariable(entry.getKey(), entry.getValue().getClass());
        }
    }
    Object compiledExpression = MVEL.compileExpression(type, ctx);
    return (T) MVELSafeHelper.getEvaluator().executeExpression(compiledExpression, parameters);
}
Also used : ParserContext(org.mvel2.ParserContext) Map(java.util.Map) ParserConfiguration(org.mvel2.ParserConfiguration)

Example 57 with Type

use of org.mvel2.asm.Type in project drools by kiegroup.

the class TraitClassBuilderImpl method buildGetter.

protected void buildGetter(ClassWriter cw, FieldDefinition field, String name, String type, String generic) {
    name = name.substring(0, 1).toUpperCase() + name.substring(1);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, BuildUtils.getterName(name, type), "()" + BuildUtils.getTypeDescriptor(type), generic == null ? null : "()" + BuildUtils.getTypeDescriptor(type).replace(";", "<" + BuildUtils.getTypeDescriptor(generic) + ">;"), null);
    mv.visitEnd();
}
Also used : MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 58 with Type

use of org.mvel2.asm.Type in project drools by kiegroup.

the class TraitCoreWrapperClassBuilderImpl method initializeDynamicTypeStructures.

protected void initializeDynamicTypeStructures(MethodVisitor mv, String wrapperName, ClassDefinition coreDef) {
    if (coreDef.isFullTraiting()) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, Type.getInternalName(TraitFieldTMSImpl.class));
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TraitFieldTMSImpl.class), "<init>", "()V");
        mv.visitFieldInsn(PUTFIELD, BuildUtils.getInternalType(wrapperName), TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class));
        for (FactField hardField : coreDef.getFields()) {
            FieldDefinition fld = (FieldDefinition) hardField;
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(wrapperName), TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class));
            mv.visitLdcInsn(Type.getType(Type.getDescriptor(coreDef.getDefinedClass())));
            mv.visitLdcInsn(fld.resolveAlias());
            if (BuildUtils.isPrimitive(fld.getTypeName())) {
                // mv.visitFieldInsn( GETSTATIC, BuildUtils.getInternalType( BuildUtils.box( fld.getTypeName() ) ), "TYPE", Type.getDescriptor( Class.class ) );
                mv.visitLdcInsn(Type.getType(BuildUtils.getTypeDescriptor(BuildUtils.box(fld.getTypeName()))));
            } else {
                mv.visitLdcInsn(Type.getType(BuildUtils.getTypeDescriptor(fld.getTypeName())));
            }
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(wrapperName), "core", Type.getDescriptor(coreDef.getDefinedClass()));
            mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(coreDef.getDefinedClass()), BuildUtils.getterName(fld.getName(), fld.getTypeName()), "()" + BuildUtils.getTypeDescriptor(fld.getTypeName()));
            if (BuildUtils.isPrimitive(fld.getTypeName())) {
                mv.visitMethodInsn(INVOKESTATIC, BuildUtils.getInternalType(BuildUtils.box(fld.getTypeName())), "valueOf", "(" + BuildUtils.getTypeDescriptor(fld.getTypeName()) + ")" + BuildUtils.getTypeDescriptor(BuildUtils.box(fld.getTypeName())));
            }
            if (fld.getInitExpr() != null) {
                mv.visitLdcInsn(fld.getInitExpr());
            } else {
                mv.visitInsn(ACONST_NULL);
            }
            mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(TraitFieldTMS.class), "registerField", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(Class.class), Type.getType(String.class), Type.getType(Class.class), Type.getType(Object.class), Type.getType(String.class) }));
        }
    }
}
Also used : FactField(org.kie.api.definition.type.FactField) Type(org.mvel2.asm.Type) FieldDefinition(org.drools.core.factmodel.FieldDefinition)

Example 59 with Type

use of org.mvel2.asm.Type in project drools by kiegroup.

the class TraitMapProxyClassBuilderImpl method buildSoftGetter.

protected void buildSoftGetter(ClassVisitor cw, FieldDefinition field, String proxy, String getterName, int accessLevel) {
    String type = field.getTypeName();
    MethodVisitor mv = cw.visitMethod(accessLevel, getterName, "()" + BuildUtils.getTypeDescriptor(type), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(proxy), "map", Type.getDescriptor(Map.class));
    mv.visitLdcInsn(field.resolveAlias());
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "get", "(" + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Object.class), true);
    String actualType = BuildUtils.isPrimitive(type) ? BuildUtils.box(type) : type;
    mv.visitVarInsn(ASTORE, 1);
    mv.visitVarInsn(ALOAD, 1);
    Label l0 = new Label();
    mv.visitJumpInsn(IFNULL, l0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(INSTANCEOF, BuildUtils.getInternalType(actualType));
    mv.visitJumpInsn(IFEQ, l0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, BuildUtils.getInternalType(actualType));
    if (BuildUtils.isPrimitive(type)) {
        TraitFactory.primitiveValue(mv, type);
        mv.visitInsn(BuildUtils.returnType(type));
        mv.visitLabel(l0);
        mv.visitInsn(BuildUtils.zero(type));
        mv.visitInsn(BuildUtils.returnType(type));
    } else {
        mv.visitInsn(ARETURN);
        mv.visitLabel(l0);
        mv.visitInsn(ACONST_NULL);
        mv.visitInsn(ARETURN);
    }
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Label(org.mvel2.asm.Label) Map(java.util.Map) ExternalizableLinkedHashMap(org.drools.core.util.ExternalizableLinkedHashMap) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 60 with Type

use of org.mvel2.asm.Type in project drools by kiegroup.

the class TraitTripleProxyClassBuilderImpl method buildSoftGetter.

protected void buildSoftGetter(ClassVisitor cw, FieldDefinition field, String proxy, String getterName, int accessMode) {
    String type = field.getTypeName();
    MethodVisitor mv = cw.visitMethod(accessMode, getterName, "()" + Type.getDescriptor(field.getType()), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(proxy), "store", Type.getDescriptor(TripleStore.class));
    mv.visitVarInsn(ALOAD, 0);
    mv.visitLdcInsn(field.resolveAlias());
    mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(proxy), "propertyKey", "(" + Type.getDescriptor(String.class) + ")" + Type.getDescriptor(Triple.class), false);
    mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TripleStore.class), "get", "(" + Type.getDescriptor(Triple.class) + ")" + Type.getDescriptor(Triple.class), false);
    String actualType = BuildUtils.isPrimitive(type) ? BuildUtils.box(type) : type;
    mv.visitVarInsn(ASTORE, 1);
    mv.visitVarInsn(ALOAD, 1);
    Label l0 = new Label();
    mv.visitJumpInsn(IFNULL, l0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Triple.class), "getValue", "()" + Type.getDescriptor(Object.class), true);
    mv.visitVarInsn(ASTORE, 2);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitTypeInsn(INSTANCEOF, BuildUtils.getInternalType(actualType));
    Label l1 = new Label();
    mv.visitJumpInsn(IFEQ, l1);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitTypeInsn(CHECKCAST, BuildUtils.getInternalType(actualType));
    if (BuildUtils.isPrimitive(type)) {
        TraitFactory.primitiveValue(mv, type);
        mv.visitInsn(BuildUtils.returnType(type));
        mv.visitLabel(l1);
        mv.visitInsn(BuildUtils.zero(type));
        mv.visitInsn(BuildUtils.returnType(type));
        mv.visitLabel(l0);
        mv.visitInsn(BuildUtils.zero(type));
        mv.visitInsn(BuildUtils.returnType(type));
    } else {
        mv.visitInsn(ARETURN);
        mv.visitLabel(l1);
        mv.visitInsn(ACONST_NULL);
        mv.visitInsn(ARETURN);
        mv.visitLabel(l0);
        mv.visitInsn(ACONST_NULL);
        mv.visitInsn(ARETURN);
    }
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Triple(org.drools.core.util.Triple) TripleStore(org.drools.core.util.TripleStore) Label(org.mvel2.asm.Label) MethodVisitor(org.mvel2.asm.MethodVisitor)

Aggregations

MethodVisitor (org.mvel2.asm.MethodVisitor)19 Map (java.util.Map)15 Label (org.mvel2.asm.Label)13 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)12 List (java.util.List)11 Type (org.mvel2.asm.Type)10 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 CompileException (org.mvel2.CompileException)7 ParserContext (org.mvel2.ParserContext)7 HashMap (java.util.HashMap)6 TypeDescriptor (org.mvel2.ast.TypeDescriptor)6 WeakHashMap (java.util.WeakHashMap)4 WorkingMemory (org.drools.core.WorkingMemory)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 Tuple (org.drools.core.spi.Tuple)4 FieldVisitor (org.mvel2.asm.FieldVisitor)4 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)4 LeftTuple (org.drools.core.reteoo.LeftTuple)3 DeclarationMatcher (org.drools.core.rule.builder.dialect.asm.GeneratorHelper.DeclarationMatcher)3