• 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

72.73
today-context/src/main/java/infra/context/properties/ConfigurationPropertiesCharSequenceToObjectConverter.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;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.util.Collections;
23
import java.util.Set;
24

25
import infra.core.TypeDescriptor;
26
import infra.core.conversion.ConditionalGenericConverter;
27
import infra.core.conversion.ConversionService;
28
import infra.format.support.ApplicationConversionService;
29

30
/**
31
 * @author Phillip Webb
32
 * @author Andy Wilkinson
33
 * @author <a href="https://github.com/TAKETODAY">海子 Yang</a>
34
 * @since 5.0
35
 */
36
final class ConfigurationPropertiesCharSequenceToObjectConverter implements ConditionalGenericConverter {
37

38
  private static final TypeDescriptor STRING = TypeDescriptor.valueOf(String.class);
3✔
39

40
  private static final TypeDescriptor BYTE_ARRAY = TypeDescriptor.valueOf(byte[].class);
3✔
41

42
  private static final Set<ConvertiblePair> TYPES;
43

44
  private final ThreadLocal<Boolean> disable = new ThreadLocal<>();
5✔
45

46
  static {
47
    TYPES = Collections.singleton(new ConvertiblePair(CharSequence.class, Object.class));
7✔
48
  }
1✔
49

50
  private final ConversionService conversionService;
51

52
  ConfigurationPropertiesCharSequenceToObjectConverter(ConversionService conversionService) {
2✔
53
    this.conversionService = conversionService;
3✔
54
  }
1✔
55

56
  @Override
57
  public Set<ConvertiblePair> getConvertibleTypes() {
58
    return TYPES;
2✔
59
  }
60

61
  @Override
62
  public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
63
    if (sourceType.getType() == String.class || this.disable.get() == Boolean.TRUE) {
9✔
64
      return false;
2✔
65
    }
66
    this.disable.set(Boolean.TRUE);
4✔
67
    try {
68
      boolean canDirectlyConvertCharSequence = this.conversionService.canConvert(sourceType, targetType);
6✔
69
      if (canDirectlyConvertCharSequence && !isStringConversionBetter(sourceType, targetType)) {
7!
70
        return false;
×
71
      }
72
      return this.conversionService.canConvert(STRING, targetType);
8✔
73
    }
74
    finally {
75
      this.disable.remove();
3✔
76
    }
77
  }
78

79
  /**
80
   * Return if String based conversion is better based on the target type. This is
81
   * required when ObjectTo... conversion produces incorrect results.
82
   *
83
   * @param sourceType the source type to test
84
   * @param targetType the target type to test
85
   * @return if string conversion is better
86
   */
87
  private boolean isStringConversionBetter(TypeDescriptor sourceType, TypeDescriptor targetType) {
88
    if (this.conversionService instanceof ApplicationConversionService applicationConversionService) {
6!
89
      if (applicationConversionService.isConvertViaObjectSourceType(sourceType, targetType)) {
×
90
        // If an ObjectTo... converter is being used then there might be a
91
        // better StringTo... version
92
        return true;
×
93
      }
94
    }
95
    // StringToArrayConverter / StringToCollectionConverter are better than
96
    // ObjectToArrayConverter / ObjectToCollectionConverter
97
    return (targetType.isArray() || targetType.isCollection()) && !targetType.equals(BYTE_ARRAY);
13!
98
  }
99

100
  @Nullable
101
  @Override
102
  public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
103
    if (source == null) {
2!
104
      return null;
×
105
    }
106
    return this.conversionService.convert(source.toString(), STRING, targetType);
8✔
107
  }
108

109
}
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