rspec/rules/S4065/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

48 lines
1016 B
Plaintext

== Why is this an issue?
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
[source,java]
----
ThreadLocal<List<String>> myThreadLocal =
new ThreadLocal<List<String>>() { // Noncompliant
@Override
protected List<String> initialValue() {
return new ArrayList<String>();
}
};
----
=== Compliant solution
[source,java]
----
ThreadLocal<List<String>> myThreadLocal = ThreadLocal.withInitial(ArrayList::new);
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Replace this anonymous class with a call to "ThreadLocal.withInitial".
=== Highlighting
ThreadLocal instance creation
endif::env-github,rspecator-view[]