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;
}
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);
}
}
}
}
Aggregations