Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] LooseCoupling rule: Extend to cover user defined implementations and interfaces #2822

Closed
linusjf opened this issue Oct 9, 2020 · 5 comments · Fixed by #3177
Closed
Labels
an:enhancement An improvement on existing features / rules
Projects
Milestone

Comments

@linusjf
Copy link

linusjf commented Oct 9, 2020

Is your feature request related to a problem? Please describe.
The rule LooseCoupling flags implemented classes when interfaces suffice. It prescribes coding against an interface as against implementor classes.
It currently checks for Collections API classes. Please extend the rule so that developers can specify user-defined classes as well.

Describe the solution you'd like
The rule must be made configurable so that users can define interfaces and implementor classes from their application's source base.

Describe alternatives you've considered
None that I know of.

Additional context

None.

Other issues for the same rule:

@linusjf linusjf added the an:enhancement An improvement on existing features / rules label Oct 9, 2020
@adangel adangel changed the title [java] LooseCoupling rule: Extend to cover user defined implementations and interfaces. [java] LooseCoupling rule: Extend to cover user defined implementations and interfaces Oct 10, 2020
@linusjf
Copy link
Author

linusjf commented Oct 25, 2020

A property is needed to optionally flag the use of var when instantiating a concrete type.

@linusjf
Copy link
Author

linusjf commented Oct 25, 2020

#2847 (comment)

Add a property to extend the rule to local variables.

@adangel adangel added this to the 7.0.0 milestone Apr 1, 2021
@adangel adangel linked a pull request Apr 2, 2021 that will close this issue
4 tasks
@oowekyala oowekyala added this to Done in PMD 7 Apr 3, 2021
@adangel
Copy link
Member

adangel commented Feb 1, 2022

With PMD7 there is a new property now "allowedTypes" for the rule LooseCoupling.
This property by default allows java.util.Properties.

While working on #3657 I found, that we should also allow java.util.Stack - while stack is a collection, you can't use the List interface, if you use the stack-methods (push, pop). That's very similar like Properties, which is a Map, but a Map doesn't have "getProperty".

@oowekyala Wdyt?

@oowekyala
Copy link
Member

Stack is a tricky case. Its own documentation recommends using the Dequeue interface and ArrayDequeue as an implementation. Maybe we could disallow it to force migration. Stack extends Vector so there are good reasons to get rid of it.

A solution I like more, however, would be to make LooseCoupling allow Stack, but to create another rule that recommends migrating to Dequeue. That new rule could subsume UseArrayListInsteadOfVector, ReplaceVectorWithList and other rules like ReplaceHashtableWithMap and ReplaceEnumerationWithIterator.

@adangel adangel mentioned this issue Jan 23, 2023
55 tasks
@adangel
Copy link
Member

adangel commented Apr 22, 2023

This has been fixed with PMD 7.0.0-rc1.

@adangel adangel closed this as completed Apr 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
an:enhancement An improvement on existing features / rules
Projects
No open projects
PMD 7
  
Done
Development

Successfully merging a pull request may close this issue.

3 participants