Search in sources :

Example 26 with FieldDefinition

use of org.drools.core.factmodel.FieldDefinition in project drools by kiegroup.

the class TypeDeclarationCache method processFieldsPosition.

private void processFieldsPosition(Class<?> cls, ClassDefinition clsDef, TypeDeclaration typeDeclaration) {
    // it's a new type declaration, so generate the @Position for it
    Collection<Field> fields = new LinkedList<Field>();
    Class<?> tempKlass = cls;
    while (tempKlass != null && tempKlass != Object.class) {
        Collections.addAll(fields, tempKlass.getDeclaredFields());
        tempKlass = tempKlass.getSuperclass();
    }
    FieldDefinition[] orderedFields = new FieldDefinition[fields.size()];
    for (Field fld : fields) {
        Position pos = fld.getAnnotation(Position.class);
        if (pos != null) {
            if (pos.value() < 0 || pos.value() >= fields.size()) {
                kbuilder.addBuilderResult(new TypeDeclarationError(typeDeclaration, "Out of range position " + pos.value() + " for field '" + fld.getName() + "' on class " + cls.getName()));
                continue;
            }
            if (orderedFields[pos.value()] != null) {
                kbuilder.addBuilderResult(new TypeDeclarationError(typeDeclaration, "Duplicated position " + pos.value() + " for field '" + fld.getName() + "' on class " + cls.getName()));
                continue;
            }
            FieldDefinition fldDef = clsDef.getField(fld.getName());
            if (fldDef == null) {
                fldDef = new FieldDefinition(fld.getName(), fld.getType().getName());
            }
            fldDef.setIndex(pos.value());
            orderedFields[pos.value()] = fldDef;
        }
    }
    for (FieldDefinition fld : orderedFields) {
        if (fld != null) {
            // it's null if there is no @Position
            clsDef.addField(fld);
        }
    }
}
Also used : Field(java.lang.reflect.Field) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) Position(org.kie.api.definition.type.Position) FieldDefinition(org.drools.core.factmodel.FieldDefinition) LinkedList(java.util.LinkedList)

Example 27 with FieldDefinition

use of org.drools.core.factmodel.FieldDefinition in project drools by kiegroup.

the class PatternBuilder method processPositional.

protected void processPositional(final RuleBuildContext context, final PatternDescr patternDescr, final Pattern pattern, final ExprConstraintDescr descr) {
    if (descr.getType() == ExprConstraintDescr.Type.POSITIONAL && pattern.getObjectType() instanceof ClassObjectType) {
        Class<?> klazz = ((ClassObjectType) pattern.getObjectType()).getClassType();
        TypeDeclaration tDecl = context.getKnowledgeBuilder().getTypeDeclaration(klazz);
        if (tDecl == null) {
            registerDescrBuildError(context, patternDescr, "Unable to find @positional definitions for :" + klazz + "\n");
            return;
        }
        ClassDefinition clsDef = tDecl.getTypeClassDef();
        if (clsDef == null) {
            registerDescrBuildError(context, patternDescr, "Unable to find @positional field " + descr.getPosition() + " for class " + tDecl.getTypeName() + "\n");
            return;
        }
        FieldDefinition field = clsDef.getField(descr.getPosition());
        if (field == null) {
            registerDescrBuildError(context, patternDescr, "Unable to find @positional field " + descr.getPosition() + " for class " + tDecl.getTypeName() + "\n");
            return;
        }
        String expr = descr.getExpression();
        boolean isSimpleIdentifier = isIdentifier(expr);
        if (isSimpleIdentifier) {
            // create a binding
            BindingDescr binder = new BindingDescr();
            binder.setUnification(true);
            binder.setExpression(field.getName());
            binder.setVariable(descr.getExpression());
            buildRuleBindings(context, patternDescr, pattern, binder);
        } else {
            // create a constraint
            build(context, patternDescr, pattern, descr, field.getName() + " == " + descr.getExpression());
        }
    }
}
Also used : BindingDescr(org.drools.compiler.lang.descr.BindingDescr) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) FieldDefinition(org.drools.core.factmodel.FieldDefinition) ClassDefinition(org.drools.core.factmodel.ClassDefinition) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 28 with FieldDefinition

