• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TAKETODAY / today-infrastructure / 18154768944

01 Oct 2025 07:26AM UTC coverage: 81.882% (-0.005%) from 81.887%
18154768944

push

github

web-flow
Merge pull request #290 from TAKETODAY/dev/jspecify

jspecify

59788 of 78013 branches covered (76.64%)

Branch coverage included in aggregate %.

141239 of 167496 relevant lines covered (84.32%)

3.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.65
today-context/src/main/java/infra/context/properties/source/ConfigurationPropertySourcesPropertyResolver.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.context.properties.source;
19

20
import org.jspecify.annotations.Nullable;
21

22
import infra.core.conversion.ConversionFailedException;
23
import infra.core.env.AbstractPropertyResolver;
24
import infra.core.env.PropertySource;
25
import infra.core.env.PropertySources;
26
import infra.core.env.PropertySourcesPropertyResolver;
27

28
/**
29
 * Alternative {@link PropertySourcesPropertyResolver} implementation that recognizes
30
 * {@link ConfigurationPropertySourcesPropertySource} and saves duplicate calls to the
31
 * underlying sources if the name is a value {@link ConfigurationPropertyName}.
32
 *
33
 * @author Phillip Webb
34
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
35
 * @since 4.0
36
 */
37
class ConfigurationPropertySourcesPropertyResolver extends AbstractPropertyResolver {
38

39
  private final PropertySources propertySources;
40

41
  private final DefaultResolver defaultResolver;
42

43
  ConfigurationPropertySourcesPropertyResolver(PropertySources propertySources) {
2✔
44
    this.propertySources = propertySources;
3✔
45
    this.defaultResolver = new DefaultResolver(propertySources);
6✔
46
  }
1✔
47

48
  @Override
49
  public boolean containsProperty(String key) {
50
    var attached = getAttached();
3✔
51
    if (attached != null) {
2!
52
      ConfigurationPropertyName name = ConfigurationPropertyName.of(key, true);
4✔
53
      if (name != null) {
2✔
54
        try {
55
          return attached.findConfigurationProperty(name) != null;
8✔
56
        }
57
        catch (Exception ignored) {
×
58
        }
59
      }
60
    }
61
    return this.defaultResolver.containsProperty(key);
5✔
62
  }
63

64
  @Nullable
65
  @Override
66
  public String getProperty(String key) {
67
    return getProperty(key, String.class, true);
7✔
68
  }
69

70
  @Nullable
71
  @Override
72
  public <T> T getProperty(String key, Class<T> targetValueType) {
73
    return getProperty(key, targetValueType, true);
6✔
74
  }
75

76
  @Nullable
77
  @Override
78
  protected String getPropertyAsRawString(String key) {
79
    return getProperty(key, String.class, false);
7✔
80
  }
81

82
  @Nullable
83
  private <T> T getProperty(String key, Class<T> targetValueType, boolean resolveNestedPlaceholders) {
84
    Object value = findPropertyValue(key);
4✔
85
    if (value == null) {
2✔
86
      return null;
2✔
87
    }
88
    if (resolveNestedPlaceholders && value instanceof String) {
5✔
89
      value = resolveNestedPlaceholders((String) value);
5✔
90
    }
91
    try {
92
      return convertValueIfNecessary(value, targetValueType);
5✔
93
    }
94
    catch (ConversionFailedException ex) {
1✔
95
      Exception wrappedCause = new InvalidConfigurationPropertyValueException(key, value,
5✔
96
              "Failed to convert to type " + ex.getTargetType(), ex.getCause());
7✔
97
      throw new ConversionFailedException(ex.getSourceType(), ex.getTargetType(), ex.getValue(), wrappedCause);
11✔
98
    }
99
  }
100

101
  @Nullable
102
  private Object findPropertyValue(String key) {
103
    var attached = getAttached();
3✔
104
    if (attached != null) {
2✔
105
      ConfigurationPropertyName name = ConfigurationPropertyName.of(key, true);
4✔
106
      if (name != null) {
2✔
107
        try {
108
          ConfigurationProperty configurationProperty = attached.findConfigurationProperty(name);
4✔
109
          return configurationProperty != null ? configurationProperty.getValue() : null;
7✔
110
        }
111
        catch (Exception ignored) {
×
112
        }
113
      }
114
    }
115
    return this.defaultResolver.getProperty(key, Object.class, false);
7✔
116
  }
117

118
  @Nullable
119
  private ConfigurationPropertySourcesPropertySource getAttached() {
120
    PropertySource<?> attached = ConfigurationPropertySources.getAttached(this.propertySources);
4✔
121
    Object attachedSource = attached != null ? attached.getSource() : null;
7✔
122
    if (attachedSource instanceof DefaultConfigurationPropertySources cps && cps.isUsingSources(this.propertySources)) {
11!
123
      return (ConfigurationPropertySourcesPropertySource) attached;
3✔
124
    }
125
    return null;
2✔
126
  }
127

128
  /**
129
   * Default {@link PropertySourcesPropertyResolver} used if
130
   * {@link ConfigurationPropertySources} is not attached.
131
   */
132
  static class DefaultResolver extends PropertySourcesPropertyResolver {
133

134
    DefaultResolver(PropertySources propertySources) {
135
      super(propertySources);
3✔
136
    }
1✔
137

138
  }
139

140
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc