Search in sources :

Example 1 with TypeFieldDescr

use of org.drools.compiler.lang.descr.TypeFieldDescr in project drools by kiegroup.

the class ClassDefinitionFactory method wireFields.

protected boolean wireFields(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type, ClassDefinition def, PackageRegistry pkgRegistry, List<TypeDefinition> unresolvedTypeDefinitions) {
    // Fields are SORTED in the process
    if (!typeDescr.getFields().isEmpty()) {
        if (unresolvedTypeDefinitions != null && !unresolvedTypeDefinitions.isEmpty()) {
            for (TypeFieldDescr fld : typeDescr.getFields().values()) {
                if (unresolvedTypeDefinitions != null) {
                    for (TypeDefinition typeDef : unresolvedTypeDefinitions) {
                        if (fld.getPattern().getObjectType().equals(typeDef.getTypeClassName())) {
                            return false;
                        }
                    }
                }
            }
        }
        List<FieldDefinition> fieldDefs = sortFields(typeDescr.getFields(), pkgRegistry.getTypeResolver(), kbuilder);
        int i = 0;
        for (FieldDefinition fieldDef : fieldDefs) {
            fieldDef.setIndex(i++);
            def.addField(fieldDef);
        }
    }
    return true;
}
Also used : TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) FieldDefinition(org.drools.core.factmodel.FieldDefinition)

Example 2 with TypeFieldDescr

use of org.drools.compiler.lang.descr.TypeFieldDescr in project drools by kiegroup.

the class ClassDefinitionFactory method populateDefinitionFromClass.

public static void populateDefinitionFromClass(ClassDefinition def, Resource resource, Class<?> concrete, boolean asTrait) {
    try {
        def.setClassName(concrete.getName());
        if (concrete.getSuperclass() != null) {
            def.setSuperClass(concrete.getSuperclass().getName());
        }
        ClassFieldInspector inspector = new ClassFieldInspector(concrete);
        Map<String, Method> methods = inspector.getGetterMethods();
        Map<String, Method> setters = inspector.getSetterMethods();
        int j = 0;
        Map<String, TypeFieldDescr> fields = new HashMap<String, TypeFieldDescr>();
        for (String fieldName : methods.keySet()) {
            if (asTrait && ("core".equals(fieldName) || "fields".equals(fieldName))) {
                continue;
            }
            if (!inspector.isNonGetter(fieldName) && setters.keySet().contains(fieldName)) {
                Position position = null;
                if (!concrete.isInterface()) {
                    try {
                        Field fld = concrete.getDeclaredField(fieldName);
                        position = fld.getAnnotation(Position.class);
                    } catch (NoSuchFieldException nsfe) {
                    // @Position can only annotate fields. This x means that a getter/setter pair was found with no field
                    }
                }
                Class ret = methods.get(fieldName).getReturnType();
                TypeFieldDescr field = new TypeFieldDescr();
                field.setResource(resource);
                field.setFieldName(fieldName);
                field.setPattern(new PatternDescr(ret.getName()));
                field.setIndex(position != null ? position.value() : -1);
                fields.put(fieldName, field);
            }
        }
        if (!fields.isEmpty()) {
            List<FieldDefinition> fieldDefs = sortFields(fields, null, null);
            int i = 0;
            for (FieldDefinition fieldDef : fieldDefs) {
                fieldDef.setIndex(i++);
                def.addField(fieldDef);
            }
        }
        Set<String> interfaces = new HashSet<String>();
        Collections.addAll(interfaces, def.getInterfaces());
        for (Class iKlass : ClassUtils.getAllImplementedInterfaceNames(concrete)) {
            interfaces.add(iKlass.getName());
        }
        def.setInterfaces(interfaces.toArray(new String[interfaces.size()]));
        def.setDefinedClass(concrete);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) HashMap(java.util.HashMap) Position(org.kie.api.definition.type.Position) FieldDefinition(org.drools.core.factmodel.FieldDefinition) Method(java.lang.reflect.Method) IOException(java.io.IOException) Field(java.lang.reflect.Field) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) ClassFieldInspector(org.drools.core.util.asm.ClassFieldInspector) HashSet(java.util.HashSet)

Example 3 with TypeFieldDescr

use of org.drools.compiler.lang.descr.TypeFieldDescr in project drools by kiegroup.

the class ClassHierarchyManager method buildDescrsFromFields.