use of org.drools.core.factmodel.FieldDefinition in project drools by kiegroup.

the class AbstractTraitFactory method buildClassDefinition.

public ClassDefinition buildClassDefinition(Class<?> klazz, Class<?> wrapperClass) throws IOException {
    ClassFieldInspector inspector = new ClassFieldInspector(klazz);
    ClassFieldAccessorStore store = getClassFieldAccessorStore();
    ClassDefinition def;
    if (!klazz.isInterface()) {
        String className = wrapperClass.getName();
        String superClass = wrapperClass != klazz ? klazz.getName() : klazz.getSuperclass().getName();
        String[] interfaces = new String[klazz.getInterfaces().length + 1];
        for (int j = 0; j < klazz.getInterfaces().length; j++) {
            interfaces[j] = klazz.getInterfaces()[j].getName();
        }
        interfaces[interfaces.length - 1] = CoreWrapper.class.getName();
        def = new ClassDefinition(className, superClass, interfaces);
        def.setDefinedClass(wrapperClass);
        Traitable tbl = wrapperClass.getAnnotation(Traitable.class);
        def.setTraitable(true, tbl != null && tbl.logical());
        Map<String, Field> fields = inspector.getFieldTypesField();
        for (Field f : fields.values()) {
            if (f != null) {
                FieldDefinition fld = new FieldDefinition();
                fld.setName(f.getName());
                fld.setTypeName(f.getType().getName());
                fld.setInherited(true);
                ClassFieldAccessor accessor = store.getAccessor(def.getDefinedClass().getName(), fld.getName());
                fld.setReadWriteAccessor(accessor);
                if (inspector.getGetterMethods().containsKey(f.getName())) {
                    fld.setGetterName(inspector.getGetterMethods().get(f.getName()).getName());
                }
                if (inspector.getSetterMethods().containsKey(f.getName())) {
                    fld.setSetterName(inspector.getSetterMethods().get(f.getName()).getName());
                }
                def.addField(fld);
            }
        }
    } else {
        String className = klazz.getName();
        String superClass = Object.class.getName();
        String[] interfaces = new String[klazz.getInterfaces().length];
        for (int j = 0; j < klazz.getInterfaces().length; j++) {
            interfaces[j] = klazz.getInterfaces()[j].getName();
        }
        def = new ClassDefinition(className, superClass, interfaces);
        def.setDefinedClass(klazz);
        Map<String, Method> properties = inspector.getGetterMethods();
        for (String key : properties.keySet()) {
            Method m = properties.get(key);
            if (m != null && m.getDeclaringClass() != TraitType.class && m.getDeclaringClass() != Thing.class && inspector.getSetterMethods().containsKey(key)) {
                FieldDefinition fld = new FieldDefinition();
                fld.setName(getterToFieldName(m.getName()));
                fld.setTypeName(m.getReturnType().getName());
                fld.setInherited(true);
                ClassFieldAccessor accessor = store.getAccessor(def.getDefinedClass().getName(), fld.getName());
                fld.setReadWriteAccessor(accessor);
                fld.setGetterName(m.getName());
                fld.setSetterName(inspector.getSetterMethods().get(key).getName());
                def.addField(fld);
            }
        }
    }
    return def;
}
Also used : FieldDefinition(org.drools.core.factmodel.FieldDefinition) ClassFieldAccessorStore(org.drools.core.base.ClassFieldAccessorStore) Method(java.lang.reflect.Method) ClassDefinition(org.drools.core.factmodel.ClassDefinition) Field(java.lang.reflect.Field) ClassFieldAccessor(org.drools.core.base.ClassFieldAccessor) ClassFieldInspector(org.drools.core.util.asm.ClassFieldInspector)

