Skip to content

Commit 4ad5d26

Browse files
committed
Add nullability annotations to core/spring-boot-properties-migrator
See gh-46587
1 parent cb2a26c commit 4ad5d26

File tree

5 files changed

+32
-16
lines changed

5 files changed

+32
-16
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.commons.logging.Log;
2323
import org.apache.commons.logging.LogFactory;
24+
import org.jspecify.annotations.Nullable;
2425

2526
import org.springframework.boot.configurationmetadata.ConfigurationMetadataRepository;
2627
import org.springframework.boot.configurationmetadata.ConfigurationMetadataRepositoryJsonBuilder;
@@ -44,7 +45,7 @@ class PropertiesMigrationListener implements ApplicationListener<SpringApplicati
4445

4546
private static final Log logger = LogFactory.getLog(PropertiesMigrationListener.class);
4647

47-
private PropertiesMigrationReport report;
48+
private @Nullable PropertiesMigrationReport report;
4849

4950
private boolean reported;
5051

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.function.Function;
2424
import java.util.stream.Collectors;
2525

26+
import org.jspecify.annotations.Nullable;
27+
2628
/**
2729
* Provides a properties migration report.
2830
*
@@ -38,7 +40,7 @@ class PropertiesMigrationReport {
3840
* properties were found, return {@code null}.
3941
* @return a report with the configurations keys that should be renamed
4042
*/
41-
String getWarningReport() {
43+
@Nullable String getWarningReport() {
4244
Map<String, List<PropertyMigration>> content = getContent(LegacyProperties::getRenamed);
4345
if (content.isEmpty()) {
4446
return null;
@@ -60,7 +62,7 @@ String getWarningReport() {
6062
* properties were found, return {@code null}.
6163
* @return a report with the configurations keys that are no longer supported
6264
*/
63-
String getErrorReport() {
65+
@Nullable String getErrorReport() {
6466
Map<String, List<PropertyMigration>> content = getContent(LegacyProperties::getUnsupported);
6567
if (content.isEmpty()) {
6668
return null;

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.util.Set;
2727
import java.util.function.Predicate;
2828

29+
import org.jspecify.annotations.Nullable;
30+
2931
import org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty;
3032
import org.springframework.boot.configurationmetadata.ConfigurationMetadataRepository;
3133
import org.springframework.boot.context.properties.source.ConfigurationProperty;
@@ -38,6 +40,7 @@
3840
import org.springframework.boot.origin.PropertySourceOrigin;
3941
import org.springframework.core.env.ConfigurableEnvironment;
4042
import org.springframework.core.env.PropertySource;
43+
import org.springframework.util.Assert;
4144
import org.springframework.util.LinkedMultiValueMap;
4245
import org.springframework.util.MultiValueMap;
4346
import org.springframework.util.StringUtils;
@@ -109,7 +112,9 @@ private String determinePropertySourceName(ConfigurationPropertySource source) {
109112
if (source.getUnderlyingSource() instanceof PropertySource<?> underlyingSource) {
110113
return underlyingSource.getName();
111114
}
112-
return source.getUnderlyingSource().toString();
115+
Object underlyingSource = source.getUnderlyingSource();
116+
Assert.state(underlyingSource != null, "'underlyingSource' must not be null");
117+
return underlyingSource.toString();
113118
}
114119

115120
private List<PropertyMigration> getMigrations(ConfigurationPropertySource propertySource,
@@ -149,7 +154,8 @@ private boolean hasSameName(ConfigurationProperty property, ConfigurationMetadat
149154
&& Objects.equals(propertySourceOrigin.getPropertyName(), replacement.getId());
150155
}
151156

152-
private ConfigurationMetadataProperty determineReplacementMetadata(ConfigurationMetadataProperty metadata) {
157+
private @Nullable ConfigurationMetadataProperty determineReplacementMetadata(
158+
ConfigurationMetadataProperty metadata) {
153159
String replacementId = metadata.getDeprecation().getReplacement();
154160
if (StringUtils.hasText(replacementId)) {
155161
ConfigurationMetadataProperty replacement = this.allProperties.get(replacementId);
@@ -161,7 +167,7 @@ private ConfigurationMetadataProperty determineReplacementMetadata(Configuration
161167
return null;
162168
}
163169

164-
private ConfigurationMetadataProperty detectMapValueReplacement(String fullId) {
170+
private @Nullable ConfigurationMetadataProperty detectMapValueReplacement(String fullId) {
165171
int lastDot = fullId.lastIndexOf('.');
166172
if (lastDot == -1) {
167173
return null;
@@ -178,7 +184,7 @@ private boolean isMapType(ConfigurationMetadataProperty property) {
178184
return type != null && type.startsWith(Map.class.getName());
179185
}
180186

181-
private PropertySource<?> mapPropertiesWithReplacement(PropertiesMigrationReport report, String name,
187+
private @Nullable PropertySource<?> mapPropertiesWithReplacement(PropertiesMigrationReport report, String name,
182188
List<PropertyMigration> properties) {
183189
report.add(name, properties);
184190
List<PropertyMigration> renamed = properties.stream().filter(PropertyMigration::isCompatibleType).toList();
@@ -229,7 +235,7 @@ boolean isPlaceholderThatAccessesName(Object value, String name) {
229235
}
230236

231237
@Override
232-
public Object getProperty(String name) {
238+
public @Nullable Object getProperty(String name) {
233239
this.accessedNames.add(name);
234240
return null;
235241
}

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.Comparator;
2121
import java.util.Map;
2222

23+
import org.jspecify.annotations.Nullable;
24+
2325
import org.springframework.boot.configurationmetadata.ConfigurationMetadataProperty;
2426
import org.springframework.boot.configurationmetadata.Deprecation;
2527
import org.springframework.boot.context.properties.source.ConfigurationProperty;
@@ -43,11 +45,11 @@ class PropertyMigration {
4345

4446
private final ConfigurationProperty property;
4547

46-
private final Integer lineNumber;
48+
private final @Nullable Integer lineNumber;
4749

4850
private final ConfigurationMetadataProperty metadata;
4951

50-
private final ConfigurationMetadataProperty replacementMetadata;
52+
private final @Nullable ConfigurationMetadataProperty replacementMetadata;
5153

5254
/**
5355
* Whether this migration happened from a map type.
@@ -57,7 +59,7 @@ class PropertyMigration {
5759
private final boolean compatibleType;
5860

5961
PropertyMigration(ConfigurationProperty property, ConfigurationMetadataProperty metadata,
60-
ConfigurationMetadataProperty replacementMetadata, boolean mapMigration) {
62+
@Nullable ConfigurationMetadataProperty replacementMetadata, boolean mapMigration) {
6163
this.property = property;
6264
this.lineNumber = determineLineNumber(property);
6365
this.metadata = metadata;
@@ -66,7 +68,7 @@ class PropertyMigration {
6668
this.compatibleType = determineCompatibleType(metadata, replacementMetadata);
6769
}
6870

69-
private static Integer determineLineNumber(ConfigurationProperty property) {
71+
private static @Nullable Integer determineLineNumber(ConfigurationProperty property) {
7072
Origin origin = property.getOrigin();
7173
if (origin instanceof PropertySourceOrigin propertySourceOrigin) {
7274
origin = propertySourceOrigin.getOrigin();
@@ -80,7 +82,7 @@ private static Integer determineLineNumber(ConfigurationProperty property) {
8082
}
8183

8284
private static boolean determineCompatibleType(ConfigurationMetadataProperty metadata,
83-
ConfigurationMetadataProperty replacementMetadata) {
85+
@Nullable ConfigurationMetadataProperty replacementMetadata) {
8486
String currentType = determineType(metadata);
8587
String replacementType = determineType(replacementMetadata);
8688
if (currentType == null || replacementType == null) {
@@ -93,7 +95,7 @@ private static boolean determineCompatibleType(ConfigurationMetadataProperty met
9395
&& (currentType.equals(Long.class.getName()) || currentType.equals(Integer.class.getName()));
9496
}
9597

96-
private static String determineType(ConfigurationMetadataProperty metadata) {
98+
private static @Nullable String determineType(@Nullable ConfigurationMetadataProperty metadata) {
9799
if (metadata == null || metadata.getType() == null) {
98100
return null;
99101
}
@@ -112,7 +114,7 @@ ConfigurationProperty getProperty() {
112114
return this.property;
113115
}
114116

115-
Integer getLineNumber() {
117+
@Nullable Integer getLineNumber() {
116118
return this.lineNumber;
117119
}
118120

@@ -126,7 +128,9 @@ boolean isCompatibleType() {
126128

127129
String getNewPropertyName() {
128130
if (this.mapMigration) {
129-
return getNewMapPropertyName(this.property, this.metadata, this.replacementMetadata).toString();
131+
ConfigurationMetadataProperty replacementMetadata = this.replacementMetadata;
132+
Assert.state(replacementMetadata != null, "'replacementMetadata' must not be null");
133+
return getNewMapPropertyName(this.property, this.metadata, replacementMetadata).toString();
130134
}
131135
return this.metadata.getDeprecation().getReplacement();
132136
}

core/spring-boot-properties-migrator/src/main/java/org/springframework/boot/context/properties/migrator/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Support for migrating legacy Spring Boot properties.
1919
*/
20+
@NullMarked
2021
package org.springframework.boot.context.properties.migrator;
22+
23+
import org.jspecify.annotations.NullMarked;

0 commit comments

Comments
 (0)