Search in sources :

Example 61 with Type

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

the class TraitTripleProxyClassBuilderImpl method buildSoftSetter.

protected void buildSoftSetter(ClassVisitor cw, FieldDefinition field, String proxy, ClassDefinition core, String setterName, int accessMode) {
    String type = field.getTypeName();
    MethodVisitor mv = cw.visitMethod(accessMode, setterName, "(" + Type.getDescriptor(field.getType()) + ")V", null, null);
    mv.visitCode();
    if (core.isFullTraiting()) {
        logicalSetter(mv, field, proxy, core);
    }
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(proxy), "store", Type.getDescriptor(TripleStore.class));
    mv.visitVarInsn(ALOAD, 0);
    mv.visitLdcInsn(field.resolveAlias());
    mv.visitVarInsn(BuildUtils.varType(type), 1);
    if (BuildUtils.isPrimitive(type)) {
        TraitFactory.valueOf(mv, type);
    }
    mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(proxy), "property", "(" + Type.getDescriptor(String.class) + Type.getDescriptor(Object.class) + ")" + Type.getDescriptor(Triple.class), false);
    mv.visitMethodInsn(INVOKEVIRTUAL, Type.getInternalName(TripleStore.class), "put", "(" + Type.getDescriptor(Triple.class) + ")Z", false);
    mv.visitInsn(POP);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Triple(org.drools.core.util.Triple) TripleStore(org.drools.core.util.TripleStore) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 62 with Type

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

the class DefaultBeanClassBuilder method buildDefaultConstructor.

/**
 * Creates a default constructor for the class
 *
 * @param cw
 */
protected void buildDefaultConstructor(ClassVisitor cw, ClassDefinition classDef) {
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}), null, null);
    mv.visitCode();
    Label l0 = null;
    if (this.debug) {
        l0 = new Label();
        mv.visitLabel(l0);
    }
    boolean hasObjects = defaultConstructorStart(mv, classDef);
    mv.visitInsn(Opcodes.RETURN);
    Label l1 = null;
    if (this.debug) {
        l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", BuildUtils.getTypeDescriptor(classDef.getClassName()), null, l0, l1, 0);
    }
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Type(org.mvel2.asm.Type) Label(org.mvel2.asm.Label) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 63 with Type

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

the class DefaultBeanClassBuilder method fieldConstructorStart.

protected void fieldConstructorStart(MethodVisitor mv, ClassDefinition classDef, Collection<FieldDefinition> fieldDefs) {
    mv.visitVarInsn(Opcodes.ALOAD, 0);
    String sup = "";
    try {
        sup = Type.getInternalName(Class.forName(classDef.getSuperClass()));
    } catch (ClassNotFoundException e) {
        sup = BuildUtils.getInternalType(classDef.getSuperClass());
    }
    mv.visitMethodInsn(Opcodes.INVOKESPECIAL, sup, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] {}));
    // local vars start at 1, as 0 is "this"
    int index = 1;
    for (FieldDefinition field : fieldDefs) {
        if (this.debug) {
            Label l11 = new Label();
            mv.visitLabel(l11);
        }
        mv.visitVarInsn(Opcodes.ALOAD, 0);
        mv.visitVarInsn(Type.getType(BuildUtils.getTypeDescriptor(field.getTypeName())).getOpcode(Opcodes.ILOAD), index++);
        if (field.getTypeName().equals("long") || field.getTypeName().equals("double")) {
            // long and double variables use 2 words on the variables table
            index++;
        }
        if (!field.isInherited()) {
            mv.visitFieldInsn(Opcodes.PUTFIELD, BuildUtils.getInternalType(classDef.getClassName()), field.getName(), BuildUtils.getTypeDescriptor(field.getTypeName()));
        } else {
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getClassName()), field.getWriteMethod(), Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(BuildUtils.getTypeDescriptor(field.getTypeName())) }));
        }
    }
    for (FieldDefinition field : classDef.getFieldsDefinitions()) {
        if (!fieldDefs.contains(field) && field.getInitExpr() != null && !"".equals(field.getInitExpr().trim())) {
            initFieldWithDefaultValue(mv, classDef, field);
        }
    }
    if (classDef.isTraitable()) {
        initializeDynamicTypeStructures(mv, classDef);
    }
}
Also used : Type(org.mvel2.asm.Type) Label(org.mvel2.asm.Label)

Example 64 with Type

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

the class DefaultBeanClassBuilder method buildFieldTMS.

/**
 * A traitable class is a special class with support for dynamic properties and types.
 *
 * A traitable class in logical mode provides additional control over the values
 * and type(s) of its fields.
 *
 * @param cw
 * @param def
 */
protected void buildFieldTMS(ClassWriter cw, ClassDefinition def) {
    FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class), null, null);
    fv.visitEnd();
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "_getFieldTMS", Type.getMethodDescriptor(Type.getType(TraitFieldTMS.class), new Type[] {}), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(def.getName()), TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "_setFieldTMS", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(TraitFieldTMS.class) }), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(PUTFIELD, BuildUtils.getInternalType(def.getName()), TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class));
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Type(org.mvel2.asm.Type) TraitFieldTMS(org.drools.core.factmodel.traits.TraitFieldTMS) FieldVisitor(org.mvel2.asm.FieldVisitor) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 65 with Type

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

the class DefaultBeanClassBuilder method buildConstructorWithFields.

/**
 * Creates a constructor that takes and assigns values to all
 * fields in the order they are declared.
 *
 * @param cw
 * @param classDef
 */
protected void buildConstructorWithFields(ClassVisitor cw, ClassDefinition classDef, Collection<FieldDefinition> fieldDefs) {
    Type[] params = new Type[fieldDefs.size()];
    int index = 0;
    for (FieldDefinition field : fieldDefs) {
        params[index++] = Type.getType(BuildUtils.getTypeDescriptor(field.getTypeName()));
    }
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, params), null, null);
    mv.visitCode();
    Label l0 = null;
    if (this.debug) {
        l0 = new Label();
        mv.visitLabel(l0);
    }
    fieldConstructorStart(mv, classDef, fieldDefs);
    mv.visitInsn(Opcodes.RETURN);
    Label l1 = null;
    if (this.debug) {
        l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLocalVariable("this", BuildUtils.getTypeDescriptor(classDef.getClassName()), null, l0, l1, 0);
        for (FieldDefinition field : classDef.getFieldsDefinitions()) {
            Label l11 = new Label();
            mv.visitLabel(l11);
            mv.visitLocalVariable(field.getName(), BuildUtils.getTypeDescriptor(field.getTypeName()), null, l0, l1, 0);
        }
    }
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Type(org.mvel2.asm.Type) 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