Search in sources :

Example 1 with TypeDeclarationError

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

the class ClassDefinitionFactory method createClassDefinition.

protected ClassDefinition createClassDefinition(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type) {
    // extracts type, supertype and interfaces
    String fullName = typeDescr.getType().getFullName();
    if (type.getKind().equals(TypeDeclaration.Kind.CLASS)) {
        TypeDeclarationDescr tdescr = (TypeDeclarationDescr) typeDescr;
        if (tdescr.getSuperTypes().size() > 1) {
            kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Declared class " + fullName + "  - has more than one supertype;"));
            return null;
        } else if (tdescr.getSuperTypes().isEmpty()) {
            tdescr.addSuperType("java.lang.Object");
        }
    }
    Traitable traitableAnn = typeDescr.getTypedAnnotation(Traitable.class);
    boolean traitable = traitableAnn != null;
    String[] fullSuperTypes = new String[typeDescr.getSuperTypes().size() + 1];
    int j = 0;
    for (QualifiedName qname : typeDescr.getSuperTypes()) {
        fullSuperTypes[j++] = qname.getFullName();
    }
    fullSuperTypes[j] = Thing.class.getName();
    List<String> interfaceList = new ArrayList<String>();
    interfaceList.add(traitable ? Externalizable.class.getName() : Serializable.class.getName());
    if (traitable) {
        interfaceList.add(TraitableBean.class.getName());
    }
    String[] interfaces = interfaceList.toArray(new String[interfaceList.size()]);
    // prepares a class definition
    ClassDefinition def;
    switch(type.getKind()) {
        case TRAIT:
            def = new ClassDefinition(fullName, Object.class.getName(), fullSuperTypes);
            break;
        case ENUM:
            def = new EnumClassDefinition(fullName, fullSuperTypes[0], null);
            break;
        case CLASS:
        default:
            def = new ClassDefinition(fullName, fullSuperTypes[0], interfaces);
            def.setTraitable(traitable, traitableAnn != null && traitableAnn.logical());
    }
    return def;
}
Also used : AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) QualifiedName(org.drools.compiler.lang.descr.QualifiedName) ArrayList(java.util.ArrayList) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) EnumClassDefinition(org.drools.core.factmodel.EnumClassDefinition) ClassDefinition(org.drools.core.factmodel.ClassDefinition) EnumClassDefinition(org.drools.core.factmodel.EnumClassDefinition) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) Traitable(org.drools.core.factmodel.traits.Traitable) Thing(org.drools.core.factmodel.traits.Thing)

Example 2 with TypeDeclarationError

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

the class DeclaredClassBuilder method generateBeanFromDefinition.

public void generateBeanFromDefinition(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type, PackageRegistry pkgRegistry, ClassDefinition def) {
    if (type.isNovel()) {
        String fullName = typeDescr.getType().getFullName();
        JavaDialectRuntimeData dialect = (JavaDialectRuntimeData) pkgRegistry.getDialectRuntimeRegistry().getDialectData("java");
        if (ensureJavaTypeConsistency(typeDescr, def, pkgRegistry.getTypeResolver())) {
            switch(type.getKind()) {
                case TRAIT:
                    try {
                        buildClass(def, fullName, dialect, this.kbuilder.getBuilderConfiguration().getClassBuilderFactory().getTraitBuilder(), pkgRegistry);
                    } catch (Exception e) {
                        e.printStackTrace();
                        this.kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Unable to compile declared trait " + fullName + ": " + e.getMessage() + ";"));
                    }
                    break;
                case ENUM:
                    try {
                        buildClass(def, fullName, dialect, this.kbuilder.getBuilderConfiguration().getClassBuilderFactory().getEnumClassBuilder(), pkgRegistry);
                    } catch (Exception e) {
                        e.printStackTrace();
                        this.kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Unable to compile declared enum " + fullName + ": " + e.getMessage() + ";"));
                    }
                    break;
                case CLASS:
                default:
                    try {
                        buildClass(def, fullName, dialect, this.kbuilder.getBuilderConfiguration().getClassBuilderFactory().getBeanClassBuilder(), pkgRegistry);
                    } catch (Exception e) {
                        e.printStackTrace();
                        this.kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Unable to create a class for declared type " + fullName + ": " + e.getMessage() + ";"));
                    }
                    break;
            }
        }
    }
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) JavaDialectRuntimeData(org.drools.core.rule.JavaDialectRuntimeData)

