Search in sources :

Example 11 with TypeDeclarationError

use of org.drools.compiler.compiler.TypeDeclarationError in project drools by kiegroup.

the class ClassHierarchyManager method mergeFields.

protected void mergeFields(String simpleSuperTypeName, String superTypePackageName, String fullSuper, TypeDeclarationDescr typeDescr, List<TypeDefinition> unresolvedTypes, Map<String, AbstractClassTypeDeclarationDescr> unprocessableDescrs, TypeResolver resolver) {
    Map<String, TypeFieldDescr> fieldMap = new LinkedHashMap<String, TypeFieldDescr>();
    boolean isNovel = TypeDeclarationUtils.isNovelClass(typeDescr, kbuilder.getPackageRegistry(typeDescr.getNamespace()));
    PackageRegistry registry = kbuilder.getPackageRegistry(superTypePackageName);
    InternalKnowledgePackage pack = null;
    if (registry != null) {
        pack = registry.getPackage();
    }
    if (unprocessableDescrs.containsKey(fullSuper)) {
        unprocessableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
        return;
    }
    // if a class is declared in DRL, its package can't be null? The default package is replaced by "defaultpkg"
    boolean isSuperClassTagged = false;
    // in the same package, or in a previous one
    boolean isSuperClassDeclared = true;
    if (pack != null) {
        // look for the supertype declaration in available packages
        TypeDeclaration superTypeDeclaration = pack.getTypeDeclaration(simpleSuperTypeName);
        if (superTypeDeclaration != null && superTypeDeclaration.getTypeClassDef() != null) {
            ClassDefinition classDef = superTypeDeclaration.getTypeClassDef();
            // inherit fields
            for (org.kie.api.definition.type.FactField fld : classDef.getFields()) {
                TypeFieldDescr inheritedFlDescr = buildInheritedFieldDescrFromDefinition(fld, typeDescr);
                fieldMap.put(inheritedFlDescr.getFieldName(), inheritedFlDescr);
            }
            // new classes are already distinguished from tagged external classes
            isSuperClassTagged = !superTypeDeclaration.isNovel();
        }
    } else {
        isSuperClassDeclared = false;
    }
    // look for the class externally
    if (!isSuperClassDeclared || isSuperClassTagged) {
        try {
            Class superKlass;
            if (registry != null) {
                superKlass = registry.getTypeResolver().resolveType(fullSuper);
            } else {
                // if the supertype has not been declared, and we have got so far, it means that this class is not novel
                superKlass = resolver.resolveType(fullSuper);
            }
            buildDescrsFromFields(superKlass, typeDescr, registry, fieldMap);
        } catch (ClassNotFoundException cnfe) {
            unprocessableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
            return;
        }
    }
    // notice that it is not possible to override a field changing its type
    for (String fieldName : typeDescr.getFields().keySet()) {
        if (fieldMap.containsKey(fieldName)) {
            String type1 = fieldMap.get(fieldName).getPattern().getObjectType();
            String type2 = typeDescr.getFields().get(fieldName).getPattern().getObjectType();
            if (type2.lastIndexOf(".") < 0) {
                try {
                    TypeResolver typeResolver = kbuilder.getPackageRegistry(typeDescr.getNamespace()).getTypeResolver();
                    type1 = typeResolver.resolveType(type1).getName();
                    type2 = typeResolver.resolveType(type2).getName();
                    // now that we are at it... this will be needed later anyway
                    fieldMap.get(fieldName).getPattern().setObjectType(type1);
                    typeDescr.getFields().get(fieldName).getPattern().setObjectType(type2);
                } catch (ClassNotFoundException cnfe) {
                // will fail later
                }
            }
            boolean clash = !type1.equals(type2);
            TypeFieldDescr overriding = null;
            if (clash) {
                // this may still be an override using a subclass of the original type
                try {
                    Class<?> sup = resolver.resolveType(type1);
                    Class<?> loc = resolver.resolveType(type2);
                    if (sup.isAssignableFrom(loc)) {
                        clash = false;
                        // mark as non inherited so that a new field is actually built
                        overriding = fieldMap.get(fieldName);
                    }
                } catch (ClassNotFoundException cnfe) {
                // not much to do
                }
            }
            if (clash) {
                kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Cannot redeclare field '" + fieldName + " from " + type1 + " to " + type2));
                typeDescr.setType(null, null);
                return;
            } else {
                String initVal = fieldMap.get(fieldName).getInitExpr();
                TypeFieldDescr fd = typeDescr.getFields().get(fieldName);
                if (fd.getInitExpr() == null) {
                    fd.setInitExpr(initVal);
                }
                fd.setInherited(fieldMap.get(fieldName).isInherited());
                fd.setOverriding(overriding);
                for (String key : fieldMap.get(fieldName).getAnnotationNames()) {
                    if (fd.getAnnotation(key) == null) {
                        fd.addAnnotation(fieldMap.get(fieldName).getAnnotation(key));
                    }
                }
                if (fd.getIndex() < 0) {
                    fd.setIndex(fieldMap.get(fieldName).getIndex());
                }
            }
        }
        fieldMap.put(fieldName, typeDescr.getFields().get(fieldName));
    }
    typeDescr.setFields(fieldMap);
}
Also used : TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) ClassDefinition(org.drools.core.factmodel.ClassDefinition) LinkedHashMap(java.util.LinkedHashMap) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 12 with TypeDeclarationError

use of org.drools.compiler.compiler.TypeDeclarationError in project drools by kiegroup.

the class KnowledgeBuilderImpl method mergePackage.

void mergePackage(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
    for (final ImportDescr importDescr : packageDescr.getImports()) {
        pkgRegistry.addImport(importDescr);
    }
    normalizeTypeDeclarationAnnotations(packageDescr, pkgRegistry.getTypeResolver());
    processAccumulateFunctions(pkgRegistry, packageDescr);
    processEntryPointDeclarations(pkgRegistry, packageDescr);
    Map<String, AbstractClassTypeDeclarationDescr> unprocesseableDescrs = new HashMap<String, AbstractClassTypeDeclarationDescr>();
    List<TypeDefinition> unresolvedTypes = new ArrayList<TypeDefinition>();
    List<AbstractClassTypeDeclarationDescr> unsortedDescrs = new ArrayList<AbstractClassTypeDeclarationDescr>();
    unsortedDescrs.addAll(packageDescr.getTypeDeclarations());
    unsortedDescrs.addAll(packageDescr.getEnumDeclarations());
    typeBuilder.processTypeDeclarations(packageDescr, pkgRegistry, unsortedDescrs, unresolvedTypes, unprocesseableDescrs);
    for (AbstractClassTypeDeclarationDescr descr : unprocesseableDescrs.values()) {
        this.addBuilderResult(new TypeDeclarationError(descr, "Unable to process type " + descr.getTypeName()));
    }
    processOtherDeclarations(pkgRegistry, packageDescr);
    normalizeRuleAnnotations(packageDescr, pkgRegistry.getTypeResolver());
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) ArrayList(java.util.ArrayList) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) AccumulateImportDescr(org.drools.compiler.lang.descr.AccumulateImportDescr)

Example 13 with TypeDeclarationError

use of org.drools.compiler.compiler.TypeDeclarationError 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 14 with TypeDeclarationError

use of org.drools.compiler.compiler.TypeDeclarationError in project drools by kiegroup.

the class TypeDeclarationConfigurator method wireDurationAccessor.

private static void wireDurationAccessor(KnowledgeBuilderImpl kbuilder, Annotated annotated, TypeDeclaration type, PackageRegistry pkgRegistry) {
    Duration duration = annotated.getTypedAnnotation(Duration.class);
    if (duration != null) {
        BaseDescr typeDescr = annotated instanceof BaseDescr ? ((BaseDescr) annotated) : new BaseDescr();
        String durationField;
        try {
            durationField = duration.value();
        } catch (Exception e) {
            kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, e.getMessage()));
            return;
        }
        type.setDurationAttribute(durationField);
        InternalKnowledgePackage pkg = pkgRegistry.getPackage();
        MVELAnalysisResult results = getMvelAnalysisResult(kbuilder, typeDescr, type, pkgRegistry, durationField, pkg);
        if (results != null) {
            type.setDurationExtractor(getFieldExtractor(type, durationField, pkg, results));
        } else {
            kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Error processing @duration for TypeDeclaration '" + type.getFullName() + "': cannot access the field '" + durationField + "'"));
        }
    }
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) MVELAnalysisResult(org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult) Duration(org.kie.api.definition.type.Duration) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 15 with TypeDeclarationError

use of org.drools.compiler.compiler.TypeDeclarationError in project drools by kiegroup.

the class TypeDeclarationFactory method checkRedeclaration.

