Search in sources :

Example 66 with Type

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

the class DefaultBeanClassBuilder method initializeDynamicTypeStructures.

/**
 * Initializes the trait map and dynamic property map to empty values
 * @param mv
 * @param classDef
 */
protected void initializeDynamicTypeStructures(MethodVisitor mv, ClassDefinition classDef) {
    if (classDef.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(classDef.getClassName()), TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class));
        for (FactField hardField : classDef.getFields()) {
            FieldDefinition fld = (FieldDefinition) hardField;
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getClassName()), TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class));
            mv.visitLdcInsn(Type.getType(BuildUtils.getTypeDescriptor(classDef.getClassName())));
            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.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getClassName()), 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 : TraitFieldTMSImpl(org.drools.core.factmodel.traits.TraitFieldTMSImpl) FactField(org.kie.api.definition.type.FactField) Type(org.mvel2.asm.Type) TraitFieldTMS(org.drools.core.factmodel.traits.TraitFieldTMS)

Example 67 with Type

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

the class DefaultBeanClassBuilder method buildDynamicPropertyMap.

/**
 * A traitable class is a special class with support for dynamic properties and types.
 *
 * This method builds the property map, containing the key/values pairs to implement
 * any property defined in a trait interface but not supported by the traited class
 * fields.
 *
 * @param cw
 * @param def
 */
protected void buildDynamicPropertyMap(ClassWriter cw, ClassDefinition def) {
    FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE, TraitableBean.MAP_FIELD_NAME, Type.getDescriptor(Map.class), "Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;", null);
    fv.visitEnd();
    MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "_getDynamicProperties", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}), "()Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;", null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(def.getName()), TraitableBean.MAP_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "_setDynamicProperties", Type.getMethodDescriptor(Type.getType(void.class), new Type[] { Type.getType(Map.class) }), "(Ljava/util/Map<Ljava/lang/String;Ljava/lang/Object;>;)V", null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(PUTFIELD, BuildUtils.getInternalType(def.getName()), TraitableBean.MAP_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Type(org.mvel2.asm.Type) FieldVisitor(org.mvel2.asm.FieldVisitor) Map(java.util.Map) TraitTypeMap(org.drools.core.factmodel.traits.TraitTypeMap) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 68 with Type

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

the class AbstractProxyClassBuilderImpl method logicalSetter.

protected void logicalSetter(MethodVisitor mv, FieldDefinition field, String masterName, ClassDefinition core) {
    String fieldType = field.getTypeName();
    int reg = 1 + BuildUtils.sizeOf(fieldType);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(masterName), "object", Type.getDescriptor(core.getDefinedClass()));
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TraitableBean.class));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(TraitableBean.class), "_getFieldTMS", Type.getMethodDescriptor(Type.getType(TraitFieldTMS.class)), true);
    mv.visitVarInsn(ASTORE, reg);
    mv.visitVarInsn(ALOAD, reg);
    mv.visitLdcInsn(field.resolveAlias());
    if (BuildUtils.isPrimitive(fieldType)) {
        mv.visitVarInsn(BuildUtils.varType(fieldType), 1);
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, BuildUtils.getInternalType(BuildUtils.box(fieldType)), "valueOf", Type.getMethodDescriptor(Type.getType(BuildUtils.getTypeDescriptor(BuildUtils.box(fieldType))), Type.getType(BuildUtils.getTypeDescriptor(fieldType))), false);
    } else {
        mv.visitVarInsn(ALOAD, 1);
    }
    if (BuildUtils.isPrimitive(fieldType)) {
        // mv.visitFieldInsn( GETSTATIC, BuildUtils.getInternalType( BuildUtils.box( fieldType ) ), "TYPE", Type.getDescriptor( Class.class ) );
        mv.visitLdcInsn(Type.getType(BuildUtils.getTypeDescriptor(BuildUtils.box(field.getTypeName()))));
    } else {
        mv.visitLdcInsn(Type.getType(Type.getDescriptor(field.getType())));
    }
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(TraitFieldTMS.class), "set", Type.getMethodDescriptor(Type.getType(Object.class), Type.getType(String.class), Type.getType(Object.class), Type.getType(Class.class)), true);
    mv.visitVarInsn(ASTORE, 1);
    mv.visitVarInsn(ALOAD, 1);
    if (BuildUtils.isPrimitive(fieldType)) {
        Label l0 = new Label();
        mv.visitJumpInsn(IFNULL, l0);
        mv.visitVarInsn(ALOAD, 1);
        Label l1 = new Label();
        mv.visitJumpInsn(GOTO, l1);
        mv.visitLabel(l0);
        mv.visitInsn(BuildUtils.zero(fieldType));
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, BuildUtils.getInternalType(BuildUtils.box(fieldType)), "valueOf", Type.getMethodDescriptor(Type.getType(BuildUtils.getTypeDescriptor(BuildUtils.box(fieldType))), Type.getType(BuildUtils.getTypeDescriptor(fieldType))), false);
        mv.visitLabel(l1);
        mv.visitTypeInsn(CHECKCAST, BuildUtils.getInternalType(BuildUtils.box(fieldType)));
        mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(BuildUtils.box(fieldType)), BuildUtils.numericMorph(BuildUtils.box(fieldType)), Type.getMethodDescriptor(Type.getType(field.getType())), false);
        mv.visitVarInsn(BuildUtils.storeType(fieldType), 1);
    }
}
Also used : Label(org.mvel2.asm.Label)

Example 69 with Type

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

the class AbstractProxyClassBuilderImpl method buildLogicalGetter.