Example 29 with FieldDefinition

use of org.drools.core.factmodel.FieldDefinition in project drools by kiegroup.

the class TraitClassBuilderImpl method buildClass.

public byte[] buildClass(ClassDefinition classDef, ClassLoader classLoader) {
    init(classDef);
    ClassWriter cw = null;
    try {
        String cName = BuildUtils.getInternalType(classDef.getClassName());
        String genericTypes = BuildUtils.getGenericTypes(classDef.getInterfaces());
        String superType = Type.getInternalName(Object.class);
        String[] intfaces = null;
        if (Object.class.getName().equals(classDef.getSuperClass())) {
            String[] tmp = BuildUtils.getInternalTypes(classDef.getInterfaces());
            intfaces = new String[tmp.length + 2];
            System.arraycopy(tmp, 0, intfaces, 0, tmp.length);
            intfaces[tmp.length] = Type.getInternalName(Serializable.class);
            intfaces[tmp.length + 1] = Type.getInternalName(GeneratedFact.class);
        } else {
            String[] tmp = BuildUtils.getInternalTypes(classDef.getInterfaces());
            intfaces = new String[tmp.length + 3];
            System.arraycopy(tmp, 0, intfaces, 0, tmp.length);
            intfaces[tmp.length] = BuildUtils.getInternalType(classDef.getSuperClass());
            intfaces[tmp.length + 1] = Type.getInternalName(Serializable.class);
            intfaces[tmp.length + 2] = Type.getInternalName(GeneratedFact.class);
        }
        cw = createClassWriter(classLoader, ACC_PUBLIC + ACC_ABSTRACT + ACC_INTERFACE, cName, genericTypes, superType, intfaces);
        {
            if (classDef.getDefinedClass() == null || classDef.getDefinedClass().getAnnotation(Trait.class) == null) {
                AnnotationVisitor av0 = cw.visitAnnotation(Type.getDescriptor(Trait.class), true);
                for (AnnotationDefinition adef : classDef.getAnnotations()) {
                    if (Trait.class.getName().equals(adef.getName())) {
                        addAnnotationAttribute(adef, av0);
                        break;
                    }
                }
                av0.visitEnd();
            }
        }
        for (FieldDefinition field : classDef.getFieldsDefinitions()) {
            buildField(cw, field);
        }
        finalizeCreation(classDef);
        cw.visitEnd();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return cw.toByteArray();
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) Serializable(java.io.Serializable) FieldDefinition(org.drools.core.factmodel.FieldDefinition) AnnotationVisitor(org.mvel2.asm.AnnotationVisitor) ClassGenerator.createClassWriter(org.drools.core.rule.builder.dialect.asm.ClassGenerator.createClassWriter) ClassWriter(org.mvel2.asm.ClassWriter) GeneratedFact(org.drools.core.factmodel.GeneratedFact)

Example 30 with FieldDefinition

use of org.drools.core.factmodel.FieldDefinition 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)

Aggregations

FieldDefinition (org.drools.core.factmodel.FieldDefinition)44 MethodVisitor (org.mvel2.asm.MethodVisitor)23 Map (java.util.Map)11 Label (org.mvel2.asm.Label)10 BitSet (java.util.BitSet)6 Collection (java.util.Collection)6 ClassDefinition (org.drools.core.factmodel.ClassDefinition)6 HashSet (java.util.HashSet)5 ArrayList (java.util.ArrayList)4 Set (java.util.Set)4 TypeFieldDescr (org.drools.compiler.lang.descr.TypeFieldDescr)4 Field (java.lang.reflect.Field)3 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)3 FactField (org.kie.api.definition.type.FactField)3 Position (org.kie.api.definition.type.Position)3 IOException (java.io.IOException)2 Method (java.lang.reflect.Method)2 HashMap (java.util.HashMap)2 AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)2 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)2