Visitar URL original
fix: initialize Resource includes/excludes to empty lists by gnodet · Pull Request #399 · apache/maven-filtering · GitHub
Skip to content

fix: initialize Resource includes/excludes to empty lists - #399

Merged
gnodet merged 1 commit into
apache:masterfrom
gnodet:fix/356-resource-null-defaults
Sep 26, 2026
Merged

gnodet merged 1 commit into
apache:masterfrom
gnodet:fix/356-resource-null-defaults

Conversation

@gnodet

@gnodet gnodet commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Resource.getIncludes() and getExcludes() returned null by default, forcing every
consumer to null-check before accessing the list. This led to verbose defensive coding
scattered across call sites.

Root Cause

Resource.java declared includes and excludes as package-private fields without
initialization, so they default to null. The addInclude and addExclude methods
had to lazily allocate the lists, and DefaultMavenResourcesFiltering had to null-check
both in the debug-logging block and in setupScanner.

Fix

  • Initialize includes and excludes to new ArrayList<>() so getters always return
    a non-null, mutable list.
  • Simplify addInclude / addExclude — remove the now-redundant null guards.
  • Clean up DefaultMavenResourcesFiltering:
    • Debug log block: replace resource.getExcludes() == null ? " empty " : resource.getExcludes().toString() with a direct resource.getExcludes() call.
    • setupScanner: simplify the if/else chain with a ternary and drop the outer null check on excludes.

Tests

All 88 existing tests pass (mvn verify). No behavioral change — the lists are still
mutable and setIncludes/setExcludes still accept null for callers that want to
clear them.

Fixes #356

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good direction — eagerly initializing includes / excludes removes a whole class of NPE hazards and simplifies the downstream code.

One gap: setIncludes(List) and setExcludes(List) still accept null, which would re-nullify the field and make the removed null-checks in DefaultMavenResourcesFiltering.setupScanner() / debug-message block crash with an NPE.
Two options (either is fine):

  1. Guard the setters: this.includes = includes != null ? includes : new ArrayList<>() (or throw NullPointerException).
  2. Add @Nullable / document the contract — but then the null-checks in DefaultMavenResourcesFiltering must stay.

Option 1 is consistent with the intent of this PR.

Minor: nonFilteredFiles still uses the old null-init + lazy-init-in-addNonFilteredFile pattern. Worth aligning it in the same commit for consistency, though not blocking.

Overall the change is correct for the normal code path; just the setter hole needs closing.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-review after merge commit 3ed89bb. The merge brought in nonFilteredFiles from #395 but did not address the previous findings.

Previous findings status

Finding Status
setIncludes/setExcludes accept null — NPE downstream ❌ NOT addressed
nonFilteredFiles still uses old null-init pattern ⚠️ Not addressed (minor, not blocking)

Blocker: setIncludes(List) (line 46) and setExcludes(List) (line 54) still do raw this.includes = includes without null-guard. Since this PR removes all null-checks in DefaultMavenResourcesFiltering (setupScanner now calls .isEmpty() directly at line 431, debug block calls .getExcludes() directly at line 166), any caller that does resource.setIncludes(null) will trigger an NPE.

Fix either setter with:

public void setIncludes(List<String> includes) {
    this.includes = includes != null ? includes : new ArrayList<>();
}

(Same for setExcludes.)

The merge commit itself introduced no new issues.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-review after merge commit 3ed89bb. The merge of master is clean, but the primary finding from the previous review is not addressed.

Previous Finding 1 (setter null hole) — NOT ADDRESSED:
setIncludes(List) and setExcludes(List) still do this.includes = includes without a null guard. A caller passing null re-nullifies the field, making the null-check removals in DefaultMavenResourcesFiltering (both setupScanner and the debug log block) crash with NPE. This was the blocking issue from the previous review and it remains open.

Fix both setters:

public void setIncludes(List<String> includes) {
    this.includes = includes != null ? includes : new ArrayList<>();
}
public void setExcludes(List<String> excludes) {
    this.excludes = excludes != null ? excludes : new ArrayList<>();
}

Previous Finding 2 (minor — nonFilteredFiles consistency) — NOT ADDRESSED:
nonFilteredFiles still uses the old null-init + lazy-init-in-addNonFilteredFile pattern. Not blocking, but worth aligning in the same commit.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-review after 1ad9efb — all previous findings addressed.

Previous Finding Status
setIncludes/setExcludes null-guard ✅ Fixed — both setters now coalesce null to new ArrayList<>()
nonFilteredFiles eager init + consistency ✅ Fixed — field eagerly initialized, setNonFilteredFiles null-guarded, addNonFilteredFile lazy-init removed

No new issues introduced. The downstream null-check removals in DefaultMavenResourcesFiltering (debug log and setupScanner) are safe given the non-null guarantees now established in Resource.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet
gnodet force-pushed the fix/356-resource-null-defaults branch from 1ad9efb to 9950b2d Compare September 25, 2026 15:18
… setters

- Initialize includes and excludes to new ArrayList<>() so getters
  always return a non-null, mutable list; remove downstream null-checks
  in DefaultMavenResourcesFiltering (setupScanner, debug-log block) and
  simplify addInclude/addExclude accordingly
- Also eagerly initialize nonFilteredFiles with the same pattern and
  remove the lazy-init in addNonFilteredFile() for consistency
- Add null-guards to setIncludes(), setExcludes(), and setNonFilteredFiles()
  so callers passing null cannot re-nullify fields after construction,
  closing the NPE hole opened by removing the downstream null-checks
@gnodet
gnodet force-pushed the fix/356-resource-null-defaults branch from 9950b2d to c4bca3d Compare September 25, 2026 15:19

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-review after rebase (c4bca3d). Tree content identical to previously approved 1ad9efb — pure rebase, no code changes. All previous findings remain addressed.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-review after rebase (9950b2d). The PR's changes are identical to the previously approved state (1ad9efb) — only the base commit changed (master advanced). All previous findings remain addressed: setters null-guarded, nonFilteredFiles eagerly initialized, downstream null-checks correctly removed. Clean rebase, no new issues.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet gnodet added bug Something isn't working java Pull requests that update Java code labels Sep 26, 2026
@gnodet
gnodet merged commit 01d9c26 into apache:master Sep 26, 2026
8 checks passed
@github-actions github-actions Bot added this to the 4.0.0-beta-2 milestone Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working java Pull requests that update Java code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Resource fields default to null instead of sensible empty values

3 participants