Repository navigation
[java] Fix #6547: NonSerializableClass checks collection/map generic element types - #6942
Merged
UncleOwen merged 6 commits intoAug 11, 2026
Merged
Conversation
…ic element types Signed-off-by: 付典 <[email protected]>
zbynek
reviewed
Aug 7, 2026
Contributor
|
Compared to main: (comment created at 2026-08-11 12:29:51+00:00 for 578a408) |
…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]>
zbynek
approved these changes
Aug 10, 2026
…ric-types-6547 # Conflicts: # docs/pages/release_notes.md
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.
What
NonSerializableClassnow also reports fields whose collection/map generic element or value type is non-serializable — e.g.List<NotSerializable> items;orMap<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/Mapthemselves areSerializable, soList<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 parameterizedIterableorMap(TypeTestUtil.isA), it walksJClassType.getTypeArgs()and runs each type argument through the same serializability check used for the field type itself (reusing the existingCHECK_ABSTRACT_TYPESproperty semantics andisUnresolved()guard). The pre-existing check is refactored intoisNotSerializableType(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:
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;interface/abstract/Objectelement types are excluded whenCHECK_ABSTRACT_TYPESis off (the default), consistent with how the rule treats the field type itself.Testing
NonSerializableClass.xml:List<NotSerializable>andMap<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.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.mdentry under the java-errorprone section.