Search in sources :

Example 11 with AbstractClassTypeDeclarationDescr

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

the class TypeDeclarationBuilder method mergeTypeDescriptors.

private boolean mergeTypeDescriptors(AbstractClassTypeDeclarationDescr prev, AbstractClassTypeDeclarationDescr descr) {
    boolean isDef1 = isDefinition(prev);
    boolean isDef2 = isDefinition(descr);
    if (isDef1 && isDef2) {
        return false;
    }
    if (!prev.getSuperTypes().isEmpty() && !descr.getSuperTypes().isEmpty() && prev.getSuperTypes().size() != descr.getSuperTypes().size()) {
        return false;
    }
    if (prev.getSuperTypes().isEmpty()) {
        for (QualifiedName qn : descr.getSuperTypes()) {
            ((TypeDeclarationDescr) prev).addSuperType(qn);
        }
    }
    if (prev.getFields().isEmpty()) {
        for (String fieldName : descr.getFields().keySet()) {
            prev.addField(descr.getFields().get(fieldName));
        }
    }
    for (AnnotationDescr ad : descr.getAnnotations()) {
        prev.addQualifiedAnnotation(ad);
    }
    for (AnnotationDescr ad : prev.getAnnotations()) {
        if (!descr.getAnnotations().contains(ad)) {
            descr.addQualifiedAnnotation(ad);
        }
    }
    return true;
}
Also used : AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) QualifiedName(org.drools.compiler.lang.descr.QualifiedName) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr)

Example 12 with AbstractClassTypeDeclarationDescr

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

the class TypeDeclarationFactory method processAnnotations.

public static void processAnnotations(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type) {
    Role role = typeDescr.getTypedAnnotation(Role.class);
    if (role != null) {
        type.setRole(role.value());
    }
    TypeSafe typeSafe = typeDescr.getTypedAnnotation(TypeSafe.class);
    if (typeSafe != null) {
        type.setTypesafe(typeSafe.value());
    }
    if (typeDescr instanceof EnumDeclarationDescr) {
        type.setKind(TypeDeclaration.Kind.ENUM);
    } else if (typeDescr instanceof TypeDeclarationDescr && ((TypeDeclarationDescr) typeDescr).isTrait()) {
        type.setKind(TypeDeclaration.Kind.TRAIT);
    }
    type.setDynamic(typeDescr.hasAnnotation(PropertyChangeSupport.class));
}
Also used : Role(org.kie.api.definition.type.Role) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) EnumDeclarationDescr(org.drools.compiler.lang.descr.EnumDeclarationDescr) PropertyChangeSupport(org.kie.api.definition.type.PropertyChangeSupport) TypeSafe(org.kie.api.definition.type.TypeSafe)

Example 13 with AbstractClassTypeDeclarationDescr

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

the class TypeDeclarationUtils method resolveType.

/**
 * Tries to determine the namespace (package) of a simple type chosen to be
 * the superclass of a declared bean. Looks among imports, local
 * declarations and previous declarations. Means that a class can't extend
 * another class declared in package that has not been loaded yet.
 *
 * @param klass        the simple name of the class
 * @param packageDescr the descriptor of the package the base class is declared in
 * @param pkgRegistry  the current package registry
 * @return the fully qualified name of the superclass
 */
public static String resolveType(String klass, PackageDescr packageDescr, PackageRegistry pkgRegistry) {
    String arraySuffix = "";
    int arrayIndex = klass.indexOf("[");
    if (arrayIndex >= 0) {
        arraySuffix = klass.substring(arrayIndex);
        klass = klass.substring(0, arrayIndex);
    }
    // look among imports
    String temp = klass;
    while (temp.length() > 0) {
        for (ImportDescr id : packageDescr.getImports()) {
            String fqKlass = id.getTarget();
            if (fqKlass.endsWith("." + temp)) {
                // logger.info("Replace supertype " + sup + " with full name " + id.getTarget());
                fqKlass = fqKlass.substring(0, fqKlass.lastIndexOf(temp)) + klass;
                return arrayIndex < 0 ? fqKlass : fqKlass + arraySuffix;
            }
        }
        temp = temp.substring(0, Math.max(0, temp.lastIndexOf('.')));
    }
    // look among local declarations
    if (pkgRegistry != null) {
        for (String declaredName : pkgRegistry.getPackage().getTypeDeclarations().keySet()) {
            if (declaredName.equals(klass)) {
                TypeDeclaration typeDeclaration = pkgRegistry.getPackage().getTypeDeclaration(declaredName);
                if (typeDeclaration.getTypeClass() != null) {
                    klass = typeDeclaration.getTypeClass().getName();
                }
            }
        }
    }
    if ((klass != null) && (!klass.contains(".")) && (packageDescr.getNamespace() != null && !packageDescr.getNamespace().isEmpty())) {
        for (AbstractClassTypeDeclarationDescr td : packageDescr.getClassAndEnumDeclarationDescrs()) {
            if (klass.equals(td.getTypeName())) {
                if (td.getType().getFullName().contains(".")) {
                    klass = td.getType().getFullName();
                }
            }
        }
    }
    return arrayIndex < 0 ? klass : klass + arraySuffix;
}
Also used : AbstractClassTypeDeclarationDescr(org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Aggregations

AbstractClassTypeDeclarationDescr (org.drools.compiler.lang.descr.AbstractClassTypeDeclarationDescr)13 TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)8 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 ImportDescr (org.drools.compiler.lang.descr.ImportDescr)4 QualifiedName (org.drools.compiler.lang.descr.QualifiedName)4 TypeDeclaration (org.drools.core.rule.TypeDeclaration)4 LinkedHashMap (java.util.LinkedHashMap)3 EnumDeclarationDescr (org.drools.compiler.lang.descr.EnumDeclarationDescr)3 ClassDefinition (org.drools.core.factmodel.ClassDefinition)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)2 AccumulateImportDescr (org.drools.compiler.lang.descr.AccumulateImportDescr)2 FunctionImportDescr (org.drools.compiler.lang.descr.FunctionImportDescr)2 Traitable (org.drools.core.factmodel.traits.Traitable)2 Collection (java.util.Collection)1 AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)1 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)1 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)1