Fix field cache to let child class fields take precedence over parent - #12626
Open
gnodet wants to merge 1 commit into
Open
Fix field cache to let child class fields take precedence over parent#12626gnodet wants to merge 1 commit into
gnodet wants to merge 1 commit into
Conversation
…n over parent buildFieldCache() used put() which let parent fields overwrite child fields with the same name, because DeclaredMembers iterates child-first then parent. This broke Mojo parameter injection for shadowed fields (e.g. the 'skip' field in TestResourcesMojo vs ResourcesMojo). Switch to putIfAbsent() — matching the method cache — so the child's field takes precedence. Reported-in: apache/maven-resources-plugin#497 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
gnodet
commented
Jul 30, 2026
gnodet
left a comment
Contributor
Author
There was a problem hiding this comment.
LGTM ✅
Clean one-line bug fix that changes put() to putIfAbsent() in buildFieldCache(), making field cache resolution consistent with the existing method cache behavior. The fix correctly ensures child class fields take precedence over shadowed parent fields when DeclaredMembers iterates child-first.
Details:
- The fix is verified correct: Sisu's
DeclaredMembers/MemberIteratorstarts with the given class and walks togetSuperclass(), confirming child-first iteration order. Withput(), the parent's field entry overwrites the child's; withputIfAbsent(), the child's field (seen first) is correctly preserved. - The method cache at line 198 already uses
putIfAbsent(), so this fix resolves an asymmetry between the two caches introduced in the originald3bd71bbdfcommit ("perf: optimize CompositeBeanHelper with reflection caching"). - The test bean hierarchy (
ParentBean/ChildBeanboth declaringprivate boolean skip) accurately mirrors the real-world scenario in apache/maven-resources-plugin#497 whereResourcesMojoandTestResourcesMojoboth declareskip. - Well-tested with two new tests covering the direct fix and cache consistency.
This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.
Claude Code on behalf of gnodet
slachiewicz
added a commit
to slachiewicz/maven-resources-plugin
that referenced
this pull request
Aug 4, 2026
apache/maven#12626 fixes the cause in the core configurator, so record what lets this be removed rather than leaving it to be rediscovered. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
slachiewicz
added a commit
to apache/maven-resources-plugin
that referenced
this pull request
Aug 4, 2026
) * Build against Maven 4.0.0-rc-6 Move the Maven 4 version property to the current RC and pin the same version explicitly in the Verify workflow, so CI does not depend on the maven4-version default in maven-gh-actions-shared. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Honour <skip> on the testResources goal again MRESOURCES-131 sets <skip>true</skip> in pluginManagement and asserts the test resource is not copied. It has been failing since 4.0.0-rc-5: the main resources goal skips, testResources does not. Both ResourcesMojo and TestResourcesMojo declare a private field named "skip". They collapse into a single descriptor parameter for the testResources goal, and the configurator writes the superclass field — so this class's own field stays false no matter what the build configured, and the guard never fires. Reading both restores the documented behaviour while keeping maven.test.skip and maven.resources.skip working as before. Renaming the field so the two stop shadowing does not work: the descriptor generator then sees two parameters called "skip" for one goal and fails with "skip has been declared multiple times in mojo with goal: testResources". mvn verify -Prun-its: all ITs pass, 24 unit tests, 0 failures. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Note that the skip workaround is temporary apache/maven#12626 fixes the cause in the core configurator, so record what lets this be removed rather than leaving it to be rediscovered. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
EnhancedCompositeBeanHelper.buildFieldCache()to useputIfAbsent()instead ofput(), so child class fields take precedence over parent fields with the same nameParentBean/ChildBeanwith sameskipfield)Problem
When a child Mojo class shadows a parent's field (e.g. both
ResourcesMojoandTestResourcesMojodeclareprivate boolean skip), the field cache incorrectly resolves to the parent's field. This causes POM<configuration><skip>true</skip></configuration>to inject into the parent'sskipfield but not the child's — breakingTestResourcesMojo's skip behavior.Root cause:
DeclaredMembersiterates child-first then parent. The field cache usedput()which overwrites the child entry with the parent's. The method cache already correctly usesputIfAbsent()— this was an asymmetry.Fix
One-line change:
fieldMap.put(...)→fieldMap.putIfAbsent(...)inbuildFieldCache().This matches the method cache behavior and ensures the first-seen field (from the child class) wins.
Reported in: apache/maven-resources-plugin#497
Test plan
testSetPropertyOnShadowedFieldInjectsIntoChildField— verifies child field receives the injected valuetestShadowedFieldCacheConsistency— verifies cache reuse preserves correct field resolutionEnhancedCompositeBeanHelperTesttests pass🤖 Generated with Claude Code