protected void checkRedeclaration(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type, PackageRegistry pkgRegistry) {
    TypeDeclaration previousTypeDeclaration = kbuilder.getPackageRegistry(typeDescr.getNamespace()).getPackage().getTypeDeclaration(typeDescr.getTypeName());
    try {
        // to the behavior previous these changes
        if (!type.isDefinition()) {
            // new declarations of a POJO can't declare new fields,
            // except if the POJO was previously generated/compiled and saved into the kjar
            Class<?> existingDeclarationClass = TypeDeclarationUtils.getExistingDeclarationClass(typeDescr, pkgRegistry);
            if (!kbuilder.getBuilderConfiguration().isPreCompiled() && !GeneratedFact.class.isAssignableFrom(existingDeclarationClass) && !type.getTypeClassDef().getFields().isEmpty()) {
                try {
                    Class existingClass = pkgRegistry.getPackage().getTypeResolver().resolveType(typeDescr.getType().getFullName());
                    ClassFieldInspector cfi = new ClassFieldInspector(existingClass);
                    int fieldCount = 0;
                    for (String existingFieldName : cfi.getFieldTypesField().keySet()) {
                        if (!cfi.isNonGetter(existingFieldName) && !"class".equals(existingFieldName) && cfi.getSetterMethods().containsKey(existingFieldName) && cfi.getGetterMethods().containsKey(existingFieldName)) {
                            if (!typeDescr.getFields().containsKey(existingFieldName)) {
                                type.setValid(false);
                                kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "New declaration of " + typeDescr.getType().getFullName() + " does not include field " + existingFieldName));
                            } else {
                                String fldType = cfi.getFieldType(existingFieldName).getName();
                                fldType = TypeDeclarationUtils.toBuildableType(fldType, kbuilder.getRootClassLoader());
                                TypeFieldDescr declaredField = typeDescr.getFields().get(existingFieldName);
                                if (!fldType.equals(type.getTypeClassDef().getField(existingFieldName).getTypeName())) {
                                    type.setValid(false);
                                    kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "New declaration of " + typeDescr.getType().getFullName() + " redeclared field " + existingFieldName + " : \n" + "existing : " + fldType + " vs declared : " + declaredField.getPattern().getObjectType()));
                                } else {
                                    fieldCount++;
                                }
                            }
                        }
                    }
                    if (fieldCount != typeDescr.getFields().size()) {
                        kbuilder.addBuilderResult(reportDeclarationDiff(cfi, typeDescr));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    type.setValid(false);
                    kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Unable to redeclare " + typeDescr.getType().getFullName() + " : " + e.getMessage()));
                } catch (ClassNotFoundException e) {
                    type.setValid(false);
                    kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Unable to redeclare " + typeDescr.getType().getFullName() + " : " + e.getMessage()));
                }
            }
        } else if (previousTypeDeclaration != null) {
            // previous declaration can be null during an incremental compilation
            int typeComparisonResult = this.compareTypeDeclarations(previousTypeDeclaration, type);
            if (typeComparisonResult < 0) {
                // oldDeclaration is "less" than newDeclaration -> error
                kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, typeDescr.getType().getFullName() + " declares more fields than the already existing version"));
                type.setValid(false);
            } else if (typeComparisonResult > 0 && !type.getTypeClassDef().getFields().isEmpty()) {
                // oldDeclaration is "grater" than newDeclaration -> error
                kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, typeDescr.getType().getFullName() + " declares less fields than the already existing version"));
                type.setValid(false);
            }
            // fields present in the previous declaration
            if (type.getNature() == TypeDeclaration.Nature.DECLARATION) {
                mergeTypeDeclarations(previousTypeDeclaration, type);
            }
        }
    } catch (IncompatibleClassChangeError error) {
        // if the types are incompatible -> error
        kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, error.getMessage()));
    }
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) IOException(java.io.IOException) TypeDeclaration(org.drools.core.rule.TypeDeclaration) GeneratedFact(org.drools.core.factmodel.GeneratedFact) ClassFieldInspector(org.drools.core.util.asm.ClassFieldInspector)

Aggregations

TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)16 AbstractClassTypeDeclarationDescr (org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr)6 TypeFieldDescr (org.drools.compiler.lang.descr.TypeFieldDescr)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 ClassDefinition (org.drools.core.factmodel.ClassDefinition)4 TypeDeclaration (org.drools.core.rule.TypeDeclaration)4 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 QualifiedName (org.drools.compiler.lang.descr.QualifiedName)3 TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 FieldDefinition (org.drools.core.factmodel.FieldDefinition)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)2 AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)2 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)2 MVELAnalysisResult (org.drools.compiler.rule.builder.dialect.mvel.MVELAnalysisResult)2 AnnotationDefinition (org.drools.core.factmodel.AnnotationDefinition)2 Traitable (org.drools.core.factmodel.traits.Traitable)2