You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fesod.apache.org by "bengbengbalabalabeng (via GitHub)" <gi...@apache.org> on 2026/08/11 12:11:22 UTC
[PR] fix: prioritize subclass fields and ignore superclass annotations on field shadowing [fesod]
bengbengbalabalabeng opened a new pull request, #1007:
URL: https://github.com/apache/fesod/pull/1007
## Purpose of the pull request
Similar to #427
Fix the issue where a subclass attribute with the same name (field shadowing) in a class inheritance chain mistakenly inherits the parent class annotation (such as `@DateTimeFormat`), causing abnormal sheet export formatting.
*Special thanks to @nkuprins for identifying this issue in Discussion #919 !*
## What's changed?
Refactored the field collection logic in `ClassUtils#doDeclaredFieldContentMap` and extracted it into a reusable `FieldUtils#resolveFields` utility method.
Scope of affected annotations:
- `@ContentStyle`
- `@ContentFontStyle`
- `@ExcelProperty` (converter attribute)
- `@DateTimeFormat`
- `@NumberFormat`
## Checklist
- [x] I have read the [Contributor Guide](https://fesod.apache.org/community/contribution/).
- [x] I have written the necessary doc or comment.
- [x] I have added the necessary unit tests and all cases have passed.
--
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: commits-unsubscribe@fesod.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org
Re: [PR] fix: prioritize subclass fields and ignore superclass annotations on field shadowing [fesod]
Posted by "delei (via GitHub)" <gi...@apache.org>.
delei merged PR #1007:
URL: https://github.com/apache/fesod/pull/1007
--
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: commits-unsubscribe@fesod.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org
Re: [PR] fix: prioritize subclass fields and ignore superclass annotations on field shadowing [fesod]
Posted by "delei (via GitHub)" <gi...@apache.org>.
delei commented on code in PR #1007:
URL: https://github.com/apache/fesod/pull/1007#discussion_r3838533993
##########
fesod-sheet/src/main/java/org/apache/fesod/sheet/util/FieldUtils.java:
##########
@@ -178,4 +186,33 @@ public static Field getField(final Class<?> cls, final String fieldName, final b
}
return match;
}
+
+ /**
+ * Resolves and retrieves all declared fields from the specified class and its inheritance hierarchy.
+ * If a field with the same name (resolved by CGLIB) is declared in both a subclass and a superclass,
+ * the subclass field definition takes precedence.
+ *
+ * @param cls the target {@link Class}, must not be {@code null}
+ * @return a {@link List} containing all resolved fields, or an empty list if no fields are found
+ */
+ public static List<Field> resolveFields(Class<?> cls) {
Review Comment:
How about this method name?
```suggestion
public static List<Field> collectResolvedFields(Class<?> cls) {
```
<details>
<summary>AI Assistant</summary>
I think `resolveFields` is a bit too generic for what this method actually does.
The implementation is not only resolving fields, but also walking the inheritance hierarchy, filtering out static/synthetic fields, and applying shadowing precedence so that subclass fields win over superclass fields.
To make that behavior clearer, I’d recommend renaming it to `collectResolvedFields`. This name better communicates that the method is collecting the final resolved field set after inheritance and shadowing are taken into account.
</details>
--
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: commits-unsubscribe@fesod.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org
Re: [PR] fix: prioritize subclass fields and ignore superclass annotations on field shadowing [fesod]
Posted by "delei (via GitHub)" <gi...@apache.org>.
delei commented on code in PR #1007:
URL: https://github.com/apache/fesod/pull/1007#discussion_r3838548548
##########
fesod-sheet/src/main/java/org/apache/fesod/sheet/util/FieldUtils.java:
##########
@@ -178,4 +186,33 @@ public static Field getField(final Class<?> cls, final String fieldName, final b
}
return match;
}
+
+ /**
+ * Resolves and retrieves all declared fields from the specified class and its inheritance hierarchy.
+ * If a field with the same name (resolved by CGLIB) is declared in both a subclass and a superclass,
+ * the subclass field definition takes precedence.
+ *
+ * @param cls the target {@link Class}, must not be {@code null}
+ * @return a {@link List} containing all resolved fields, or an empty list if no fields are found
+ */
+ public static List<Field> resolveFields(Class<?> cls) {
+ Validate.isTrue(cls != null, "The class must not be null");
+
+ List<Field> result = new ArrayList<>();
+ Set<String> fieldNames = new HashSet<>();
+ Class<?> tempClass = cls;
+ while (tempClass != null && tempClass != Object.class) {
+ for (Field field : tempClass.getDeclaredFields()) {
+ boolean shouldSkip = Modifier.isStatic(field.getModifiers())
+ || field.isSynthetic()
+ || !fieldNames.add(resolveCglibFieldName(field));
+ if (shouldSkip) {
+ continue;
+ }
+ result.add(field);
+ }
+ tempClass = tempClass.getSuperclass();
+ }
+ return Collections.unmodifiableList(result);
Review Comment:
As a public utility method, I suggest returning result directly instead of Collections.unmodifiableList(result).
Otherwise, callers may hit UnsupportedOperationException during common follow-up operations (e.g., add / remove / sort), which can be inconsistent with typical utility-method expectations.
--
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: commits-unsubscribe@fesod.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org
Re: [PR] fix: prioritize subclass fields and ignore superclass annotations on field shadowing [fesod]
Posted by "bengbengbalabalabeng (via GitHub)" <gi...@apache.org>.
bengbengbalabalabeng commented on code in PR #1007:
URL: https://github.com/apache/fesod/pull/1007#discussion_r3838592465
##########
fesod-sheet/src/main/java/org/apache/fesod/sheet/util/FieldUtils.java:
##########
@@ -178,4 +186,33 @@ public static Field getField(final Class<?> cls, final String fieldName, final b
}
return match;
}
+
+ /**
+ * Resolves and retrieves all declared fields from the specified class and its inheritance hierarchy.
+ * If a field with the same name (resolved by CGLIB) is declared in both a subclass and a superclass,
+ * the subclass field definition takes precedence.
+ *
+ * @param cls the target {@link Class}, must not be {@code null}
+ * @return a {@link List} containing all resolved fields, or an empty list if no fields are found
+ */
+ public static List<Field> resolveFields(Class<?> cls) {
Review Comment:
I think having `collect` and `resolved` together is a bit redundant.
How about `resolveAllFields`? (Reference: `FieldUtils#getAllFieldsList` in Apache Commons Lang).
--
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: commits-unsubscribe@fesod.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org
Re: [PR] fix: prioritize subclass fields and ignore superclass annotations on field shadowing [fesod]
Posted by "delei (via GitHub)" <gi...@apache.org>.
delei commented on code in PR #1007:
URL: https://github.com/apache/fesod/pull/1007#discussion_r3838598503
##########
fesod-sheet/src/main/java/org/apache/fesod/sheet/util/FieldUtils.java:
##########
@@ -178,4 +186,33 @@ public static Field getField(final Class<?> cls, final String fieldName, final b
}
return match;
}
+
+ /**
+ * Resolves and retrieves all declared fields from the specified class and its inheritance hierarchy.
+ * If a field with the same name (resolved by CGLIB) is declared in both a subclass and a superclass,
+ * the subclass field definition takes precedence.
+ *
+ * @param cls the target {@link Class}, must not be {@code null}
+ * @return a {@link List} containing all resolved fields, or an empty list if no fields are found
+ */
+ public static List<Field> resolveFields(Class<?> cls) {
Review Comment:
`resolveAllFields` is good.
--
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: commits-unsubscribe@fesod.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org