Skip to content

[java] New Rule: ReplaceJavaUtilVector to consolidate ReplaceVectorWithList / UseArrayListInsteadOfVector #6546

Closed

Description

@zbynek

Affects PMD Version:

7.22.0

Rules:

Description:

ReplaceVectorWithList (since 3.4) and UseArrayListInsteadOfVector (since 3.0) describe the same issue, though UseArrayListInsteadOfVector is more restricted (but ignores missing imports since 0ee8dd93).

  • UseArrayListInsteadOfVector checks
    //ConstructorCall/ClassType[pmd-java:typeIsExactly('java.util.Vector') or pmd-java:typeIsExactly('Vector')]
  • ReplaceVectorWithList checks //ClassType[pmd-java:typeIsExactly('java.util.Vector')]

Exception Stacktrace:

N/A

Code Sample demonstrating the issue:

Object o = new java.util.Vector(); // reported by both once
java.util.Vector v = new java.util.Vector();  // ReplaceVectorWithList reports this twice, UseArrayListInsteadOfVector once
Object o2 = new Vector(); // ReplaceVectorWithList ignores this without import 

Steps to reproduce:

Analyze a codebase with both rules enabled => duplicate reports.

Running PMD through: Gradle

Activity

  1. added
    a:bug
    on Mar 24, 2026
  2. UncleOwen commented on Apr 23, 2026

    @UncleOwen
    Member

    So, which one do we keep? My vote is for ReplaceVectorWithList, because it fits nicely with a lot of other ReplaceFooWithBar rules we already have.

    If the import is missing the code won't compile, so that's not an issue.

    Maybe restrict ReplaceVectorWithList to just check for variable declarations?

  3. UncleOwen commented on Sep 8, 2026

    @UncleOwen
    Member

    Yes, definitely go with #2.

    #1 would be two rules that look for the same problem but in different ways which would be... not good.

  4. zbynek commented on Sep 11, 2026

    @zbynek
    ContributorAuthor

    It would be definitely better to avoid two errors for one statement.

    But both of these should still be flagged

    Object o = new Vector();
    

    and

    class BetterVector extends Vector {}
    

    so I think both the current "just constructors" and the above suggested "just declarations" approaches are not strict enough.

    Also if we're changing this, maybe the rule should use typeIs instead of typeIsExactly and the description should be updated to suggest replacing Stack with Deque -- Stack is the only significant subclass of Vector and the same reasons for not using Vector apply to Stack.

  5. adangel commented on Sep 11, 2026

    @adangel
    Member

    We currently explicitly do not find Stack instances, see

    <test-code>
    <description>Only consider Vector and not subclasses</description>
    <expected-problems>0</expected-problems>
    <code><![CDATA[
    import java.util.Stack;
    class StackUser {
    Stack s = new Stack();
    }
    ]]></code>
    </test-code>

    (Introduced in #4156). Don't remember exactly, why I moved to typeIsExactly. Probably because we then found Stack instances as well - and you usually create a Stack, if you want to call push/pop, which ArrayList doesn't support. So, if we find Stack as well, then the rule should definitely have a different name...
    Alternatively we could have two rules? ReplaceVectorWithList and ReplaceStackWithDeque?

    Also note, that this example

    Object o2 = new Vector(); // ReplaceVectorWithList ignores this without import

    won't compile, if there is no import... So, it's probably not worth optimizing the rule to catch this.

  6. UncleOwen commented on Sep 11, 2026

    @UncleOwen
    Member

    We could remove ArrayList from the name and call it ReplaceJavaUtilVector. We could then mention ArrayList and ArrayDeque in the rule's description, just like we do for ReplaceJavaUtilCalendar

  7. zbynek commented on Sep 11, 2026

    @zbynek
    ContributorAuthor

    Don't remember exactly, why I moved to typeIsExactly.

    The other rule changed to typeIsExactly here: #4984 -- from all I can tell it's not related to Stack, maybe the reason for changing the UseArrayListInsteadOfVector rule was similar?

    won't compile, if there is no import...

    I agree it's better to avoid the not qualified Vector in the rule, but I'm not sure why was it explicitly added in 0ee8dd93#diff-acbbdf76ecede9109116efd33dde9b61c39cf79a5937d1d27ffcf1e65a2a7a59R97-R658

  8. UncleOwen commented on Sep 11, 2026

    @UncleOwen
    Member

    I agree it's better to avoid the not qualified Vector in the rule, but I'm not sure why was it explicitly added in 0ee8dd93#diff-acbbdf76ecede9109116efd33dde9b61c39cf79a5937d1d27ffcf1e65a2a7a59R97-R658

    My guess would be as a workaround for a missing auxClassPath.

  9. added this to the 7.29.0 milestone on Oct 29, 2026
  10. added a commit that references this issue on Oct 29, 2026
    95a777a
  11. changed the title [-][java] Duplicate rule: ReplaceVectorWithList / UseArrayListInsteadOfVector[/-] [+][java] New rule: ReplaceJavaUtilVector to consolidate ReplaceVectorWithList / UseArrayListInsteadOfVector[/+] on Oct 29, 2026
  12. added
    a:new-rule
    on Oct 29, 2026
  13. changed the title [-][java] New rule: ReplaceJavaUtilVector to consolidate ReplaceVectorWithList / UseArrayListInsteadOfVector[/-] [+][java] New Rule: ReplaceJavaUtilVector to consolidate ReplaceVectorWithList / UseArrayListInsteadOfVector[/+] on Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:buga:new-rule

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions