GeezerVison is showing github.com — [kotlin] Fix #7092: Add LooseCoupling rule by stokpop · Pull Request #7094 · pmd/pmd
Conversation
stokpop
commented
Sep 22, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Fixes #7092
Adds a LooseCoupling rule to pmd-kotlin, ported from pmd-java's LooseCouplingRule. Implemented as a plain XPath rule using the pmd-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.
- Uses
typeIs() (subtype-aware), so user-defined subclasses are caught too.
- Skips local variables,
override-annotated members, and secondary-constructor parameters (not part of the API surface / not currently type-annotated).
- Has an
allowedTypes property (default java.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)
- Checks a fixed list of JDK types rather than generically flagging any concrete
Collection/Map implementation like pmd-java's rule does (would need a new isInterface()-style function).
- Message is static (no
{0} type-name substitution) — XPathRule's {0} auto-binding doesn't apply cleanly to Kotlin nodes; documented in docs/pages/pmd/languages/kotlin.md.
Related
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.)
|
Documentation Preview
Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 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-10-23 12:55:49+00:00 for 09ff99a)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
adangel
changed the title
[kotlin] Add LooseCoupling rule
[kotlin] Fix #7092: Add LooseCoupling rule
Oct 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looks good!
I'll update it with the current main and merge it for 7.29.0
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
|
|
|
|
### Message parameter (`{0}`) substitution
|
|
|
|
|
|
`net.sourceforge.pmd.lang.rule.xpath.XPathRule` auto-binds `{0}` in a rule's `message` from the
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI - this is a known limitation, see also #4787
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Thanks, looks good!
I'll update it with the current main and merge it for 7.29.0
@adangel Thanks for taking the time for reviewing the kotlin related PRs! Much appreciated
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
adangel
adangel approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Development
Successfully merging this pull request may close these issues.
[kotlin] New Rule: LooseCoupling
Uh oh!
There was an error while loading. Please reload this page.
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.
|