Example 3 with TypeDeclarationError

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

the class TypeDeclarationBuilder method createBean.

protected void createBean(AbstractClassTypeDeclarationDescr typeDescr, PackageRegistry pkgRegistry, ClassHierarchyManager hierarchyManager, List<TypeDefinition> unresolvedTypes, Map<String, AbstractClassTypeDeclarationDescr> unprocesseableDescrs) {
    // descriptor needs fields inherited from superclass
    if (typeDescr instanceof TypeDeclarationDescr) {
        hierarchyManager.inheritFields(pkgRegistry, typeDescr, hierarchyManager.getSortedDescriptors(), unresolvedTypes, unprocesseableDescrs);
    }
    TypeDeclaration type = typeDeclarationFactory.processTypeDeclaration(pkgRegistry, typeDescr);
    boolean success = !kbuilder.hasErrors();
    try {
        // the type declaration is generated in any case (to be used by subclasses, if any)
        // the actual class will be generated only if needed
        ClassDefinition def = null;
        if (success) {
            def = classDefinitionFactory.generateDeclaredBean(typeDescr, type, pkgRegistry, unresolvedTypes, unprocesseableDescrs);
            // this has to be done after the classDef has been generated
            if (!type.isNovel()) {
                typeDeclarationFactory.checkRedeclaration(typeDescr, type, pkgRegistry);
            }
        }
        success = (def != null) && (!kbuilder.hasErrors());
        if (success) {
            updateTraitInformation(typeDescr, type, def, pkgRegistry);
        }
        success = !kbuilder.hasErrors();
        if (success) {
            declaredClassBuilder.generateBeanFromDefinition(typeDescr, type, pkgRegistry, def);
        }
        success = !kbuilder.hasErrors();
        if (success) {
            Class<?> clazz = pkgRegistry.getTypeResolver().resolveType(typeDescr.getType().getFullName());
            type.setTypeClass(clazz);
            type.setValid(true);
        } else {
            unprocesseableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
            type.setValid(false);
        }
        typeDeclarationConfigurator.finalize(type, typeDescr, pkgRegistry, kbuilder.getPackageRegistry(), hierarchyManager);
    } catch (final ClassNotFoundException e) {
        unprocesseableDescrs.put(typeDescr.getType().getFullName(), typeDescr);
        kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Class '" + type.getTypeClassName() + "' not found for type declaration of '" + type.getTypeName() + "'"));
    }
    if (!success) {
        unresolvedTypes.add(new TypeDefinition(type, typeDescr));
    } else {
        registerGeneratedType(typeDescr);
    }
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) ClassDefinition(org.drools.core.factmodel.ClassDefinition) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 4 with TypeDeclarationError

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

the class TypeDeclarationBuilder method compactDefinitionsAndDeclarations.

private Collection<AbstractClassTypeDeclarationDescr> compactDefinitionsAndDeclarations(Collection<AbstractClassTypeDeclarationDescr> unsortedDescrs, Map<String, AbstractClassTypeDeclarationDescr> unprocesseableDescrs) {
    Map<String, AbstractClassTypeDeclarationDescr> compactedUnsorted = new HashMap<String, AbstractClassTypeDeclarationDescr>(unsortedDescrs.size());
    for (AbstractClassTypeDeclarationDescr descr : unsortedDescrs) {
        if (compactedUnsorted.containsKey(descr.getType().getFullName())) {
            AbstractClassTypeDeclarationDescr prev = compactedUnsorted.get(descr.getType().getFullName());
            boolean res = mergeTypeDescriptors(prev, descr);
            if (!res) {
                unprocesseableDescrs.put(prev.getType().getFullName(), prev);
                kbuilder.addBuilderResult(new TypeDeclarationError(prev, "Found duplicate declaration for type " + prev.getType().getFullName() + ", unable to reconcile "));
            }
        } else {
            compactedUnsorted.put(descr.getType().getFullName(), descr);
        }
    }
    return compactedUnsorted.values();
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) HashMap(java.util.HashMap) AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr)

Example 5 with TypeDeclarationError

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

the class TypeDeclarationBuilder method updateTraitInformation.