protected void buildLogicalGetter(ClassVisitor cw, FieldDefinition field, String proxy, ClassDefinition core) {
    String fieldName = field.getName();
    String fieldType = field.getTypeName();
    String getter = BuildUtils.getterName(fieldName, fieldType);
    MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, getter, "()" + BuildUtils.getTypeDescriptor(fieldType), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(proxy), "object", Type.getDescriptor(core.getDefinedClass()));
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(TraitableBean.class));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(TraitableBean.class), "_getFieldTMS", Type.getMethodDescriptor(Type.getType(TraitFieldTMS.class)), true);
    mv.visitLdcInsn(field.resolveAlias());
    if (BuildUtils.isPrimitive(fieldType)) {
        // mv.visitFieldInsn( GETSTATIC, BuildUtils.getInternalType( BuildUtils.box( fieldType ) ), "TYPE", Type.getDescriptor( Class.class ) );
        mv.visitLdcInsn(Type.getType(BuildUtils.getTypeDescriptor(BuildUtils.box(field.getTypeName()))));
    } else {
        mv.visitLdcInsn(Type.getType(Type.getDescriptor(field.getType())));
    }
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(TraitFieldTMS.class), "get", Type.getMethodDescriptor(Type.getType(Object.class), Type.getType(String.class), Type.getType(Class.class)), true);
    mv.visitVarInsn(ASTORE, 1);
    mv.visitVarInsn(ALOAD, 1);
    if (BuildUtils.isPrimitive(fieldType)) {
        Label l0 = new Label();
        mv.visitJumpInsn(IFNULL, l0);
        mv.visitVarInsn(ALOAD, 1);
        Label l1 = new Label();
        mv.visitJumpInsn(GOTO, l1);
        mv.visitLabel(l0);
        mv.visitInsn(BuildUtils.zero(fieldType));
        mv.visitMethodInsn(Opcodes.INVOKESTATIC, BuildUtils.getInternalType(BuildUtils.box(fieldType)), "valueOf", Type.getMethodDescriptor(Type.getType(BuildUtils.getTypeDescriptor(BuildUtils.box(fieldType))), Type.getType(BuildUtils.getTypeDescriptor(fieldType))), false);
        mv.visitLabel(l1);
        mv.visitTypeInsn(CHECKCAST, BuildUtils.getInternalType(BuildUtils.box(fieldType)));
        mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(BuildUtils.box(fieldType)), BuildUtils.numericMorph(BuildUtils.box(fieldType)), Type.getMethodDescriptor(Type.getType(field.getType())), false);
        mv.visitInsn(BuildUtils.returnType(fieldType));
    } else {
        mv.visitTypeInsn(CHECKCAST, BuildUtils.getInternalType(fieldType));
        mv.visitInsn(ARETURN);
    }
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Label(org.mvel2.asm.Label) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 70 with Type

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

the class JavaFunctionBuilder method build.

/* (non-Javadoc)
     * @see org.kie.rule.builder.dialect.java.JavaFunctionBuilder#build(org.kie.rule.Package, org.kie.lang.descr.FunctionDescr, org.codehaus.jfdi.interpreter.TypeResolver, java.util.Map)
     */
public String build(final InternalKnowledgePackage pkg, final FunctionDescr functionDescr, final TypeResolver typeResolver, final Map<String, LineMappings> lineMappings, final List<KnowledgeBuilderResult> errors) {
    final Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("package", pkg.getName());
    vars.put("imports", pkg.getImports().keySet());
    final List<String> staticImports = new LinkedList<String>();
    for (String staticImport : pkg.getStaticImports()) {
        if (!staticImport.endsWith(functionDescr.getName())) {
            staticImports.add(staticImport);
        }
    }
    vars.put("staticImports", staticImports);
    vars.put("className", StringUtils.ucFirst(functionDescr.getName()));
    vars.put("methodName", functionDescr.getName());
    vars.put("returnType", functionDescr.getReturnType());
    vars.put("parameterTypes", functionDescr.getParameterTypes());
    vars.put("parameterNames", functionDescr.getParameterNames());
    vars.put("hashCode", functionDescr.getText().hashCode());
    // Check that all the parameters are resolvable
    final List<String> names = functionDescr.getParameterNames();
    final List<String> types = functionDescr.getParameterTypes();
    for (int i = 0, size = names.size(); i < size; i++) {
        try {
            typeResolver.resolveType(types.get(i));
        } catch (final ClassNotFoundException e) {
            errors.add(new FunctionError(functionDescr, e, "Unable to resolve type " + types.get(i) + " while building function."));
            break;
        }
    }
    vars.put("text", functionDescr.getText());
    final String text = String.valueOf(TemplateRuntime.eval(template, null, new MapVariableResolverFactory(vars)));
    final BufferedReader reader = new BufferedReader(new StringReader(text));
    final String lineStartsWith = "    public static " + functionDescr.getReturnType() + " " + functionDescr.getName();
    try {
        String line;
        int offset = 0;
        while ((line = reader.readLine()) != null) {
            offset++;
            if (line.startsWith(lineStartsWith)) {
                break;
            }
        }
        functionDescr.setOffset(offset);
    } catch (final IOException e) {
        // won't ever happen, it's just reading over a string.
        throw new RuntimeException("Error determining start offset with function");
    }
    final String name = pkg.getName() + "." + StringUtils.ucFirst(functionDescr.getName());
    final LineMappings mapping = new LineMappings(name);
    mapping.setStartLine(functionDescr.getLine());
    mapping.setOffset(functionDescr.getOffset());
    lineMappings.put(name, mapping);
    return text;
}
Also used : HashMap(java.util.HashMap) FunctionError(org.drools.compiler.compiler.FunctionError) IOException(java.io.IOException) LinkedList(java.util.LinkedList) LineMappings(org.drools.core.rule.LineMappings) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory)

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