GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [kotlin] Fix #7092: Add LooseCoupling rule by stokpop · Pull Request #7094 · pmd/pmd

Conversation

@stokpop

@stokpop stokpop commented Sep 22, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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

  • [kotlin] Add XPath functions and type attributes #6893 — the XPath functions/attributes PR this demonstrates
  • [kotlin] Follow-ups from XPath functions/type attributes PR (#6893) #7091 — follow-up/discussion issue (design gaps, lessons learned)
  • [kotlin] New Rule: LooseCoupling #7092 — new-rule proposal issue for this rule

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
stokpop added 3 commits September 22, 2026 20:11
@stokpop
[kotlin] Add LooseCoupling rule
dec2496
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
@stokpop
[kotlin] LooseCoupling: refine message/description per review
d02ca11
- 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
[kotlin] Document {0} message limitation, add release notes entry
a0b1869
@stokpop
stokpop force-pushed the kotlin/loose-coupling-rule branch from 101aea9 to a0b1869 Compare September 22, 2026 20:13
@pmd-actions-helper

pmd-actions-helper Bot commented Sep 22, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@stokpop
stokpop marked this pull request as ready for review September 23, 2026 05:40
@adangel adangel changed the title [kotlin] Add LooseCoupling rule [kotlin] Fix #7092: Add LooseCoupling rule Oct 23, 2026
@adangel adangel added the a:new-rule Proposal to add a new built-in rule label Oct 23, 2026
adangel
adangel approved these changes Oct 23, 2026
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

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.

All reactions
Comment thread docs/pages/pmd/languages/kotlin.md

### Message parameter (`{0}`) substitution

`net.sourceforge.pmd.lang.rule.xpath.XPathRule` auto-binds `{0}` in a rule's `message` from the

@adangel adangel Oct 23, 2026

Copy link
Copy Markdown
Member

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.

All reactions
Comment thread docs/pages/release_notes.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pmd-kotlin/src/main/resources/category/kotlin/bestpractices.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

@adangel adangel added this to the 7.29.0 milestone Oct 23, 2026
adangel added 2 commits October 23, 2026 14:30
@adangel
Merge branch 'main' into pr-7094
1ab702f
@adangel
Update to 7.29.0
09ff99a
@adangel
adangel merged commit 8d4094b into pmd:main Oct 23, 2026
13 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@stokpop

stokpop commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@adangel adangel adangel approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

a:new-rule Proposal to add a new built-in rule

Projects

None yet

Milestone

7.29.0

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.

2 participants

@stokpop @adangel
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.