Skip to content

[java] Fix #6547: NonSerializableClass checks collection/map generic element types - #6942

Merged
UncleOwen merged 6 commits into
pmd:mainfrom
fudianchn:fix/nonserializableclass-generic-types-6547
Aug 11, 2026
Merged

UncleOwen merged 6 commits into
pmd:mainfrom
fudianchn:fix/nonserializableclass-generic-types-6547

Conversation

@fudianchn

Copy link
Copy Markdown
Contributor

What

NonSerializableClass now also reports fields whose collection/map generic element or value type is non-serializable — e.g. List<NotSerializable> items; or Map<String, NotSerializable> map; — not just fields whose own raw type is non-serializable.

Closes #6547.

Why

The rule checked only whether a field's declared type implements Serializable. List/Map themselves are Serializable, so List<NotSerializable> slipped through — yet serializing such a field still fails, because the element/value type isn't serializable. The container being serializable doesn't make its contents serializable.

How

Add hasNonSerializableElementType(typeMirror), invoked alongside the existing type check. For a field whose type is a parameterized Iterable or Map (TypeTestUtil.isA), it walks JClassType.getTypeArgs() and runs each type argument through the same serializability check used for the field type itself (reusing the existing CHECK_ABSTRACT_TYPES property semantics and isUnresolved() guard). The pre-existing check is refactored into isNotSerializableType(JTypeMirror) so both paths share one definition; behaviour for non-container fields is unchanged.

It is deliberately conservative to avoid false positives — only concrete class type arguments are evaluated:

  • type variables (List<T>), wildcards (List<? extends Serializable>), bounded type vars (List<T extends NotSerializable>), array types, and intersection types are skipped, since their serializability can't be determined statically;
  • unresolved type arguments are skipped via the existing guard;
  • interface/abstract/Object element types are excluded when CHECK_ABSTRACT_TYPES is off (the default), consistent with how the rule treats the field type itself.

Testing

  • Added cases to NonSerializableClass.xml: List<NotSerializable> and Map<String, NotSerializable> → 2 findings, plus negatives — List<String> (serializable element), List<T> / Map<String,T> (type variables), List<? extends Serializable> (wildcard), List<T extends NotSerializable> (bounded type var) → 0 findings.
  • Verified fails before the fix (expected: <2> but was: <0>) and passes after (24/24, including all pre-existing cases).
  • ./mvnw -pl pmd-java checkstyle:check → 0 violations.

Added a release_notes.md entry under the java-errorprone section.

@pmd-actions-helper

pmd-actions-helper Bot commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 2 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 16 errors and 9 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-08-11 12:29:51+00:00 for 578a408)

zbynek and others added 2 commits August 7, 2026 22:19
…ement check

Per review feedback (pmd#6942): a bare Iterable<NotSerializable> does not
guarantee that it actually holds the instances — Iterable only promises
an iterator, whose elements need not be the container's stored values
(e.g. generators, adapters). Collection is the contract for a container
that holds its elements, which is what serialization actually traverses.

Switch the container type guard from Iterable to Collection (Map is
already handled separately and is unaffected, since Map does not extend
Collection). Add a regression test asserting that a bare
Iterable<NotSerializable> field is not reported.

Signed-off-by: 付典 <[email protected]>
@UncleOwen UncleOwen added this to the 7.27.0 milestone Aug 11, 2026

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@UncleOwen
UncleOwen merged commit 8a90370 into pmd:main Aug 11, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] NonSerializableClass: False negative for generic element/value types of collections and maps

3 participants