Search in sources :

Example 1 with TraitClassBuilderImpl

use of org.drools.traits.core.factmodel.TraitClassBuilderImpl in project drools by kiegroup.

the class StandaloneTraitFactory method extendAsProperTrait.

private Class<T> extendAsProperTrait(Class<T> trait) {
    String extName = trait.getName() + SUFFIX;
    if (!classLoader.isClassInUse(extName)) {
        try {
            ClassDefinition extDef = new ClassDefinition(extName);
            extDef.setSuperClass(Object.class.getName());
            AnnotationDefinition annot = new AnnotationDefinition(Trait.class.getName());
            extDef.addAnnotation(annot);
            String[] supers = new String[] { Thing.class.getName(), trait.getName() };
            extDef.setInterfaces(supers);
            byte[] ext = new TraitClassBuilderImpl().buildClass(extDef, classLoader);
            Class<?> klass = registerAndLoadTypeDefinition(extName, ext);
            ClassDefinition tDef = buildClassDefinition(trait, trait);
            tDef.setDefinedClass(klass);
            getTraitRegistry().addTrait(tDef);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    try {
        return (Class<T>) Class.forName(extName, false, classLoader);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) TraitClassBuilderImpl(org.drools.traits.core.factmodel.TraitClassBuilderImpl) Trait(org.drools.core.factmodel.traits.Trait) ClassDefinition(org.drools.core.factmodel.ClassDefinition) IOException(java.io.IOException) LogicalTypeInconsistencyException(org.drools.traits.core.factmodel.LogicalTypeInconsistencyException)

Example 2 with TraitClassBuilderImpl

use of org.drools.traits.core.factmodel.TraitClassBuilderImpl in project drools by kiegroup.

the class TraitsTypeDeclarationBuilderImpl method postGenerateDeclaredBean.

@Override
protected void postGenerateDeclaredBean(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type, ClassDefinition def, PackageRegistry pkgRegistry) {
    traitRegistry = ((TraitKnowledgePackageImpl) pkgRegistry.getPackage()).getTraitRegistry();
    if (typeDescr.hasAnnotation(Traitable.class) || (!type.getKind().equals(TypeDeclaration.Kind.TRAIT) && kbuilder.getPackageRegistry().containsKey(def.getSuperClass()) && traitRegistry.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
            }
        }
        traitRegistry.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() + TraitFactoryImpl.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, new TraitClassBuilderImpl());
                    try {
                        Class<?> clazz = pkgRegistry.getTypeResolver().resolveType(tempDescr.getType().getFullName());
                        tempDeclr.setTypeClass(clazz);
                        traitRegistry.addTrait(tempDef.getClassName().replace(TraitFactoryImpl.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);
                    traitRegistry.addTrait(def);
                }
            } catch (ClassNotFoundException cnfe) {
            // we already know the class exists
            }
        } else {
            if (def.getClassName().endsWith(TraitFactoryImpl.SUFFIX)) {
                traitRegistry.addTrait(def.getClassName().replace(TraitFactoryImpl.SUFFIX, ""), def);
            } else {
                traitRegistry.addTrait(def);
            }
        }
    }
}
Also used : TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) AbstractClassTypeDeclarationDescr(org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) FieldDefinition(org.drools.core.factmodel.FieldDefinition) TraitClassBuilderImpl(org.drools.traits.core.factmodel.TraitClassBuilderImpl) Traitable(org.drools.core.factmodel.traits.Traitable) ClassDefinition(org.drools.core.factmodel.ClassDefinition) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Aggregations

ClassDefinition (org.drools.core.factmodel.ClassDefinition)2 TraitClassBuilderImpl (org.drools.traits.core.factmodel.TraitClassBuilderImpl)2 IOException (java.io.IOException)1 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)1 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)1 AnnotationDefinition (org.drools.core.factmodel.AnnotationDefinition)1 FieldDefinition (org.drools.core.factmodel.FieldDefinition)1 Trait (org.drools.core.factmodel.traits.Trait)1 Traitable (org.drools.core.factmodel.traits.Traitable)1 TypeDeclaration (org.drools.core.rule.TypeDeclaration)1 AbstractClassTypeDeclarationDescr (org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr)1 TypeDeclarationDescr (org.drools.drl.ast.descr.TypeDeclarationDescr)1 LogicalTypeInconsistencyException (org.drools.traits.core.factmodel.LogicalTypeInconsistencyException)1