Merge pull request #32655 from bertoltmeier

* gh-32655:
  Add checkstyle rule for List.of() / Set.of() / Map.of()

Closes gh-32655
This commit is contained in:
Andy Wilkinson 2022-10-12 18:47:12 +01:00
commit 035d89df2a
10 changed files with 34 additions and 15 deletions

View File

@ -16,6 +16,7 @@
package org.springframework.boot.actuate.autoconfigure.tracing; package org.springframework.boot.actuate.autoconfigure.tracing;
import java.util.Collections;
import java.util.List; import java.util.List;
import brave.Tracer; import brave.Tracer;
@ -207,7 +208,7 @@ public class BraveAutoConfiguration {
Factory delegate = switch (this.tracingProperties.getPropagation().getType()) { Factory delegate = switch (this.tracingProperties.getPropagation().getType()) {
case B3 -> case B3 ->
B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.SINGLE_NO_PARENT).build(); B3Propagation.newFactoryBuilder().injectFormat(B3Propagation.Format.SINGLE_NO_PARENT).build();
case W3C -> new W3CPropagation(BRAVE_BAGGAGE_MANAGER, List.of()); case W3C -> new W3CPropagation(BRAVE_BAGGAGE_MANAGER, Collections.emptyList());
}; };
FactoryBuilder builder = BaggagePropagation.newFactoryBuilder(delegate); FactoryBuilder builder = BaggagePropagation.newFactoryBuilder(delegate);
baggagePropagationCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder)); baggagePropagationCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));

View File

@ -16,6 +16,7 @@
package org.springframework.boot.actuate.autoconfigure.tracing; package org.springframework.boot.actuate.autoconfigure.tracing;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -137,8 +138,9 @@ public class OpenTelemetryAutoConfiguration {
@ConditionalOnMissingBean @ConditionalOnMissingBean
OtelTracer micrometerOtelTracer(Tracer tracer, EventPublisher eventPublisher, OtelTracer micrometerOtelTracer(Tracer tracer, EventPublisher eventPublisher,
OtelCurrentTraceContext otelCurrentTraceContext) { OtelCurrentTraceContext otelCurrentTraceContext) {
return new OtelTracer(tracer, otelCurrentTraceContext, eventPublisher, new OtelBaggageManager( return new OtelTracer(tracer, otelCurrentTraceContext, eventPublisher,
otelCurrentTraceContext, this.tracingProperties.getBaggage().getRemoteFields(), List.of())); new OtelBaggageManager(otelCurrentTraceContext, this.tracingProperties.getBaggage().getRemoteFields(),
Collections.emptyList()));
} }
@Bean @Bean
@ -202,8 +204,9 @@ public class OpenTelemetryAutoConfiguration {
@ConditionalOnProperty(prefix = "management.tracing.propagation", name = "type", havingValue = "B3") @ConditionalOnProperty(prefix = "management.tracing.propagation", name = "type", havingValue = "B3")
TextMapPropagator b3BaggageTextMapPropagator(OtelCurrentTraceContext otelCurrentTraceContext) { TextMapPropagator b3BaggageTextMapPropagator(OtelCurrentTraceContext otelCurrentTraceContext) {
List<String> remoteFields = this.tracingProperties.getBaggage().getRemoteFields(); List<String> remoteFields = this.tracingProperties.getBaggage().getRemoteFields();
return TextMapPropagator.composite(B3Propagator.injectingSingleHeader(), new BaggageTextMapPropagator( return TextMapPropagator.composite(B3Propagator.injectingSingleHeader(),
remoteFields, new OtelBaggageManager(otelCurrentTraceContext, remoteFields, List.of()))); new BaggageTextMapPropagator(remoteFields,
new OtelBaggageManager(otelCurrentTraceContext, remoteFields, Collections.emptyList())));
} }
@Bean @Bean

View File

@ -16,7 +16,7 @@
package org.springframework.boot.actuate.autoconfigure.tracing.wavefront; package org.springframework.boot.actuate.autoconfigure.tracing.wavefront;
import java.util.Set; import java.util.Collections;
import brave.handler.SpanHandler; import brave.handler.SpanHandler;
import com.wavefront.sdk.common.WavefrontSender; import com.wavefront.sdk.common.WavefrontSender;
@ -84,7 +84,7 @@ public class WavefrontTracingAutoConfiguration {
WavefrontSpanHandler wavefrontSpanHandler(WavefrontProperties properties, WavefrontSender wavefrontSender, WavefrontSpanHandler wavefrontSpanHandler(WavefrontProperties properties, WavefrontSender wavefrontSender,
SpanMetrics spanMetrics, ApplicationTags applicationTags) { SpanMetrics spanMetrics, ApplicationTags applicationTags) {
return new WavefrontSpanHandler(properties.getSender().getMaxQueueSize(), wavefrontSender, spanMetrics, return new WavefrontSpanHandler(properties.getSender().getMaxQueueSize(), wavefrontSender, spanMetrics,
properties.getSourceOrDefault(), applicationTags, Set.of()); properties.getSourceOrDefault(), applicationTags, Collections.emptySet());
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)

View File

@ -19,6 +19,7 @@ package org.springframework.boot.actuate.autoconfigure.tracing.zipkin;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.UncheckedIOException; import java.io.UncheckedIOException;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
@ -68,7 +69,7 @@ abstract class HttpSender extends Sender {
@Override @Override
public CheckResult check() { public CheckResult check() {
try { try {
sendSpans(List.of()).execute(); sendSpans(Collections.emptyList()).execute();
return CheckResult.OK; return CheckResult.OK;
} }
catch (IOException | RuntimeException ex) { catch (IOException | RuntimeException ex) {

View File

@ -19,6 +19,7 @@ package org.springframework.boot.actuate.autoconfigure.tracing.zipkin;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.Duration; import java.time.Duration;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ -53,7 +54,7 @@ abstract class ZipkinHttpSenderTests {
@Test @Test
void sendSpansShouldThrowIfCloseWasCalled() throws IOException { void sendSpansShouldThrowIfCloseWasCalled() throws IOException {
this.sut.close(); this.sut.close();
assertThatThrownBy(() -> this.sut.sendSpans(List.of())).isInstanceOf(ClosedSenderException.class); assertThatThrownBy(() -> this.sut.sendSpans(Collections.emptyList())).isInstanceOf(ClosedSenderException.class);
} }
protected void makeRequest(List<byte[]> encodedSpans, boolean async) throws IOException { protected void makeRequest(List<byte[]> encodedSpans, boolean async) throws IOException {

View File

@ -18,6 +18,7 @@ package org.springframework.boot.actuate.autoconfigure.tracing.zipkin;
import java.io.IOException; import java.io.IOException;
import java.util.Base64; import java.util.Base64;
import java.util.Collections;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
@ -95,12 +96,13 @@ class ZipkinRestTemplateSenderTests extends ZipkinHttpSenderTests {
this.mockServer.expect(requestTo(ZIPKIN_URL)).andExpect(method(HttpMethod.POST)) this.mockServer.expect(requestTo(ZIPKIN_URL)).andExpect(method(HttpMethod.POST))
.andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR)); .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
if (async) { if (async) {
CallbackResult callbackResult = makeAsyncRequest(List.of()); CallbackResult callbackResult = makeAsyncRequest(Collections.emptyList());
assertThat(callbackResult.success()).isFalse(); assertThat(callbackResult.success()).isFalse();
assertThat(callbackResult.error()).isNotNull().hasMessageContaining("500 Internal Server Error"); assertThat(callbackResult.error()).isNotNull().hasMessageContaining("500 Internal Server Error");
} }
else { else {
assertThatThrownBy(() -> makeSyncRequest(List.of())).hasMessageContaining("500 Internal Server Error"); assertThatThrownBy(() -> makeSyncRequest(Collections.emptyList()))
.hasMessageContaining("500 Internal Server Error");
} }
} }

View File

@ -18,6 +18,7 @@ package org.springframework.boot.actuate.autoconfigure.tracing.zipkin;
import java.io.IOException; import java.io.IOException;
import java.util.Base64; import java.util.Base64;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -106,12 +107,13 @@ class ZipkinWebClientSenderTests extends ZipkinHttpSenderTests {
void sendSpansShouldHandleHttpFailures(boolean async) throws InterruptedException { void sendSpansShouldHandleHttpFailures(boolean async) throws InterruptedException {
mockBackEnd.enqueue(new MockResponse().setResponseCode(500)); mockBackEnd.enqueue(new MockResponse().setResponseCode(500));
if (async) { if (async) {
CallbackResult callbackResult = makeAsyncRequest(List.of()); CallbackResult callbackResult = makeAsyncRequest(Collections.emptyList());
assertThat(callbackResult.success()).isFalse(); assertThat(callbackResult.success()).isFalse();
assertThat(callbackResult.error()).isNotNull().hasMessageContaining("500 Internal Server Error"); assertThat(callbackResult.error()).isNotNull().hasMessageContaining("500 Internal Server Error");
} }
else { else {
assertThatThrownBy(() -> makeSyncRequest(List.of())).hasMessageContaining("500 Internal Server Error"); assertThatThrownBy(() -> makeSyncRequest(Collections.emptyList()))
.hasMessageContaining("500 Internal Server Error");
} }
requestAssertions((request) -> assertThat(request.getMethod()).isEqualTo("POST")); requestAssertions((request) -> assertThat(request.getMethod()).isEqualTo("POST"));

View File

@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.kafka; package org.springframework.boot.autoconfigure.kafka;
import java.util.Collections;
import java.util.Map; import java.util.Map;
import org.apache.kafka.common.config.SslConfigs; import org.apache.kafka.common.config.SslConfigs;
@ -55,7 +56,7 @@ class KafkaPropertiesTests {
@Test @Test
void adminDefaultValuesAreConsistent() { void adminDefaultValuesAreConsistent() {
KafkaAdmin admin = new KafkaAdmin(Map.of()); KafkaAdmin admin = new KafkaAdmin(Collections.emptyMap());
Admin adminProperties = new KafkaProperties().getAdmin(); Admin adminProperties = new KafkaProperties().getAdmin();
assertThat(admin).hasFieldOrPropertyWithValue("fatalIfBrokerNotAvailable", adminProperties.isFailFast()); assertThat(admin).hasFieldOrPropertyWithValue("fatalIfBrokerNotAvailable", adminProperties.isFailFast());
assertThat(admin).hasFieldOrPropertyWithValue("modifyTopicConfigs", adminProperties.isModifyTopicConfigs()); assertThat(admin).hasFieldOrPropertyWithValue("modifyTopicConfigs", adminProperties.isModifyTopicConfigs());

View File

@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.web;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
@ -52,7 +53,7 @@ class WebResourcesRuntimeHintsTests {
@Test @Test
void registerHintsWithNoLocation() { void registerHintsWithNoLocation() {
RuntimeHints hints = register(new TestClassLoader(List.of())); RuntimeHints hints = register(new TestClassLoader(Collections.emptyList()));
assertThat(hints.resources().resourcePatternHints()).isEmpty(); assertThat(hints.resources().resourcePatternHints()).isEmpty();
} }

View File

@ -54,6 +54,13 @@
value="Please use BDD-style (given, when, then) using BDDMockito imports." /> value="Please use BDD-style (given, when, then) using BDDMockito imports." />
<property name="ignoreComments" value="true" /> <property name="ignoreComments" value="true" />
</module> </module>
<module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">
<property name="maximum" value="0"/>
<property name="format" value="(List|Map|Set)\.of\(\)" />
<property name="message"
value="Please use Collections.emptyList()/emptyMap()/emptySet() for creating empty lists/maps/sets." />
<property name="ignoreComments" value="true" />
</module>
<module name="io.spring.javaformat.checkstyle.check.SpringJavadocCheck"> <module name="io.spring.javaformat.checkstyle.check.SpringJavadocCheck">
<property name="publicOnlySinceTags" value="true" /> <property name="publicOnlySinceTags" value="true" />
<property name="requireSinceTag" value="true" /> <property name="requireSinceTag" value="true" />