rspec/rules/S1149/java/rule.adoc
2023-06-09 16:36:32 +02:00

69 lines
1.6 KiB
Plaintext

== Why is this an issue?
Early classes of the Java API, such as `Vector`, `Hashtable` and `StringBuffer`, were synchronized to make them thread-safe.
However, synchronization has a significant negative impact on performance, even when using these collections from a single thread.
It is often best to use their non-synchronized counterparts:
* `ArrayList` or `LinkedList` instead of `Vector`
* `Deque` instead of `Stack`
* `HashMap` instead of `Hashtable`
* `StringBuilder` instead of `StringBuffer`
Even when used in synchronized contexts, you should think twice before using their synchronized counterparts, since their usage can be costly.
If you are confident the usage is legitimate, you can safely ignore this warning.
=== Noncompliant code example
[source,java,diff-id=1,diff-type=noncompliant]
----
Vector<Cat> cats = new Vector<>();
----
=== Compliant solution
[source,java,diff-id=1,diff-type=compliant]
----
ArrayList<Cat> cats = new ArrayList<>();
----
=== Exceptions
Usage of these synchronized classes is ignored in the signatures of overriding methods.
[source,java]
----
@Override
public Vector getCats() {...} // Compliant
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Replace the synchronized class "{}" by an unsynchronized one such as "{}".
'''
== Comments And Links
(visible only on this page)
=== relates to: S1076
=== on 31 Jul 2013, 15:13:51 Dinesh Bolkensteyn wrote:
Can also replace UseArrayListInsteadOfVector
=== on 31 Jul 2013, 15:18:08 Dinesh Bolkensteyn wrote:
Implemented by \http://jira.codehaus.org/browse/SONARJAVA-236
endif::env-github,rspecator-view[]