use of org.drools.drl.ast.descr.TypeDeclarationDescr 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);
}
}
}
}
use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.
the class TypeDeclarationDescrVisitor method visit.
public void visit(List<TypeDeclarationDescr> typeDeclarationDescrs) {
for (TypeDeclarationDescr typeDeclaration : typeDeclarationDescrs) {
Import objectImport = data.getImportByName(typeDeclaration.getTypeName());
String objectTypeName;
if (objectImport == null) {
objectTypeName = typeDeclaration.getTypeName();
} else {
objectTypeName = objectImport.getName();
}
ObjectType objectType = this.data.getObjectTypeByFullName(objectTypeName);
if (objectType == null) {
objectType = new ObjectType(typeDeclaration);
objectType.setName(typeDeclaration.getTypeName());
objectType.setFullName(typeDeclaration.getTypeName());
data.add(objectType);
}
for (String fieldName : typeDeclaration.getFields().keySet()) {
Field field = data.getFieldByObjectTypeAndFieldName(objectType.getFullName(), fieldName);
if (field == null) {
field = ObjectTypeFactory.createField(typeDeclaration.getFields().get(fieldName), fieldName, objectType);
field.setFieldType(typeDeclaration.getFields().get(fieldName).getPattern().getObjectType());
data.add(field);
}
}
for (AnnotationDescr annDescr : typeDeclaration.getAnnotations()) {
Map<String, Object> values = typeDeclaration.getAnnotation(annDescr.getName()).getValueMap();
for (String value : values.keySet()) {
objectType.getMetadata().put(annDescr.getName(), value);
}
}
}
}
use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.
the class DescrTypeDefinition method typeFieldsSortedByPosition.
private List<TypeFieldDescr> typeFieldsSortedByPosition(List<FieldDefinition> inheritedFields) {
Collection<TypeFieldDescr> typeFields = typeDeclarationDescr.getFields().values().stream().filter(f -> inheritedFields.stream().map(FieldDefinition::getFieldName).noneMatch(name -> name.equals(f.getFieldName()))).collect(Collectors.toList());
TypeFieldDescr[] sortedTypes = new TypeFieldDescr[typeFields.size()];
List<TypeFieldDescr> nonPositionalFields = new ArrayList<>();
for (TypeFieldDescr descr : typeFields) {
AnnotationDescr ann = descr.getAnnotation("Position");
if (ann == null) {
nonPositionalFields.add(descr);
} else {
int pos = Integer.parseInt(ann.getValue().toString());
if (pos >= sortedTypes.length) {
errors.add(new TypeDeclarationError(typeDeclarationDescr, "Out of range position " + pos + " for field '" + descr.getFieldName() + "' on class " + typeDeclarationDescr.getTypeName()));
} else if (sortedTypes[pos] != null) {
errors.add(new TypeDeclarationError(typeDeclarationDescr, "Duplicated position " + pos + " for field '" + descr.getFieldName() + "' on class " + typeDeclarationDescr.getTypeName()));
} else {
sortedTypes[pos] = descr;
}
}
}
if (!errors.isEmpty()) {
return Collections.emptyList();
}
int counter = 0;
for (TypeFieldDescr descr : nonPositionalFields) {
for (; sortedTypes[counter] != null; counter++) ;
sortedTypes[counter++] = descr;
}
return Arrays.asList(sortedTypes);
}
use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.
the class POJOGenerator method findPOJOorGenerate.
public void findPOJOorGenerate() {
TypeResolver typeResolver = pkg.getTypeResolver();
Set<String> generatedPojos = new HashSet<>();
for (TypeDeclarationDescr typeDescr : packageDescr.getTypeDeclarations()) {
if (!generatedPojos.add(typeDescr.getFullTypeName())) {
builder.addBuilderResult(new DuplicatedDeclarationError(typeDescr.getFullTypeName()));
continue;
}
try {
Class<?> type = typeResolver.resolveType(typeDescr.getFullTypeName());
checkRedeclarationCompatibility(type, typeDescr);
processTypeMetadata(type, typeDescr.getAnnotations());
} catch (ClassNotFoundException e) {
createPOJO(typeDescr);
}
}
for (EnumDeclarationDescr enumDescr : packageDescr.getEnumDeclarations()) {
try {
Class<?> type = typeResolver.resolveType(enumDescr.getFullTypeName());
processTypeMetadata(type, enumDescr.getAnnotations());
} catch (ClassNotFoundException e) {
TypeDeclaration generatedEnum = new EnumGenerator().generate(enumDescr);
packageModel.addGeneratedPOJO(generatedEnum);
addTypeMetadata(enumDescr.getTypeName());
}
}
}
use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testTypeDeclarationWithFieldMetadata.
@Test
public void testTypeDeclarationWithFieldMetadata() throws Exception {
PackageDescr pkgDescr = new PackageDescr("org.drools.mvel.compiler.test");
TypeDeclarationDescr typeDescr = new TypeDeclarationDescr("TypeWithFieldMeta");
TypeFieldDescr f1 = new TypeFieldDescr("field", new PatternDescr("String"));
f1.addAnnotation("custom", null);
typeDescr.addField(f1);
pkgDescr.addTypeDeclaration(typeDescr);
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
builder.addPackage(pkgDescr);
assertFalse(builder.hasErrors());
InternalKnowledgePackage bp = builder.getPackage(pkgDescr.getName());
final FactType factType = bp.getFactType("org.drools.mvel.compiler.test.TypeWithFieldMeta");
assertNotNull(factType);
final FactField field = factType.getField("field");
assertNotNull(field);
final Map<String, Object> fieldMetaData = field.getMetaData();
assertNotNull("No field-level custom metadata got compiled", fieldMetaData);
assertTrue("Field metadata does not include expected value", fieldMetaData.containsKey("custom"));
}
Aggregations