Skip to content

Commit 31e2a3d

Browse files
committed
Enables jspecify
1 parent 11363c7 commit 31e2a3d

14 files changed

+124
-65
lines changed

.mvn/maven.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
-DaltSnapshotDeploymentRepository=repo.spring.io::default::https://repo.spring.io/libs-snapshot-local -P spring
1+
-Djspecify.enabled=true
2+
-P spring

spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/ConfigurationPropertiesRebinderAutoConfiguration.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.springframework.cloud.context.properties.ConfigurationPropertiesBeans;
2525
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder;
2626
import org.springframework.context.ApplicationContext;
27-
import org.springframework.context.ApplicationContextAware;
2827
import org.springframework.context.annotation.Bean;
2928
import org.springframework.context.annotation.Configuration;
3029

@@ -35,14 +34,12 @@
3534
*/
3635
@Configuration(proxyBeanMethods = false)
3736
@ConditionalOnBean(ConfigurationPropertiesBindingPostProcessor.class)
38-
public class ConfigurationPropertiesRebinderAutoConfiguration
39-
implements ApplicationContextAware, SmartInitializingSingleton {
37+
public class ConfigurationPropertiesRebinderAutoConfiguration implements SmartInitializingSingleton {
4038

41-
private ApplicationContext context;
39+
private final ApplicationContext context;
4240

43-
@Override
44-
public void setApplicationContext(ApplicationContext applicationContext) {
45-
this.context = applicationContext;
41+
public ConfigurationPropertiesRebinderAutoConfiguration(ApplicationContext context) {
42+
this.context = context;
4643
}
4744

4845
@Bean

spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshAutoConfiguration.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616

1717
package org.springframework.cloud.autoconfigure;
1818

19+
import java.util.ArrayList;
1920
import java.util.HashSet;
2021
import java.util.List;
2122
import java.util.Set;
2223

24+
import org.jspecify.annotations.Nullable;
25+
2326
import org.springframework.aop.scope.ScopedProxyUtils;
2427
import org.springframework.beans.BeansException;
2528
import org.springframework.beans.factory.BeanFactory;
@@ -133,7 +136,7 @@ public static class RefreshProperties {
133136
* property sources are retained. This property allows property sources, such as
134137
* property sources created by EnvironmentPostProcessors to be retained as well.
135138
*/
136-
private List<String> additionalPropertySourcesToRetain;
139+
private List<String> additionalPropertySourcesToRetain = new ArrayList<>();
137140

138141
public List<String> getAdditionalPropertySourcesToRetain() {
139142
return this.additionalPropertySourcesToRetain;
@@ -157,7 +160,7 @@ public String toString() {
157160
protected static class RefreshScopeBeanDefinitionEnhancer
158161
implements BeanDefinitionRegistryPostProcessor, EnvironmentAware {
159162

160-
private Environment environment;
163+
private @Nullable Environment environment;
161164

162165
private boolean bound = false;
163166

spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.util.Map;
2828
import java.util.Set;
2929

30+
import org.jspecify.annotations.Nullable;
31+
3032
import org.springframework.beans.factory.ListableBeanFactory;
3133
import org.springframework.boot.Banner.Mode;
3234
import org.springframework.boot.SpringApplication;
@@ -118,19 +120,24 @@ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
118120
apply(context, event.getSpringApplication(), environment);
119121
}
120122

121-
private ConfigurableApplicationContext findBootstrapContext(ParentContextApplicationContextInitializer initializer,
122-
String configName) {
123+
private @Nullable ConfigurableApplicationContext findBootstrapContext(
124+
ParentContextApplicationContextInitializer initializer, String configName) {
125+
ConfigurableApplicationContext parent = null;
123126
Field field = ReflectionUtils.findField(ParentContextApplicationContextInitializer.class, "parent");
124-
ReflectionUtils.makeAccessible(field);
125-
ConfigurableApplicationContext parent = safeCast(ConfigurableApplicationContext.class,
126-
ReflectionUtils.getField(field, initializer));
127-
if (parent != null && !configName.equals(parent.getId())) {
128-
parent = safeCast(ConfigurableApplicationContext.class, parent.getParent());
127+
if (field != null) {
128+
ReflectionUtils.makeAccessible(field);
129+
Object value = ReflectionUtils.getField(field, initializer);
130+
if (value != null) {
131+
parent = safeCast(ConfigurableApplicationContext.class, value);
132+
}
133+
if (parent != null && !configName.equals(parent.getId()) && parent.getParent() != null) {
134+
parent = safeCast(ConfigurableApplicationContext.class, parent.getParent());
135+
}
129136
}
130137
return parent;
131138
}
132139

133-
private <T> T safeCast(Class<T> type, Object object) {
140+
private <T> @Nullable T safeCast(Class<T> type, Object object) {
134141
try {
135142
return type.cast(object);
136143
}
@@ -177,7 +184,7 @@ private ConfigurableApplicationContext bootstrapServiceContext(ConfigurableEnvir
177184
.logStartupInfo(false)
178185
.web(WebApplicationType.NONE);
179186
final SpringApplication builderApplication = builder.application();
180-
if (builderApplication.getMainApplicationClass() == null) {
187+
if (builderApplication.getMainApplicationClass() == null && application.getMainApplicationClass() != null) {
181188
// gh_425:
182189
// SpringApplication cannot deduce the MainApplicationClass here
183190
// if it is booted from SpringBootServletInitializer due to the
@@ -225,7 +232,7 @@ private void mergeDefaultProperties(MutablePropertySources environment, MutableP
225232
String name = DEFAULT_PROPERTIES;
226233
if (bootstrap.contains(name)) {
227234
PropertySource<?> source = bootstrap.get(name);
228-
if (!environment.contains(name)) {
235+
if (!environment.contains(name) && source != null) {
229236
environment.addLast(source);
230237
}
231238
else {
@@ -246,6 +253,9 @@ private void mergeDefaultProperties(MutablePropertySources environment, MutableP
246253

247254
private void mergeAdditionalPropertySources(MutablePropertySources environment, MutablePropertySources bootstrap) {
248255
PropertySource<?> defaultProperties = environment.get(DEFAULT_PROPERTIES);
256+
if (defaultProperties == null) {
257+
defaultProperties = new MapPropertySource(DEFAULT_PROPERTIES, new LinkedHashMap<>());
258+
}
249259
ExtendedDefaultPropertySource result = defaultProperties instanceof ExtendedDefaultPropertySource
250260
? (ExtendedDefaultPropertySource) defaultProperties
251261
: new ExtendedDefaultPropertySource(DEFAULT_PROPERTIES, defaultProperties);
@@ -456,7 +466,7 @@ public void add(PropertySource<?> source) {
456466
}
457467

458468
@Override
459-
public Object getProperty(String name) {
469+
public @Nullable Object getProperty(String name) {
460470
if (this.sources.containsProperty(name)) {
461471
return this.sources.getProperty(name);
462472
}
@@ -480,7 +490,7 @@ public String[] getPropertyNames() {
480490
}
481491

482492
@Override
483-
public Origin getOrigin(String name) {
493+
public @Nullable Origin getOrigin(String name) {
484494
return this.sources.getOrigin(name);
485495
}
486496

0 commit comments

Comments
 (0)