38 lines
879 B
Plaintext
38 lines
879 B
Plaintext
Java 8 introduced ``++ThreadLocal.withInitial++`` which is a simpler alternative to creating an anonymous inner class to initialise a ``++ThreadLocal++`` instance.
|
|
|
|
|
|
This rule raises an issue when a ``++ThreadLocal++`` anonymous inner class can be replaced by a call to ``++ThreadLocal.withInitial++``.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
ThreadLocal<List<String>> myThreadLocal =
|
|
new ThreadLocal<List<String>>() { // Noncompliant
|
|
@Override
|
|
protected List<String> initialValue() {
|
|
return new ArrayList<String>();
|
|
}
|
|
};
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
ThreadLocal<List<String>> myThreadLocal = ThreadLocal.withInitial(ArrayList::new);
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
include::highlighting.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|