-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
@TempDir
field in super class skipped when it has same name as a @TempDir
field in subclass
#3532
Comments
Tentatively slated for inclusion in @marcphilipp, I've already submitted PR #3533 (which contains some TODOs), but if I don't complete it before you release |
This commit includes a fix with two simple test classes that demonstrate the issue. TODO: - add "formal" tests in ReflectionUtilsTests and AnnotationUtilsTests - add release note entries See junit-team#3498 Closes junit-team#3532
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. TODO: - add release note entries See junit-team#3498 Closes junit-team#3532
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. TODO: - add release note entries See junit-team#3498 Closes junit-team#3532
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
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)
This commit consistently applies method predicates in search algorithms analogous to the application of field predicates. See junit-team#3498 See junit-team#3532 Closes junit-team#3534
This commit consistently applies method predicates in search algorithms analogous to the application of field predicates. See junit-team#3498 See junit-team#3532 Closes junit-team#3534
This has been merged into |
Out of curiosity (not because my tests currently do this), this only covers the case where one field is |
Hi @Marcono1234,
Yes, that's correct. A field supersedes a package private field with the same name in a superclass when the superclass resides in a different package unless a filter ( That's the status quo, albeit perhaps not very intuitive. This also applies to lifecycle and test methods (and well, for any methods looked up via JUnit 5's reflection support). We've had numerous discussions about this behavior over the years, and I think it might be worth discussing again within the team. In light of that, I plan to create a new issue to revisit the topic. Thanks for bringing it up! 👍 Related Issues(so that I don't lose track of them)
|
Closely related to: |
Overview
After fixing #3498, I realized that the same types of bugs exist for finding fields in a type hierarchy.
The fix for fields should be analogous to the fix for methods: apply field predicate before searching type hierarchy.
Example
SuperclassTempDirTests
passes, butSubclassTempDirTests
fails unless you rename one of the@TempDir
fields to something other thantempDir
.Related Issues
@BeforeAll
method in super class skipped when it has same name as a@BeforeEach
method in subclass #3498Deliverables
The text was updated successfully, but these errors were encountered: