rspec/rules/S1149/java/rule.adoc

57 lines
1.3 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Early classes of the Java API, such as ``++Vector++``, ``++Hashtable++`` and ``++StringBuffer++``, were synchronized to make them thread-safe. Unfortunately, synchronization has a big negative impact on performance, even when using these collections from a single thread.
It is better to use their new unsynchronized replacements:
* ``++ArrayList++`` or ``++LinkedList++`` instead of ``++Vector++``
* ``++Deque++`` instead of ``++Stack++``
* ``++HashMap++`` instead of ``++Hashtable++``
* ``++StringBuilder++`` instead of ``++StringBuffer++``
Even when used in synchronized context, you should think twice before using it, since it's usage can be tricky. If you are confident the usage is legitimate, you can safely ignore this warning.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
Vector cats = new Vector();
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
ArrayList cats = new ArrayList();
----
=== Exceptions
2021-04-28 16:49:39 +02:00
Use of those synchronized classes is ignored in the signatures of overriding methods.
----
@Override
public Vector getCats() {...}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]