• 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

81.82
today-context/src/main/java/infra/validation/MessageInterpolatorFactory.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.validation;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.util.Collections;
23
import java.util.LinkedHashSet;
24
import java.util.Set;
25
import java.util.function.Supplier;
26

27
import infra.beans.BeanUtils;
28
import infra.beans.BeansException;
29
import infra.context.MessageSource;
30
import infra.util.ClassUtils;
31
import jakarta.validation.MessageInterpolator;
32
import jakarta.validation.Validation;
33
import jakarta.validation.ValidationException;
34

35
/**
36
 * {@link Supplier} that can be used to create a {@link MessageInterpolator}.
37
 * Attempts to pick the most appropriate {@link MessageInterpolator} based on the
38
 * classpath.
39
 *
40
 * @author Phillip Webb
41
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
42
 * @since 4.0
43
 */
44
public class MessageInterpolatorFactory implements Supplier<MessageInterpolator> {
45

46
  private static final Set<String> FALLBACKS;
47

48
  static {
49
    Set<String> fallbacks = new LinkedHashSet<>();
4✔
50
    fallbacks.add("org.hibernate.validator.messageinterpolation.ParameterMessageInterpolator");
4✔
51
    FALLBACKS = Collections.unmodifiableSet(fallbacks);
3✔
52
  }
1✔
53

54
  @Nullable
55
  private final MessageSource messageSource;
56

57
  public MessageInterpolatorFactory() {
58
    this(null);
3✔
59
  }
1✔
60

61
  /**
62
   * Creates a new {@link MessageInterpolatorFactory} that will produce a
63
   * {@link MessageInterpolator} that uses the given {@code messageSource} to resolve
64
   * any message parameters before final interpolation.
65
   *
66
   * @param messageSource message source to be used by the interpolator
67
   */
68
  public MessageInterpolatorFactory(@Nullable MessageSource messageSource) {
2✔
69
    this.messageSource = messageSource;
3✔
70
  }
1✔
71

72
  @Override
73
  public MessageInterpolator get() throws BeansException {
74
    MessageInterpolator messageInterpolator = getMessageInterpolator();
3✔
75
    if (this.messageSource != null) {
3✔
76
      return new MessageSourceMessageInterpolator(this.messageSource, messageInterpolator);
7✔
77
    }
78
    return messageInterpolator;
2✔
79
  }
80

81
  private MessageInterpolator getMessageInterpolator() {
82
    try {
83
      return Validation.byDefaultProvider().configure().getDefaultMessageInterpolator();
4✔
84
    }
85
    catch (ValidationException ex) {
1✔
86
      MessageInterpolator fallback = getFallback();
3✔
87
      if (fallback != null) {
2!
88
        return fallback;
×
89
      }
90
      throw ex;
2✔
91
    }
92
  }
93

94
  @Nullable
95
  private MessageInterpolator getFallback() {
96
    for (String fallback : FALLBACKS) {
10✔
97
      try {
98
        return getFallback(fallback);
×
99
      }
100
      catch (Exception ex) {
1✔
101
        // Swallow and continue
102
      }
103
    }
1✔
104
    return null;
2✔
105
  }
106

107
  private MessageInterpolator getFallback(String fallback) {
108
    Class<?> interpolatorClass = ClassUtils.resolveClassName(fallback, null);
×
109
    Object interpolator = BeanUtils.newInstance(interpolatorClass);
×
110
    return (MessageInterpolator) interpolator;
×
111
  }
112

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