Make MessageSource locale parameter nullable
Backport Bot / build (push) Has been cancelled Details
Build and Deploy Snapshot / Build and Deploy Snapshot (push) Has been cancelled Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:false version:17], map[id:ubuntu-latest name:Linux]) (push) Has been cancelled Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:21], map[id:ubuntu-latest name:Linux]) (push) Has been cancelled Details
CI / ${{ matrix.os.name}} | Java ${{ matrix.java.version}} (map[toolchain:true version:23], map[id:ubuntu-latest name:Linux]) (push) Has been cancelled Details
Deploy Docs / Dispatch docs deployment (push) Has been cancelled Details
Build and Deploy Snapshot / Verify (push) Has been cancelled Details

Closes gh-35230
This commit is contained in:
Sébastien Deleuze 2025-07-22 12:04:24 +02:00
parent 445da24631
commit 5e338ef1b8
7 changed files with 47 additions and 26 deletions

View File

@ -55,7 +55,7 @@ public interface MessageSource {
* @see java.text.MessageFormat
*/
@Nullable
String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale);
String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, @Nullable Locale locale);
/**
* Try to resolve the message. Treat as an error if the message can't be found.
@ -71,7 +71,7 @@ public interface MessageSource {
* @see #getMessage(MessageSourceResolvable, Locale)
* @see java.text.MessageFormat
*/
String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException;
String getMessage(String code, @Nullable Object[] args, @Nullable Locale locale) throws NoSuchMessageException;
/**
* Try to resolve the message using all the attributes contained within the
@ -91,6 +91,6 @@ public interface MessageSource {
* @see MessageSourceResolvable#getDefaultMessage()
* @see java.text.MessageFormat
*/
String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException;
String getMessage(MessageSourceResolvable resolvable, @Nullable Locale locale) throws NoSuchMessageException;
}

View File

@ -1502,17 +1502,17 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
@Override
@Nullable
public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, @Nullable Locale locale) {
return getMessageSource().getMessage(code, args, defaultMessage, locale);
}
@Override
public String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException {
public String getMessage(String code, @Nullable Object[] args, @Nullable Locale locale) throws NoSuchMessageException {
return getMessageSource().getMessage(code, args, locale);
}
@Override
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
public String getMessage(MessageSourceResolvable resolvable, @Nullable Locale locale) throws NoSuchMessageException {
return getMessageSource().getMessage(resolvable, locale);
}

View File

@ -138,7 +138,7 @@ public abstract class AbstractMessageSource extends MessageSourceSupport impleme
@Override
@Nullable
public final String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
public final String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, @Nullable Locale locale) {
String msg = getMessageInternal(code, args, locale);
if (msg != null) {
return msg;
@ -150,7 +150,7 @@ public abstract class AbstractMessageSource extends MessageSourceSupport impleme
}
@Override
public final String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException {
public final String getMessage(String code, @Nullable Object[] args, @Nullable Locale locale) throws NoSuchMessageException {
String msg = getMessageInternal(code, args, locale);
if (msg != null) {
return msg;
@ -159,11 +159,16 @@ public abstract class AbstractMessageSource extends MessageSourceSupport impleme
if (fallback != null) {
return fallback;
}
throw new NoSuchMessageException(code, locale);
if (locale == null ) {
throw new NoSuchMessageException(code);
}
else {
throw new NoSuchMessageException(code, locale);
}
}
@Override
public final String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
public final String getMessage(MessageSourceResolvable resolvable, @Nullable Locale locale) throws NoSuchMessageException {
String[] codes = resolvable.getCodes();
if (codes != null) {
for (String code : codes) {
@ -177,7 +182,13 @@ public abstract class AbstractMessageSource extends MessageSourceSupport impleme
if (defaultMessage != null) {
return defaultMessage;
}
throw new NoSuchMessageException(!ObjectUtils.isEmpty(codes) ? codes[codes.length - 1] : "", locale);
String code = !ObjectUtils.isEmpty(codes) ? codes[codes.length - 1] : "";
if (locale == null ) {
throw new NoSuchMessageException(code);
}
else {
throw new NoSuchMessageException(code, locale);
}
}
@ -284,7 +295,7 @@ public abstract class AbstractMessageSource extends MessageSourceSupport impleme
* @see #getDefaultMessage(String)
*/
@Nullable
protected String getDefaultMessage(MessageSourceResolvable resolvable, Locale locale) {
protected String getDefaultMessage(MessageSourceResolvable resolvable, @Nullable Locale locale) {
String defaultMessage = resolvable.getDefaultMessage();
String[] codes = resolvable.getCodes();
if (defaultMessage != null) {
@ -331,7 +342,7 @@ public abstract class AbstractMessageSource extends MessageSourceSupport impleme
* @return an array of arguments with any MessageSourceResolvables resolved
*/
@Override
protected Object[] resolveArguments(@Nullable Object[] args, Locale locale) {
protected Object[] resolveArguments(@Nullable Object[] args, @Nullable Locale locale) {
if (ObjectUtils.isEmpty(args)) {
return super.resolveArguments(args, locale);
}

View File

@ -55,7 +55,7 @@ public class DelegatingMessageSource extends MessageSourceSupport implements Hie
@Override
@Nullable
public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, @Nullable Locale locale) {
if (this.parentMessageSource != null) {
return this.parentMessageSource.getMessage(code, args, defaultMessage, locale);
}
@ -68,17 +68,22 @@ public class DelegatingMessageSource extends MessageSourceSupport implements Hie
}
@Override
public String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException {
public String getMessage(String code, @Nullable Object[] args, @Nullable Locale locale) throws NoSuchMessageException {
if (this.parentMessageSource != null) {
return this.parentMessageSource.getMessage(code, args, locale);
}
else {
throw new NoSuchMessageException(code, locale);
if (locale == null) {
throw new NoSuchMessageException(code);
}
else {
throw new NoSuchMessageException(code, locale);
}
}
}
@Override
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
public String getMessage(MessageSourceResolvable resolvable, @Nullable Locale locale) throws NoSuchMessageException {
if (this.parentMessageSource != null) {
return this.parentMessageSource.getMessage(resolvable, locale);
}
@ -88,7 +93,12 @@ public class DelegatingMessageSource extends MessageSourceSupport implements Hie
}
String[] codes = resolvable.getCodes();
String code = (codes != null && codes.length > 0 ? codes[0] : "");
throw new NoSuchMessageException(code, locale);
if (locale == null) {
throw new NoSuchMessageException(code);
}
else {
throw new NoSuchMessageException(code, locale);
}
}
}

View File

@ -98,7 +98,7 @@ public abstract class MessageSourceSupport {
* @return the rendered default message (with resolved arguments)
* @see #formatMessage(String, Object[], java.util.Locale)
*/
protected String renderDefaultMessage(String defaultMessage, @Nullable Object[] args, Locale locale) {
protected String renderDefaultMessage(String defaultMessage, @Nullable Object[] args, @Nullable Locale locale) {
return formatMessage(defaultMessage, args, locale);
}
@ -112,7 +112,7 @@ public abstract class MessageSourceSupport {
* @param locale the Locale used for formatting
* @return the formatted message (with resolved arguments)
*/
protected String formatMessage(String msg, @Nullable Object[] args, Locale locale) {
protected String formatMessage(String msg, @Nullable Object[] args, @Nullable Locale locale) {
if (!isAlwaysUseMessageFormat() && ObjectUtils.isEmpty(args)) {
return msg;
}
@ -146,7 +146,7 @@ public abstract class MessageSourceSupport {
* @param locale the Locale to create a {@code MessageFormat} for
* @return the {@code MessageFormat} instance
*/
protected MessageFormat createMessageFormat(String msg, Locale locale) {
protected MessageFormat createMessageFormat(String msg, @Nullable Locale locale) {
return new MessageFormat(msg, locale);
}
@ -158,7 +158,7 @@ public abstract class MessageSourceSupport {
* @param locale the Locale to resolve against
* @return the resolved argument array
*/
protected Object[] resolveArguments(@Nullable Object[] args, Locale locale) {
protected Object[] resolveArguments(@Nullable Object[] args, @Nullable Locale locale) {
return (args != null ? args : new Object[0]);
}

View File

@ -357,17 +357,17 @@ class StubWebApplicationContext implements WebApplicationContext {
@Override
@Nullable
public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, @Nullable Locale locale) {
return this.messageSource.getMessage(code, args, defaultMessage, locale);
}
@Override
public String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException {
public String getMessage(String code, @Nullable Object[] args, @Nullable Locale locale) throws NoSuchMessageException {
return this.messageSource.getMessage(code, args, locale);
}
@Override
public String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
public String getMessage(MessageSourceResolvable resolvable, @Nullable Locale locale) throws NoSuchMessageException {
return this.messageSource.getMessage(resolvable, locale);
}

View File

@ -116,7 +116,7 @@ public abstract class BindErrorUtils {
@Override
@Nullable
protected String getDefaultMessage(MessageSourceResolvable resolvable, Locale locale) {
protected String getDefaultMessage(MessageSourceResolvable resolvable, @Nullable Locale locale) {
String message = super.getDefaultMessage(resolvable, locale);
return (resolvable instanceof FieldError error ? error.getField() + ": " + message : message);
}