• 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/context/support/MessageSourceAccessor.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.support;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.util.Locale;
23

24
import infra.context.MessageSource;
25
import infra.context.MessageSourceResolvable;
26
import infra.context.NoSuchMessageException;
27
import infra.core.i18n.LocaleContextHolder;
28

29
/**
30
 * Helper class for easy access to messages from a MessageSource,
31
 * providing various overloaded getMessage methods.
32
 *
33
 * <p>Available from ApplicationObjectSupport, but also reusable
34
 * as a standalone helper to delegate to in application objects.
35
 *
36
 * @author Juergen Hoeller
37
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
38
 * @see ApplicationObjectSupport#getMessageSourceAccessor
39
 * @since 4.0
40
 */
41
public class MessageSourceAccessor {
42

43
  private final MessageSource messageSource;
44

45
  @Nullable
46
  private final Locale defaultLocale;
47

48
  /**
49
   * Create a new MessageSourceAccessor, using LocaleContextHolder's locale
50
   * as default locale.
51
   *
52
   * @param messageSource the MessageSource to wrap
53
   * @see LocaleContextHolder#getLocale()
54
   */
55
  public MessageSourceAccessor(MessageSource messageSource) {
2✔
56
    this.messageSource = messageSource;
3✔
57
    this.defaultLocale = null;
3✔
58
  }
1✔
59

60
  /**
61
   * Create a new MessageSourceAccessor, using the given default locale.
62
   *
63
   * @param messageSource the MessageSource to wrap
64
   * @param defaultLocale the default locale to use for message access
65
   */
66
  public MessageSourceAccessor(MessageSource messageSource, @Nullable Locale defaultLocale) {
2✔
67
    this.messageSource = messageSource;
3✔
68
    this.defaultLocale = defaultLocale;
3✔
69
  }
1✔
70

71
  /**
72
   * Return the default locale to use if no explicit locale has been given.
73
   * <p>The default implementation returns the default locale passed into the
74
   * corresponding constructor, or LocaleContextHolder's locale as fallback.
75
   * Can be overridden in subclasses.
76
   *
77
   * @see #MessageSourceAccessor(infra.context.MessageSource, java.util.Locale)
78
   * @see LocaleContextHolder#getLocale()
79
   */
80
  protected Locale getDefaultLocale() {
81
    return (this.defaultLocale != null ? this.defaultLocale : LocaleContextHolder.getLocale());
8✔
82
  }
83

84
  /**
85
   * Retrieve the message for the given code and the default Locale.
86
   *
87
   * @param code the code of the message
88
   * @param defaultMessage the String to return if the lookup fails
89
   * @return the message
90
   */
91
  public String getMessage(String code, String defaultMessage) {
92
    String msg = this.messageSource.getMessage(code, null, defaultMessage, getDefaultLocale());
9✔
93
    return (msg != null ? msg : "");
6✔
94
  }
95

96
  /**
97
   * Retrieve the message for the given code and the given Locale.
98
   *
99
   * @param code the code of the message
100
   * @param defaultMessage the String to return if the lookup fails
101
   * @param locale the Locale in which to do lookup
102
   * @return the message
103
   */
104
  public String getMessage(String code, String defaultMessage, Locale locale) {
105
    String msg = this.messageSource.getMessage(code, null, defaultMessage, locale);
×
106
    return (msg != null ? msg : "");
×
107
  }
108

109
  /**
110
   * Retrieve the message for the given code and the default Locale.
111
   *
112
   * @param code the code of the message
113
   * @param args arguments for the message, or {@code null} if none
114
   * @param defaultMessage the String to return if the lookup fails
115
   * @return the message
116
   */
117
  public String getMessage(String code, Object @Nullable [] args, String defaultMessage) {
118
    String msg = this.messageSource.getMessage(code, args, defaultMessage, getDefaultLocale());
9✔
119
    return (msg != null ? msg : "");
5!
120
  }
121

122
  /**
123
   * Retrieve the message for the given code and the given Locale.
124
   *
125
   * @param code the code of the message
126
   * @param args arguments for the message, or {@code null} if none
127
   * @param defaultMessage the String to return if the lookup fails
128
   * @param locale the Locale in which to do lookup
129
   * @return the message
130
   */
131
  public String getMessage(String code, Object @Nullable [] args, String defaultMessage, Locale locale) {
132
    String msg = this.messageSource.getMessage(code, args, defaultMessage, locale);
8✔
133
    return (msg != null ? msg : "");
5!
134
  }
135

136
  /**
137
   * Retrieve the message for the given code and the default Locale.
138
   *
139
   * @param code the code of the message
140
   * @return the message
141
   * @throws infra.context.NoSuchMessageException if not found
142
   */
143
  public String getMessage(String code) throws NoSuchMessageException {
144
    return this.messageSource.getMessage(code, null, getDefaultLocale());
8✔
145
  }
146

147
  /**
148
   * Retrieve the message for the given code and the given Locale.
149
   *
150
   * @param code the code of the message
151
   * @param locale the Locale in which to do lookup
152
   * @return the message
153
   * @throws infra.context.NoSuchMessageException if not found
154
   */
155
  public String getMessage(String code, Locale locale) throws NoSuchMessageException {
156
    return this.messageSource.getMessage(code, null, locale);
7✔
157
  }
158

159
  /**
160
   * Retrieve the message for the given code and the default Locale.
161
   *
162
   * @param code the code of the message
163
   * @param args arguments for the message, or {@code null} if none
164
   * @return the message
165
   * @throws infra.context.NoSuchMessageException if not found
166
   */
167
  public String getMessage(String code, Object @Nullable [] args) throws NoSuchMessageException {
168
    return this.messageSource.getMessage(code, args, getDefaultLocale());
8✔
169
  }
170

171
  /**
172
   * Retrieve the message for the given code and the given Locale.
173
   *
174
   * @param code the code of the message
175
   * @param args arguments for the message, or {@code null} if none
176
   * @param locale the Locale in which to do lookup
177
   * @return the message
178
   * @throws infra.context.NoSuchMessageException if not found
179
   */
180
  public String getMessage(String code, Object @Nullable [] args, Locale locale) throws NoSuchMessageException {
181
    return this.messageSource.getMessage(code, args, locale);
7✔
182
  }
183

184
  /**
185
   * Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance)
186
   * in the default Locale.
187
   *
188
   * @param resolvable the MessageSourceResolvable
189
   * @return the message
190
   * @throws infra.context.NoSuchMessageException if not found
191
   */
192
  public String getMessage(MessageSourceResolvable resolvable) throws NoSuchMessageException {
193
    return this.messageSource.getMessage(resolvable, getDefaultLocale());
7✔
194
  }
195

196
  /**
197
   * Retrieve the given MessageSourceResolvable (e.g. an ObjectError instance)
198
   * in the given Locale.
199
   *
200
   * @param resolvable the MessageSourceResolvable
201
   * @param locale the Locale in which to do lookup
202
   * @return the message
203
   * @throws infra.context.NoSuchMessageException if not found
204
   */
205
  public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
206
    return this.messageSource.getMessage(resolvable, locale);
6✔
207
  }
208

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