private static void buildDescrsFromFields(Class klass, TypeDeclarationDescr typeDescr, PackageRegistry pkgRegistry, Map<String, TypeFieldDescr> fieldMap) {
    ClassFieldInspector inspector = null;
    try {
        inspector = new ClassFieldInspector(klass);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    for (String name : inspector.getGetterMethods().keySet()) {
        // classFieldAccessor requires both getter and setter
        if (inspector.getSetterMethods().containsKey(name)) {
            if (!inspector.isNonGetter(name) && !"class".equals(name)) {
                Resource resource = typeDescr.getResource();
                PatternDescr patternDescr = new PatternDescr(inspector.getFieldType(name).getName());
                patternDescr.setResource(resource);
                TypeFieldDescr inheritedFlDescr = new TypeFieldDescr(name, patternDescr);
                inheritedFlDescr.setResource(resource);
                inheritedFlDescr.setInherited(!Modifier.isAbstract(inspector.getGetterMethods().get(name).getModifiers()));
                if (!fieldMap.containsKey(inheritedFlDescr.getFieldName()))
                    fieldMap.put(inheritedFlDescr.getFieldName(), inheritedFlDescr);
            }
        }
    }
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) Resource(org.kie.api.io.Resource) IOException(java.io.IOException) ClassFieldInspector(org.drools.core.util.asm.ClassFieldInspector)

Example 4 with TypeFieldDescr

use of org.drools.compiler.lang.descr.TypeFieldDescr in project drools by kiegroup.

the class ClassHierarchyManager method buildInheritedFieldDescrFromDefinition.

protected TypeFieldDescr buildInheritedFieldDescrFromDefinition(org.kie.api.definition.type.FactField fld, TypeDeclarationDescr typeDescr) {
    TypeFieldDescr inheritedFldDescr = new TypeFieldDescr();
    inheritedFldDescr.setFieldName(fld.getName());
    inheritedFldDescr.setResource(typeDescr.getResource());
    PatternDescr fldType = new PatternDescr();
    fldType.setObjectType(((FieldDefinition) fld).getTypeName());
    // also sets resource for PatternDescr fldType
    inheritedFldDescr.setPattern(fldType);
    if (fld.isKey()) {
        AnnotationDescr keyAnnotation = new AnnotationDescr(Key.class.getCanonicalName());
        keyAnnotation.setFullyQualifiedName(Key.class.getCanonicalName());
        keyAnnotation.setResource(typeDescr.getResource());
        inheritedFldDescr.addAnnotation(keyAnnotation);
    }
    inheritedFldDescr.setIndex(((FieldDefinition) fld).getDeclIndex());
    inheritedFldDescr.setInherited(true);
    String initExprOverride = ((FieldDefinition) fld).getInitExpr();
    int overrideCount = 0;
    // only @aliasing local fields may override defaults.
    for (TypeFieldDescr localField : typeDescr.getFields().values()) {
        Alias alias = localField.getTypedAnnotation(Alias.class);
        if (alias != null && fld.getName().equals(alias.value().replaceAll("\"", "")) && localField.getInitExpr() != null) {
            overrideCount++;
            initExprOverride = localField.getInitExpr();
        }
    }
    if (overrideCount > 1) {
        // however, only one is allowed
        initExprOverride = null;
    }
    inheritedFldDescr.setInitExpr(initExprOverride);
    return inheritedFldDescr;
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) Alias(org.drools.core.factmodel.traits.Alias) FieldDefinition(org.drools.core.factmodel.FieldDefinition) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) Key(org.kie.api.definition.type.Key)

Example 5 with TypeFieldDescr

use of org.drools.compiler.lang.descr.TypeFieldDescr in project drools by kiegroup.

the class TypeDeclarationNameResolver method ensureQualifiedFieldType.

public void ensureQualifiedFieldType(AbstractClassTypeDeclarationDescr typeDescr, PackageDescr packageDescr, TypeResolver typeResolver, List<TypeDefinition> unresolvedTypes) {
    for (TypeFieldDescr field : typeDescr.getFields().values()) {
        String declaredType = field.getPattern().getObjectType();
        String resolved = resolveName(declaredType, typeDescr, packageDescr, typeResolver, unresolvedTypes, true);
        if (resolved != null) {
            field.getPattern().setObjectType(resolved);
        } else {
            kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Cannot resolve type '" + declaredType + " for field " + field.getFieldName() + " in declared type " + typeDescr.getTypeName()));
        }
    }
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr)

Aggregations

TypeFieldDescr (org.drools.compiler.lang.descr.TypeFieldDescr)14 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)5 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)5 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)5 TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 FieldDefinition (org.drools.core.factmodel.FieldDefinition)4 TypeDeclaration (org.drools.core.rule.TypeDeclaration)3 ClassFieldInspector (org.drools.core.util.asm.ClassFieldInspector)3 Test (org.junit.Test)3 Position (org.kie.api.definition.type.Position)3 Collection (java.util.Collection)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 GeneratedFact (org.drools.core.factmodel.GeneratedFact)2 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)2