From 6cdc2794dec7740050c71a9eeb298594baa6ba17 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Wed, 22 Jan 2025 13:11:56 +0800 Subject: [PATCH] Polish usage of Collection in @ConfigurationProperties classes 1. It's safe to use immutable Collection object as default value of field. 2. It's unnecessary to create new Collection object in setter method. Signed-off-by: Yanming Zhou --- .../actuate/autoconfigure/metrics/MetricsProperties.java | 6 ++---- .../web/exchanges/HttpExchangesProperties.java | 5 ++--- .../boot/autoconfigure/context/MessageSourceProperties.java | 5 ++--- .../elasticsearch/ElasticsearchProperties.java | 6 ++---- .../boot/autoconfigure/flyway/FlywayProperties.java | 5 ++--- .../boot/autoconfigure/kafka/KafkaProperties.java | 6 ++---- .../boot/autoconfigure/quartz/QuartzProperties.java | 6 ++---- .../boot/autoconfigure/security/SecurityProperties.java | 4 ++-- .../automaticmetadatageneration/MyMessagingProperties.java | 6 ++---- .../javabeanbinding/MyProperties.java | 6 ++---- .../org/springframework/boot/ApplicationProperties.java | 4 ++-- 11 files changed, 22 insertions(+), 37 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsProperties.java index 8a85337446f0..0674e05582e7 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,8 +18,6 @@ import java.io.File; import java.time.Duration; -import java.util.ArrayList; -import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -202,7 +200,7 @@ public static class Diskspace { /** * List of paths to report disk metrics for. */ - private List paths = new ArrayList<>(Collections.singletonList(new File("."))); + private List paths = List.of(new File(".")); public List getPaths() { return this.paths; diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/exchanges/HttpExchangesProperties.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/exchanges/HttpExchangesProperties.java index b4eb1c1df3fc..27603411efa8 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/exchanges/HttpExchangesProperties.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/exchanges/HttpExchangesProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,6 @@ package org.springframework.boot.actuate.autoconfigure.web.exchanges; -import java.util.HashSet; import java.util.Set; import org.springframework.boot.actuate.web.exchanges.Include; @@ -53,7 +52,7 @@ public static class Recording { * (excluding Authorization and Cookie), response headers (excluding Set-Cookie), * and time taken. */ - private Set include = new HashSet<>(Include.defaultIncludes()); + private Set include = Include.defaultIncludes(); public Set getInclude() { return this.include; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceProperties.java index 0495e285fd4f..98a872ac7e2f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/MessageSourceProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,6 @@ import java.nio.charset.StandardCharsets; import java.time.Duration; import java.time.temporal.ChronoUnit; -import java.util.ArrayList; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -44,7 +43,7 @@ public class MessageSourceProperties { * locations. If it doesn't contain a package qualifier (such as "org.mypackage"), it * will be resolved from the classpath root. */ - private List basename = new ArrayList<>(List.of("messages")); + private List basename = List.of("messages"); /** * List of locale-independent property file resources containing common messages. diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java index a0325023aeb3..5cfd8b108ba8 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,8 +17,6 @@ package org.springframework.boot.autoconfigure.elasticsearch; import java.time.Duration; -import java.util.ArrayList; -import java.util.Collections; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -35,7 +33,7 @@ public class ElasticsearchProperties { /** * List of the Elasticsearch instances to use. */ - private List uris = new ArrayList<>(Collections.singletonList("http://localhost:9200")); + private List uris = List.of("http://localhost:9200"); /** * Username for authentication with Elasticsearch. diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java index 60c812d9fa40..3210bae1b88f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayProperties.java @@ -22,7 +22,6 @@ import java.time.Duration; import java.time.temporal.ChronoUnit; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -57,7 +56,7 @@ public class FlywayProperties { * Locations of migrations scripts. Can contain the special "{vendor}" placeholder to * use vendor-specific locations. */ - private List locations = new ArrayList<>(Collections.singletonList("classpath:db/migration")); + private List locations = List.of("classpath:db/migration"); /** * Encoding of SQL migrations. @@ -157,7 +156,7 @@ public class FlywayProperties { /** * File name suffix for SQL migrations. */ - private List sqlMigrationSuffixes = new ArrayList<>(Collections.singleton(".sql")); + private List sqlMigrationSuffixes = List.of(".sql"); /** * File name separator for SQL migrations. diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/KafkaProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/KafkaProperties.java index 90133fab101f..7e9c43bfd660 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/KafkaProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/KafkaProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,8 +19,6 @@ import java.io.IOException; import java.time.Duration; import java.time.temporal.ChronoUnit; -import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -69,7 +67,7 @@ public class KafkaProperties { * List of host:port pairs to use for establishing the initial connections to the * Kafka cluster. Applies to all components unless overridden. */ - private List bootstrapServers = new ArrayList<>(Collections.singletonList("localhost:9092")); + private List bootstrapServers = List.of("localhost:9092"); /** * ID to pass to the server when making requests. Used for server-side logging. diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java index cc2ebaf5e56b..6a6c0c636286 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/quartz/QuartzProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,8 +17,6 @@ package org.springframework.boot.autoconfigure.quartz; import java.time.Duration; -import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -155,7 +153,7 @@ public static class Jdbc { /** * Prefixes for single-line comments in SQL initialization scripts. */ - private List commentPrefix = new ArrayList<>(Arrays.asList("#", "--")); + private List commentPrefix = List.of("#", "--"); public String getSchema() { return this.schema; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SecurityProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SecurityProperties.java index 504044c9eb98..4504e3383322 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SecurityProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/SecurityProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -146,7 +146,7 @@ public List getRoles() { } public void setRoles(List roles) { - this.roles = new ArrayList<>(roles); + this.roles = roles; } public boolean isPasswordGenerated() { diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/appendix/configurationmetadata/annotationprocessor/automaticmetadatageneration/MyMessagingProperties.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/appendix/configurationmetadata/annotationprocessor/automaticmetadatageneration/MyMessagingProperties.java index e03d8b131d1d..1699b7871cf6 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/appendix/configurationmetadata/annotationprocessor/automaticmetadatageneration/MyMessagingProperties.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/appendix/configurationmetadata/annotationprocessor/automaticmetadatageneration/MyMessagingProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,6 @@ package org.springframework.boot.docs.appendix.configurationmetadata.annotationprocessor.automaticmetadatageneration; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -25,7 +23,7 @@ @ConfigurationProperties(prefix = "my.messaging") public class MyMessagingProperties { - private List addresses = new ArrayList<>(Arrays.asList("a", "b")); + private List addresses = List.of("a", "b"); private ContainerType containerType = ContainerType.SIMPLE; diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/externalconfig/typesafeconfigurationproperties/javabeanbinding/MyProperties.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/externalconfig/typesafeconfigurationproperties/javabeanbinding/MyProperties.java index 7d91ff68e9e7..3e07962e5324 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/externalconfig/typesafeconfigurationproperties/javabeanbinding/MyProperties.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/externalconfig/typesafeconfigurationproperties/javabeanbinding/MyProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,8 +17,6 @@ package org.springframework.boot.docs.features.externalconfig.typesafeconfigurationproperties.javabeanbinding; import java.net.InetAddress; -import java.util.ArrayList; -import java.util.Collections; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -60,7 +58,7 @@ public static class Security { private String password; - private List roles = new ArrayList<>(Collections.singleton("USER")); + private List roles = List.of("USER"); // @fold:on // getters / setters... public String getUsername() { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationProperties.java index 34848c776966..842d9b4fab8d 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ApplicationProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -146,7 +146,7 @@ Set getSources() { } void setSources(Set sources) { - this.sources = new LinkedHashSet<>(sources); + this.sources = sources; } WebApplicationType getWebApplicationType() {