drccrd commented on code in PR #6785:
URL:
https://github.com/apache/incubator-kie-drools/pull/6785#discussion_r3594565919
##########
drools-model/drools-model-codegen/src/main/java/org/drools/model/codegen/execmodel/generator/declaredtype/DescrTypeDefinition.java:
##########
@@ -165,13 +180,95 @@ private static Optional<TypeDeclarationDescr>
getSuperType(TypeDeclarationDescr
@Override
public List<FieldDefinition> findInheritedDeclaredFields() {
- List<FieldDefinition> fields = findInheritedDeclaredFields(new
ArrayList<>(), getSuperType(typeDeclarationDescr, packageDescr));
- if (fields.isEmpty()) {
- abstractClass.ifPresent(superClass ->
fields.addAll(inheritedFieldsFromSuperClass(superClass)));
+ return inheritedFieldsOf(typeDeclarationDescr, packageDescr,
typeResolver);
+ }
+
+ /**
+ * Computes the inherited (supertype) fields of a declared type,
base-first, following the
+ * supertype chain across packages and, at the end of the chain, into a
resolvable Java
+ * superclass. This covers a declared type extending another declared type
in the same OR a
+ * different package, and a declared type (possibly several levels up)
extending a Java class on
+ * the classpath. A field-less re-declaration of a classpath class
("declare X end") does not
+ * hide that class's {@link Position} fields. See
+ * DeclaredTypesTest#testExtendPojoInheritedFieldsConstructor and the
cross-package inheritance
+ * cases.
+ */
+ private List<FieldDefinition> inheritedFieldsOf(TypeDeclarationDescr td,
PackageDescr pkgDescr, TypeResolver resolver) {
+ List<FieldDefinition> fields = new ArrayList<>();
+ String superName = td.getSuperTypeName();
+ if (superName == null) {
+ return fields;
+ }
+
+ Optional<DeclaredSuperType> declaredSuper =
findDeclaredSuperType(superName, pkgDescr);
+ if (declaredSuper.isPresent()) {
+ DeclaredSuperType st = declaredSuper.get();
+ // the super's own inherited fields first, then the super's own
declared fields
+ fields.addAll(inheritedFieldsOf(st.descr, st.packageDescr,
st.resolver));
+
st.descr.getFields().values().stream().map(DescrFieldDefinition::new).forEach(fields::add);
+ if (!fields.isEmpty()) {
+ return fields;
+ }
+ // The declared supertype chain contributed no fields: it may be a
field-less
+ // re-declaration of a Java class on the classpath ("declare X
end" over an imported
+ // class), so fall through to that class's @Position fields.
}
+
+ // The supertype is (or re-declares) a Java class. Resolve it in the
declaring package's
+ // scope (its imports) and contribute its @Position fields.
+ resolver.resolveType(superName)
+ .filter(c -> !c.isInterface())
+ .ifPresent(c ->
fields.addAll(inheritedFieldsFromSuperClass(c)));
return fields;
}
+ /**
+ * Locates a declared supertype by simple name, searching the current
package first then every
+ * other package in the build, returning the descriptor together with its
declaring package's
+ * descriptor and type resolver (needed to resolve that package's own
supertypes and imports).
+ */
+ private Optional<DeclaredSuperType> findDeclaredSuperType(String
superName, PackageDescr currentPkgDescr) {
+ Optional<TypeDeclarationDescr> inCurrent =
currentPkgDescr.getTypeDeclarations().stream()
+ .filter(td -> td.getTypeName().equals(superName))
+ .findFirst();
+ if (inCurrent.isPresent()) {
+ return Optional.of(new DeclaredSuperType(inCurrent.get(),
currentPkgDescr, typeResolver));
+ }
+ if (allPackages != null) {
+ for (PackageDescr pkg : allPackages) {
+ Optional<TypeDeclarationDescr> found =
pkg.getTypeDeclarations().stream()
+ .filter(td -> td.getTypeName().equals(superName))
+ .findFirst();
+ if (found.isPresent()) {
+ return Optional.of(new DeclaredSuperType(found.get(), pkg,
resolverForPackage(pkg.getNamespace())));
+ }
+ }
+ }
+ return Optional.empty();
+ }
Review Comment:
made more accurate in
[5c738f1](https://github.com/apache/incubator-kie-drools/pull/6785/commits/5c738f14ce93b71e76811c8f2da74ab4d9952021)
- DescrTypeDefinition no longer resolves a declared supertype by first match
on simple name across all packages. It prefers a local declaration, then an
explicit qualifier or single-type import to pin the declaring package, and
falls back to a namespace-ordered (deterministic) scan - so a
same-simple-name
type in an unrelated package is never picked and resolution no longer
depends
on package iteration order. Also threads the correct per-package
TypeResolver
through the supertype chain.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]