You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] New Rule: ReplaceJavaUtilVector to consolidate ReplaceVectorWithList / UseArrayListInsteadOfVector #6546
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')]
Objecto = newjava.util.Vector(); // reported by both oncejava.util.Vectorv = newjava.util.Vector(); // ReplaceVectorWithList reports this twice, UseArrayListInsteadOfVector onceObjecto2 = newVector(); // ReplaceVectorWithList ignores this without import
Steps to reproduce:
Analyze a codebase with both rules enabled => duplicate reports.
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.
<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.
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
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?
changed the title [-][java] Duplicate rule: ReplaceVectorWithList / UseArrayListInsteadOfVector[/-][+][java] New rule: ReplaceJavaUtilVector to consolidate ReplaceVectorWithList / UseArrayListInsteadOfVector[/+]on Oct 29, 2026
changed the title [-][java] New rule: ReplaceJavaUtilVector to consolidate ReplaceVectorWithList / UseArrayListInsteadOfVector[/-][+][java] New Rule: ReplaceJavaUtilVector to consolidate ReplaceVectorWithList / UseArrayListInsteadOfVector[/+]on Oct 29, 2026
Activity
UncleOwen commentedon Apr 23, 2026
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?
UncleOwen commentedon Sep 8, 2026
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.
zbynek commentedon Sep 11, 2026
It would be definitely better to avoid two errors for one statement.
But both of these should still be flagged
and
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
typeIsinstead oftypeIsExactlyand the description should be updated to suggest replacingStackwithDeque--Stackis the only significant subclass ofVectorand the same reasons for not usingVectorapply toStack.adangel commentedon Sep 11, 2026
We currently explicitly do not find Stack instances, see
pmd/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/performance/xml/UseArrayListInsteadOfVector.xml
Lines 120 to 129 in 97c5369
(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
won't compile, if there is no import... So, it's probably not worth optimizing the rule to catch this.
UncleOwen commentedon Sep 11, 2026
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
zbynek commentedon Sep 11, 2026
The other rule changed to
typeIsExactlyhere: #4984 -- from all I can tell it's not related toStack, maybe the reason for changing theUseArrayListInsteadOfVectorrule was similar?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
UncleOwen commentedon Sep 11, 2026
My guess would be as a workaround for a missing auxClassPath.
[java] Fix pmd#6546: Consolidate the Vector replacement rules (pmd#7199)
[-][java] Duplicate rule: ReplaceVectorWithList / UseArrayListInsteadOfVector[/-][+][java] New rule: ReplaceJavaUtilVector to consolidate ReplaceVectorWithList / UseArrayListInsteadOfVector[/+][-][java] New rule: ReplaceJavaUtilVector to consolidate ReplaceVectorWithList / UseArrayListInsteadOfVector[/-][+][java] New Rule: ReplaceJavaUtilVector to consolidate ReplaceVectorWithList / UseArrayListInsteadOfVector[/+]