Skip to content

Commit aac1845

Browse files
committed
Improve null-safety of core/spring-boot
See gh-46926
1 parent 7689389 commit aac1845

23 files changed

+57
-43
lines changed

core/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesScanRegistrar.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.LinkedHashSet;
2121
import java.util.Set;
2222

23+
import org.jspecify.annotations.Nullable;
24+
2325
import org.springframework.beans.factory.BeanFactory;
2426
import org.springframework.beans.factory.config.BeanDefinition;
2527
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
@@ -50,9 +52,9 @@ class ConfigurationPropertiesScanRegistrar implements ImportBeanDefinitionRegist
5052

5153
private final Environment environment;
5254

53-
private final ResourceLoader resourceLoader;
55+
private final @Nullable ResourceLoader resourceLoader;
5456

55-
ConfigurationPropertiesScanRegistrar(Environment environment, ResourceLoader resourceLoader) {
57+
ConfigurationPropertiesScanRegistrar(Environment environment, @Nullable ResourceLoader resourceLoader) {
5658
this.environment = environment;
5759
this.resourceLoader = resourceLoader;
5860
}

core/spring-boot/src/main/java/org/springframework/boot/context/properties/source/CachingConfigurationPropertySource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ interface CachingConfigurationPropertySource {
3838
* @return a {@link ConfigurationPropertyCaching} instance or {@code null} if the
3939
* source does not support caching.
4040
*/
41-
static @Nullable ConfigurationPropertyCaching find(ConfigurationPropertySource source) {
41+
static @Nullable ConfigurationPropertyCaching find(@Nullable ConfigurationPropertySource source) {
4242
if (source instanceof CachingConfigurationPropertySource cachingSource) {
4343
return cachingSource.getCaching();
4444
}

core/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationProperty.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.boot.origin.OriginProvider;
2323
import org.springframework.boot.origin.OriginTrackedValue;
2424
import org.springframework.core.style.ToStringCreator;
25+
import org.springframework.lang.Contract;
2526
import org.springframework.util.Assert;
2627
import org.springframework.util.ObjectUtils;
2728

@@ -124,13 +125,15 @@ public int compareTo(ConfigurationProperty other) {
124125
return this.name.compareTo(other.name);
125126
}
126127

128+
@Contract("_, !null -> !null")
127129
static @Nullable ConfigurationProperty of(ConfigurationPropertyName name, @Nullable OriginTrackedValue value) {
128130
if (value == null) {
129131
return null;
130132
}
131133
return new ConfigurationProperty(name, value.getValue(), value.getOrigin());
132134
}
133135

136+
@Contract("_, _, !null, _ -> !null")
134137
static @Nullable ConfigurationProperty of(@Nullable ConfigurationPropertySource source,
135138
ConfigurationPropertyName name, @Nullable Object value, @Nullable Origin origin) {
136139
if (value == null) {

core/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public int getNumberOfElements() {
210210
* @return a new {@link ConfigurationPropertyName}
211211
* @throws InvalidConfigurationPropertyNameException if the result is not valid
212212
*/
213-
public ConfigurationPropertyName append(String suffix) {
213+
public ConfigurationPropertyName append(@Nullable String suffix) {
214214
if (!StringUtils.hasLength(suffix)) {
215215
return this;
216216
}
@@ -625,7 +625,7 @@ boolean hasDashedElement() {
625625
* @param name the name to test
626626
* @return {@code true} if the name is valid
627627
*/
628-
public static boolean isValid(CharSequence name) {
628+
public static boolean isValid(@Nullable CharSequence name) {
629629
return of(name, true) != null;
630630
}
631631

@@ -636,7 +636,7 @@ public static boolean isValid(CharSequence name) {
636636
* @throws InvalidConfigurationPropertyNameException if the name is not valid
637637
*/
638638
@SuppressWarnings("NullAway") // See https://github.com/uber/NullAway/issues/1232
639-
public static ConfigurationPropertyName of(CharSequence name) {
639+
public static ConfigurationPropertyName of(@Nullable CharSequence name) {
640640
return of(name, false);
641641
}
642642

@@ -647,7 +647,7 @@ public static ConfigurationPropertyName of(CharSequence name) {
647647
* @return a {@link ConfigurationPropertyName} instance
648648
* @since 2.3.1
649649
*/
650-
public static @Nullable ConfigurationPropertyName ofIfValid(CharSequence name) {
650+
public static @Nullable ConfigurationPropertyName ofIfValid(@Nullable CharSequence name) {
651651
return of(name, true);
652652
}
653653

@@ -660,7 +660,7 @@ public static ConfigurationPropertyName of(CharSequence name) {
660660
* {@code returnNullIfInvalid} is {@code false}
661661
*/
662662
@Contract("_, false -> !null")
663-
static @Nullable ConfigurationPropertyName of(CharSequence name, boolean returnNullIfInvalid) {
663+
static @Nullable ConfigurationPropertyName of(@Nullable CharSequence name, boolean returnNullIfInvalid) {
664664
Elements elements = elementsOf(name, returnNullIfInvalid, ElementsParser.DEFAULT_CAPACITY);
665665
return (elements != null) ? new ConfigurationPropertyName(elements) : null;
666666
}

core/spring-boot/src/main/java/org/springframework/boot/diagnostics/AbstractFailureAnalyzer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected Class<? extends T> getCauseType() {
6161
}
6262

6363
@SuppressWarnings("unchecked")
64-
protected final <E extends Throwable> @Nullable E findCause(Throwable failure, Class<E> type) {
64+
protected final <E extends Throwable> @Nullable E findCause(@Nullable Throwable failure, Class<E> type) {
6565
while (failure != null) {
6666
if (type.isInstance(failure)) {
6767
return (E) failure;

core/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/MutuallyExclusiveConfigurationPropertiesFailureAnalyzer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@
4848
class MutuallyExclusiveConfigurationPropertiesFailureAnalyzer
4949
extends AbstractFailureAnalyzer<MutuallyExclusiveConfigurationPropertiesException> {
5050

51-
private final ConfigurableEnvironment environment;
51+
private final @Nullable ConfigurableEnvironment environment;
5252

53-
MutuallyExclusiveConfigurationPropertiesFailureAnalyzer(Environment environment) {
53+
MutuallyExclusiveConfigurationPropertiesFailureAnalyzer(@Nullable Environment environment) {
5454
this.environment = (ConfigurableEnvironment) environment;
5555
}
5656

core/spring-boot/src/main/java/org/springframework/boot/json/AbstractJsonParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
*/
3535
public abstract class AbstractJsonParser implements JsonParser {
3636

37-
protected final Map<String, Object> parseMap(String json, Function<String, Map<String, Object>> parser) {
37+
protected final Map<String, Object> parseMap(@Nullable String json, Function<String, Map<String, Object>> parser) {
3838
return trimParse(json, "{", parser);
3939
}
4040

41-
protected final List<Object> parseList(String json, Function<String, List<Object>> parser) {
41+
protected final List<Object> parseList(@Nullable String json, Function<String, List<Object>> parser) {
4242
return trimParse(json, "[", parser);
4343
}
4444

core/spring-boot/src/main/java/org/springframework/boot/json/BasicJsonParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ public class BasicJsonParser extends AbstractJsonParser {
4444
private static final int MAX_DEPTH = 1000;
4545

4646
@Override
47-
public Map<String, Object> parseMap(String json) {
47+
public Map<String, Object> parseMap(@Nullable String json) {
4848
return tryParse(() -> parseMap(json, (jsonToParse) -> parseMapInternal(0, jsonToParse)), Exception.class);
4949
}
5050

5151
@Override
52-
public List<Object> parseList(String json) {
52+
public List<Object> parseList(@Nullable String json) {
5353
return tryParse(() -> parseList(json, (jsonToParse) -> parseListInternal(0, jsonToParse)), Exception.class);
5454
}
5555

core/spring-boot/src/main/java/org/springframework/boot/json/GsonJsonParser.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.gson.Gson;
2323
import com.google.gson.GsonBuilder;
2424
import com.google.gson.reflect.TypeToken;
25+
import org.jspecify.annotations.Nullable;
2526

2627
/**
2728
* Thin wrapper to adapt {@link Gson} to a {@link JsonParser}.
@@ -40,13 +41,13 @@ public class GsonJsonParser extends AbstractJsonParser {
4041
private final Gson gson = new GsonBuilder().create();
4142

4243
@Override
43-
public Map<String, Object> parseMap(String json) {
44+
public Map<String, Object> parseMap(@Nullable String json) {
4445
return tryParse(() -> parseMap(json, (trimmed) -> this.gson.fromJson(trimmed, MAP_TYPE.getType())),
4546
Exception.class);
4647
}
4748

4849
@Override
49-
public List<Object> parseList(String json) {
50+
public List<Object> parseList(@Nullable String json) {
5051
return tryParse(() -> parseList(json, (trimmed) -> this.gson.fromJson(trimmed, LIST_TYPE.getType())),
5152
Exception.class);
5253
}

core/spring-boot/src/main/java/org/springframework/boot/json/JacksonJsonParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public JacksonJsonParser() {
5353
}
5454

5555
@Override
56-
public Map<String, Object> parseMap(String json) {
56+
public Map<String, Object> parseMap(@Nullable String json) {
5757
return tryParse(() -> getObjectMapper().readValue(json, MAP_TYPE), Exception.class);
5858
}
5959

6060
@Override
61-
public List<Object> parseList(String json) {
61+
public List<Object> parseList(@Nullable String json) {
6262
return tryParse(() -> getObjectMapper().readValue(json, LIST_TYPE), Exception.class);
6363
}
6464

0 commit comments

Comments
 (0)