Skip to content

Commit fc68b00

Browse files
committed
Remove isImmutable and getPrefix from OriginLookup
Drop `isImmutable` and `getPrefix` from `OriginLookup` since they're not really Origin concerns. Now that `env` is a foundational layer we can add a dedicated `PropertySourceInfo` interface and add that to the `o.s.b.env` package without creating a package tangle. Closes gh-45547
1 parent 24c25ae commit fc68b00

File tree

12 files changed

+77
-59
lines changed

12 files changed

+77
-59
lines changed

config/checkstyle/import-control.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
<subpackage name="ssl">
4646
<disallow pkg="org.springframework.boot" exact-match="true"/>
4747
</subpackage>
48+
<subpackage name="origin">
49+
<disallow pkg="org.springframework.boot" exact-match="true"/>
50+
</subpackage>
4851
<subpackage name="util">
4952
<disallow pkg="org.springframework.boot" exact-match="true"/>
5053
</subpackage>

core/spring-boot/src/main/java/org/springframework/boot/ApplicationInfoPropertySource.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121

2222
import org.jspecify.annotations.Nullable;
2323

24-
import org.springframework.boot.origin.Origin;
25-
import org.springframework.boot.origin.OriginLookup;
24+
import org.springframework.boot.env.PropertySourceInfo;
2625
import org.springframework.boot.system.ApplicationPid;
2726
import org.springframework.core.env.ConfigurableEnvironment;
2827
import org.springframework.core.env.MapPropertySource;
@@ -36,7 +35,7 @@
3635
*
3736
* @author Moritz Halbritter
3837
*/
39-
class ApplicationInfoPropertySource extends MapPropertySource implements OriginLookup<String> {
38+
class ApplicationInfoPropertySource extends MapPropertySource implements PropertySourceInfo {
4039

4140
static final String NAME = "applicationInfo";
4241

@@ -48,11 +47,6 @@ class ApplicationInfoPropertySource extends MapPropertySource implements OriginL
4847
super(NAME, getProperties(applicationVersion));
4948
}
5049

51-
@Override
52-
public @Nullable Origin getOrigin(String key) {
53-
return null;
54-
}
55-
5650
@Override
5751
public boolean isImmutable() {
5852
return true;

core/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.springframework.boot.context.properties.bind.Binder;
3232
import org.springframework.boot.context.properties.bind.PlaceholdersResolver;
3333
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
34-
import org.springframework.boot.origin.OriginLookup;
34+
import org.springframework.boot.env.PropertySourceInfo;
3535
import org.springframework.core.convert.ConversionService;
3636
import org.springframework.core.env.Environment;
3737
import org.springframework.core.env.PropertySource;
@@ -454,8 +454,8 @@ static ConfigDataEnvironmentContributor ofUnboundImport(@Nullable ConfigDataLoca
454454
private static @Nullable ConfigurationPropertySource asConfigurationPropertySource(
455455
PropertySource<?> propertySource) {
456456
ConfigurationPropertySource configurationPropertySource = ConfigurationPropertySource.from(propertySource);
457-
if (configurationPropertySource != null && propertySource instanceof OriginLookup<?> originLookup) {
458-
configurationPropertySource = configurationPropertySource.withPrefix(originLookup.getPrefix());
457+
if (configurationPropertySource != null && propertySource instanceof PropertySourceInfo propertySourceInfo) {
458+
configurationPropertySource = configurationPropertySource.withPrefix(propertySourceInfo.getPrefix());
459459
}
460460
return configurationPropertySource;
461461
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import org.jspecify.annotations.Nullable;
2727

28-
import org.springframework.boot.origin.OriginLookup;
28+
import org.springframework.boot.env.PropertySourceInfo;
2929
import org.springframework.core.env.ConfigurableEnvironment;
3030
import org.springframework.core.env.MutablePropertySources;
3131
import org.springframework.core.env.PropertySource;
@@ -69,8 +69,8 @@ private ConfigurationPropertySource adapt(PropertySource<?> source) {
6969
return result;
7070
}
7171
result = SpringConfigurationPropertySource.from(source);
72-
if (source instanceof OriginLookup<?> originLookup) {
73-
result = result.withPrefix(originLookup.getPrefix());
72+
if (source instanceof PropertySourceInfo propertySourceInfo) {
73+
result = result.withPrefix(propertySourceInfo.getPrefix());
7474
}
7575
this.cache.put(source, result);
7676
return result;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232

3333
import org.jspecify.annotations.Nullable;
3434

35+
import org.springframework.boot.env.PropertySourceInfo;
3536
import org.springframework.boot.origin.Origin;
36-
import org.springframework.boot.origin.OriginLookup;
3737
import org.springframework.boot.origin.PropertySourceOrigin;
3838
import org.springframework.core.env.EnumerablePropertySource;
3939
import org.springframework.core.env.MapPropertySource;
@@ -202,8 +202,8 @@ private Cache updateCache(Cache cache) {
202202

203203
boolean isImmutablePropertySource() {
204204
EnumerablePropertySource<?> source = getPropertySource();
205-
if (source instanceof OriginLookup<?> originLookup) {
206-
return originLookup.isImmutable();
205+
if (source instanceof PropertySourceInfo propertySourceInfo) {
206+
return propertySourceInfo.isImmutable();
207207
}
208208
if (StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME.equals(source.getName())) {
209209
return source.getSource() == System.getenv();

core/spring-boot/src/main/java/org/springframework/boot/env/ConfigTreePropertySource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@
7676
* @author Phillip Webb
7777
* @since 2.4.0
7878
*/
79-
public class ConfigTreePropertySource extends EnumerablePropertySource<Path> implements OriginLookup<String> {
79+
public class ConfigTreePropertySource extends EnumerablePropertySource<Path>
80+
implements PropertySourceInfo, OriginLookup<String> {
8081

8182
private static final int MAX_DEPTH = 100;
8283

core/spring-boot/src/main/java/org/springframework/boot/env/OriginTrackedMapPropertySource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
* @since 2.0.0
3535
* @see OriginTrackedValue
3636
*/
37-
public final class OriginTrackedMapPropertySource extends MapPropertySource implements OriginLookup<String> {
37+
public final class OriginTrackedMapPropertySource extends MapPropertySource
38+
implements PropertySourceInfo, OriginLookup<String> {
3839

3940
private final boolean immutable;
4041

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.env;
18+
19+
import org.jspecify.annotations.Nullable;
20+
21+
import org.springframework.core.env.PropertySource;
22+
23+
/**
24+
* Interface that can be optionally implemented by a {@link PropertySource} to provide
25+
* additional information.
26+
*
27+
* @author Phillip Webb
28+
* @since 4.0.0
29+
*/
30+
public interface PropertySourceInfo {
31+
32+
/**
33+
* Return {@code true} if this lookup is immutable and has contents that will never
34+
* change.
35+
* @return if the lookup is immutable
36+
*/
37+
default boolean isImmutable() {
38+
return false;
39+
}
40+
41+
/**
42+
* Return the implicit prefix that is applied when performing a lookup or {@code null}
43+
* if no prefix is used. Prefixes can be used to disambiguate keys that would
44+
* otherwise clash. For example, if multiple applications are running on the same
45+
* machine a different prefix can be set on each application to ensure that different
46+
* environment variables are used.
47+
* @return the prefix applied by the lookup class or {@code null}.
48+
*/
49+
default @Nullable String getPrefix() {
50+
return null;
51+
}
52+
53+
}

core/spring-boot/src/main/java/org/springframework/boot/origin/OriginLookup.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,6 @@ public interface OriginLookup<K> {
3737
*/
3838
@Nullable Origin getOrigin(K key);
3939

40-
/**
41-
* Return {@code true} if this lookup is immutable and has contents that will never
42-
* change.
43-
* @return if the lookup is immutable
44-
* @since 2.2.0
45-
*/
46-
default boolean isImmutable() {
47-
return false;
48-
}
49-
50-
/**
51-
* Return the implicit prefix that is applied when performing a lookup or {@code null}
52-
* if no prefix is used. Prefixes can be used to disambiguate keys that would
53-
* otherwise clash. For example, if multiple applications are running on the same
54-
* machine a different prefix can be set on each application to ensure that different
55-
* environment variables are used.
56-
* @return the prefix applied by the lookup class or {@code null}.
57-
* @since 2.5.0
58-
*/
59-
default @Nullable String getPrefix() {
60-
return null;
61-
}
62-
6340
/**
6441
* Attempt to look up the origin from the given source. If the source is not a
6542
* {@link OriginLookup} or if an exception occurs during lookup then {@code null} is

core/spring-boot/src/main/java/org/springframework/boot/support/SystemEnvironmentPropertySourceEnvironmentPostProcessor.java

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

2323
import org.springframework.boot.EnvironmentPostProcessor;
2424
import org.springframework.boot.SpringApplication;
25+
import org.springframework.boot.env.PropertySourceInfo;
2526
import org.springframework.boot.origin.Origin;
2627
import org.springframework.boot.origin.OriginLookup;
2728
import org.springframework.boot.origin.SystemEnvironmentOrigin;
@@ -81,7 +82,7 @@ public void setOrder(int order) {
8182
* {@link SystemEnvironmentPropertySource} that also tracks {@link Origin}.
8283
*/
8384
protected static class OriginAwareSystemEnvironmentPropertySource extends SystemEnvironmentPropertySource
84-
implements OriginLookup<String> {
85+
implements PropertySourceInfo, OriginLookup<String> {
8586

8687
private final @Nullable String prefix;
8788

0 commit comments

Comments
 (0)