Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply field predicate before searching type hierarchy #3533

Conversation

sbrannen
Copy link
Member

@sbrannen sbrannen commented Nov 3, 2023

Overview

This PR includes a fix for #3532 with two simple test classes that demonstrate the issue.

Related Issues

Deliverables

  • Fix the bug
  • Add "formal" tests in ReflectionUtilsTests and AnnotationUtilsTests
  • Document in the Release Notes

Prior to this commit, findFields() and streamFields() in
ReflectionSupport as well as findAnnotatedFields() and
findAnnotatedFieldValues() in AnnotationSupport first searched for all
fields in the type hierarchy and then applied the user-supplied
predicate (or "is annotated?" predicate) afterwards.

This resulted in fields in subclasses incorrectly "shadowing"
package-private fields in superclasses (in a different package) even if
the predicate would otherwise exclude the field in such a subclass.

For example, given a superclass that declares a package-private static
@⁠TempDir "tempDir" field and a subclass (in a different package) that
declares a @⁠TempDir "tempDir" field, when JUnit Jupiter looked up
@⁠TempDir fields for the subclass, the @⁠TempDir "tempDir" field in the
superclass was not found because the @⁠TempDir "tempDir" field shadowed
it based solely on the field signature, ignoring the type of annotation
sought.

To address that, this commit modifies the internal search algorithms in
ReflectionUtils so that field predicates are applied while searching
the hierarchy for fields.

See junit-team#3498
Closes junit-team#3532
Closes junit-team#3533
@sbrannen sbrannen force-pushed the issues/3532-apply-field-predicate-before-searching-hierarchy branch from 7c30fd3 to e36637a Compare November 4, 2023 12:06
@sbrannen sbrannen closed this in f30a8d5 Nov 4, 2023
sbrannen added a commit that referenced this pull request Nov 4, 2023
Prior to this commit, findFields() and streamFields() in
ReflectionSupport as well as findAnnotatedFields() and
findAnnotatedFieldValues() in AnnotationSupport first searched for all
fields in the type hierarchy and then applied the user-supplied
predicate (or "is annotated?" predicate) afterwards.

This resulted in fields in subclasses incorrectly "shadowing"
package-private fields in superclasses (in a different package) even if
the predicate would otherwise exclude the field in such a subclass.

For example, given a superclass that declares a package-private static
@⁠TempDir "tempDir" field and a subclass (in a different package) that
declares a @⁠TempDir "tempDir" field, when JUnit Jupiter looked up
@⁠TempDir fields for the subclass, the @⁠TempDir "tempDir" field in the
superclass was not found because the @⁠TempDir "tempDir" field shadowed
it based solely on the field signature, ignoring the type of annotation
sought.

To address that, this commit modifies the internal search algorithms in
ReflectionUtils so that field predicates are applied while searching
the hierarchy for fields.

See #3498
Closes #3532
Closes #3533

(cherry picked from commit f30a8d5)
Copy link
Member

@marcphilipp marcphilipp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@sbrannen
Copy link
Member Author

Due to #3600, for 5.10.2 we have decided to revert the changes to our field search algorithm introduced in conjunction with this issue.

However, we will address the issue originally raised in this issue when we resolve #3553 in 5.11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@TempDir field in super class skipped when it has same name as a @TempDir field in subclass
2 participants