use of org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr in project drools by kiegroup.
the class ClassHierarchyManager method sortByHierarchy.
/**
* Utility method to sort declared beans. Linearizes the hierarchy,
* i.e.generates a sequence of declaration such that, if Sub is subclass of
* Sup, then the index of Sub will be > than the index of Sup in the
* resulting collection. This ensures that superclasses are processed before
* their subclasses
*/
protected List<AbstractClassTypeDeclarationDescr> sortByHierarchy(Collection<AbstractClassTypeDeclarationDescr> unsortedDescrs, KnowledgeBuilderImpl kbuilder) {
taxonomy = new HashMap<>();
Map<QualifiedName, AbstractClassTypeDeclarationDescr> cache = new HashMap<>();
for (AbstractClassTypeDeclarationDescr tdescr : unsortedDescrs) {
cache.put(tdescr.getType(), tdescr);
}
for (AbstractClassTypeDeclarationDescr tdescr : unsortedDescrs) {
QualifiedName name = tdescr.getType();
Collection<QualifiedName> supers = taxonomy.get(name);
if (supers == null) {
supers = new ArrayList<>();
taxonomy.put(name, supers);
} else {
kbuilder.addBuilderResult(new TypeDeclarationError(tdescr, "Found duplicate declaration for type " + tdescr.getType()));
}
boolean circular = false;
for (QualifiedName sup : tdescr.getSuperTypes()) {
if (!Object.class.getName().equals(name.getFullName())) {
if (!hasCircularDependency(tdescr.getType(), sup, taxonomy)) {
if (cache.containsKey(sup)) {
supers.add(sup);
}
} else {
circular = true;
kbuilder.addBuilderResult(new TypeDeclarationError(tdescr, "Found circular dependency for type " + tdescr.getTypeName()));
break;
}
}
}
if (circular) {
tdescr.getSuperTypes().clear();
}
}
for (AbstractClassTypeDeclarationDescr tdescr : unsortedDescrs) {
for (TypeFieldDescr field : tdescr.getFields().values()) {
QualifiedName name = tdescr.getType();
QualifiedName typeName = new QualifiedName(field.getPattern().getGenericType().getRawType());
if (!hasCircularDependency(name, typeName, taxonomy)) {
if (cache.containsKey(typeName)) {
taxonomy.get(name).add(typeName);
}
} else {
field.setRecursive(true);
}
}
}
List<QualifiedName> sorted = new HierarchySorter<QualifiedName>().sort(taxonomy);
ArrayList list = new ArrayList(sorted.size());
for (QualifiedName name : sorted) {
list.add(cache.get(name));
}
return list;
}
use of org.drools.drl.ast.descr.AbstractClassTypeDeclarationDescr in project drools by kiegroup.
the class TypeDeclarationFactory method processAnnotations.
public static void processAnnotations(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type) {
Role role = getTypedAnnotation(typeDescr, Role.class);
if (role != null) {
type.setRole(role.value());
}
TypeSafe typeSafe = getTypedAnnotation(typeDescr, 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));
}
use of org.drools.drl.ast.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.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;
}
use of org.drools.drl.ast.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;
}
Aggregations