Repository navigation
[kotlin] Fix #7092: Add LooseCoupling rule - #7094
Merged
Merged
Conversation
Ports pmd-java's LooseCoupling rule to Kotlin as an XPath rule, flagging concrete collection/map implementation types (ArrayList, HashMap, LinkedHashMap, HashSet, LinkedHashSet, TreeMap, TreeSet, Vector, Stack, Hashtable, LinkedList) used for properties, class/function parameters, and function return types, in favor of the corresponding interface. Demonstrates the new pmd-kotlin:typeIs() / typeIsExactly() / modifiers() XPath functions and @typename / @ReturnTypeName attributes added in pmd#6893. - Uses typeIs() (subtype-aware) so user-defined subclasses of a concrete collection type are also caught. - Skips local variables (not part of the API surface), including those declared inside init {} blocks and secondary constructors, which use a plain Block rather than a FunctionBody. - Skips override-annotated members, matching the Java rule's behavior. - Skips secondary-constructor parameters, since they aren't currently visited by the type-annotation visitor and would otherwise fall back to a line-tolerant type lookup that can misattribute an adjacent declaration's type. - Supports an allowedTypes property (default java.util.Properties), mirroring the Java rule's property of the same name. Fixes pmd#7092
- Message no longer suggests Mutable* as an equal alternative to the read-only interface; that nuance now lives only in the description, worded to prefer the read-only interface by default and fall back to Mutable* only when mutation is actually needed. - Description now documents that this rule checks a fixed list of common JDK concrete types, unlike pmd-java's LooseCouplingRule, which generically flags any concrete Collection/Map implementation. Doing the same generically in Kotlin isn't currently possible: ktm only exposes interface-vs-class info for declarations found in the analyzed Kotlin source, not for external/JDK types resolved via classpath. Tracked as a follow-up in pmd#7091. Deliberately not growing the fixed type list piecemeal with popular library collection types (Guava Multimap/BiMap/Immutable*, Apache Commons Collections4, kotlinx.collections.immutable Persistent*, etc.): several of those are often intentionally exposed as concrete types to guarantee immutability, so hand-picking more now would create debatable false positives. Prefer waiting for the generic isInterface-based check (pmd#7091) to decide "concrete vs. interface" for any library, rather than expanding this list ad hoc. (Note for PR description once opened.)
stokpop
force-pushed
the
kotlin/loose-coupling-rule
branch
from
September 22, 2026 20:13
101aea9 to
a0b1869
Compare
Contributor
|
Compared to main: (comment created at 2026-10-23 12:55:49+00:00 for 09ff99a) |
stokpop
marked this pull request as ready for review
September 23, 2026 05:40
adangel
approved these changes
Oct 23, 2026
adangel
left a comment
Member
There was a problem hiding this comment.
Thanks, looks good!
I'll update it with the current main and merge it for 7.29.0
|
|
||
| ### Message parameter (`{0}`) substitution | ||
|
|
||
| `net.sourceforge.pmd.lang.rule.xpath.XPathRule` auto-binds `{0}` in a rule's `message` from the |
Contributor
Author
@adangel Thanks for taking the time for reviewing the kotlin related PRs! Much appreciated |
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.
Fixes #7092
Adds a
LooseCouplingrule to pmd-kotlin, ported from pmd-java'sLooseCouplingRule. Implemented as a plain XPath rule using thepmd-kotlin:typeIs()/typeIsExactly()functions and@TypeName/@ReturnTypeName/modifiers()attributes added in #6893.What it flags
Concrete collection/map implementation types (
ArrayList,LinkedList,Vector,Stack,HashMap,Hashtable,TreeMap,HashSet,LinkedHashSet,TreeSet) used for properties, class/function parameters, and function return types, in favor of the corresponding interface.typeIs()(subtype-aware), so user-defined subclasses are caught too.override-annotated members, and secondary-constructor parameters (not part of the API surface / not currently type-annotated).allowedTypesproperty (defaultjava.util.Properties), mirroring the Java rule.18 test cases; also validated against a real-world ~525-file Kotlin codebase, finding genuine violations at correct line numbers.
Known limitations (tracked in #7091, not blocking)
Collection/Mapimplementation like pmd-java's rule does (would need a newisInterface()-style function).{0}type-name substitution) — XPathRule's{0}auto-binding doesn't apply cleanly to Kotlin nodes; documented indocs/pages/pmd/languages/kotlin.md.Related