protected void updateTraitInformation(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type, ClassDefinition def, PackageRegistry pkgRegistry) {
    if (typeDescr.hasAnnotation(Traitable.class) || (!type.getKind().equals(TypeDeclaration.Kind.TRAIT) && kbuilder.getPackageRegistry().containsKey(def.getSuperClass()) && kbuilder.getPackageRegistry(def.getSuperClass()).getTraitRegistry().getTraitables().containsKey(def.getSuperClass()))) {
        // traitable
        if (type.isNovel()) {
            try {
                PackageRegistry reg = kbuilder.getPackageRegistry(typeDescr.getNamespace());
                String availableName = typeDescr.getType().getFullName();
                Class<?> resolvedType = reg.getTypeResolver().resolveType(availableName);
                updateTraitDefinition(type, resolvedType, false);
            } catch (ClassNotFoundException cnfe) {
            // we already know the class exists
            }
        }
        pkgRegistry.getTraitRegistry().addTraitable(def);
    } else if (type.getKind().equals(TypeDeclaration.Kind.TRAIT) || typeDescr.hasAnnotation(Trait.class)) {
        // trait
        if (!type.isNovel()) {
            try {
                PackageRegistry reg = kbuilder.getPackageRegistry(typeDescr.getNamespace());
                String availableName = typeDescr.getType().getFullName();
                Class<?> resolvedType = reg.getTypeResolver().resolveType(availableName);
                if (!Thing.class.isAssignableFrom(resolvedType)) {
                    if (!resolvedType.isInterface()) {
                        kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Unable to redeclare concrete class " + resolvedType.getName() + " as a trait."));
                        return;
                    }
                    updateTraitDefinition(type, resolvedType, false);
                    String target = typeDescr.getTypeName() + TraitFactory.SUFFIX;
                    TypeDeclarationDescr tempDescr = new TypeDeclarationDescr();
                    tempDescr.setNamespace(typeDescr.getNamespace());
                    tempDescr.setFields(typeDescr.getFields());
                    tempDescr.setType(target, typeDescr.getNamespace());
                    tempDescr.setTrait(true);
                    tempDescr.addSuperType(typeDescr.getType());
                    tempDescr.setResource(type.getResource());
                    TypeDeclaration tempDeclr = new TypeDeclaration(target);
                    tempDeclr.setKind(TypeDeclaration.Kind.TRAIT);
                    tempDeclr.setTypesafe(type.isTypesafe());
                    tempDeclr.setNovel(true);
                    tempDeclr.setTypeClassName(tempDescr.getType().getFullName());
                    tempDeclr.setResource(type.getResource());
                    ClassDefinition tempDef = new ClassDefinition(target);
                    tempDef.setClassName(tempDescr.getType().getFullName());
                    tempDef.setTraitable(false);
                    for (FieldDefinition fld : def.getFieldsDefinitions()) {
                        tempDef.addField(fld);
                    }
                    tempDef.setInterfaces(def.getInterfaces());
                    tempDef.setSuperClass(def.getClassName());
                    tempDef.setDefinedClass(resolvedType);
                    tempDef.setAbstrakt(true);
                    tempDeclr.setTypeClassDef(tempDef);
                    declaredClassBuilder.generateBeanFromDefinition(tempDescr, tempDeclr, pkgRegistry, tempDef);
                    try {
                        Class<?> clazz = pkgRegistry.getTypeResolver().resolveType(tempDescr.getType().getFullName());
                        tempDeclr.setTypeClass(clazz);
                        pkgRegistry.getTraitRegistry().addTrait(tempDef.getClassName().replace(TraitFactory.SUFFIX, ""), tempDef);
                    } catch (ClassNotFoundException cnfe) {
                        kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Internal Trait extension Class '" + target + "' could not be generated correctly'"));
                    } finally {
                        pkgRegistry.getPackage().addTypeDeclaration(tempDeclr);
                    }
                } else {
                    updateTraitDefinition(type, resolvedType, true);
                    pkgRegistry.getTraitRegistry().addTrait(def);
                }
            } catch (ClassNotFoundException cnfe) {
            // we already know the class exists
            }
        } else {
            if (def.getClassName().endsWith(TraitFactory.SUFFIX)) {
                pkgRegistry.getTraitRegistry().addTrait(def.getClassName().replace(TraitFactory.SUFFIX, ""), def);
            } else {
                pkgRegistry.getTraitRegistry().addTrait(def);
            }
        }
    }
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) FieldDefinition(org.drools.core.factmodel.FieldDefinition) Traitable(org.drools.core.factmodel.traits.Traitable) ClassDefinition(org.drools.core.factmodel.ClassDefinition) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

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