Polishing

This commit is contained in:
Juergen Hoeller
2017-01-12 21:18:01 +01:00
parent 02d727fd7c
commit e19dff179e
9 changed files with 73 additions and 78 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -42,11 +42,11 @@ public interface MessageSource {
* @param code the code to lookup up, such as 'calculator.noRateSet'. Users of
* this class are encouraged to base message names on the relevant fully
* qualified class name, thus avoiding conflict and ensuring maximum clarity.
* @param args array of arguments that will be filled in for params within
* @param args an array of arguments that will be filled in for params within
* the message (params look like "{0}", "{1,date}", "{2,time}" within a message),
* or {@code null} if none.
* @param defaultMessage String to return if the lookup fails
* @param locale the Locale in which to do the lookup
* @param defaultMessage a default message to return if the lookup fails
* @param locale the locale in which to do the lookup
* @return the resolved message if the lookup was successful;
* otherwise the default message passed as a parameter
* @see java.text.MessageFormat
@@ -56,10 +56,10 @@ public interface MessageSource {
/**
* Try to resolve the message. Treat as an error if the message can't be found.
* @param code the code to lookup up, such as 'calculator.noRateSet'
* @param args Array of arguments that will be filled in for params within
* @param args an array of arguments that will be filled in for params within
* the message (params look like "{0}", "{1,date}", "{2,time}" within a message),
* or {@code null} if none.
* @param locale the Locale in which to do the lookup
* @param locale the locale in which to do the lookup
* @return the resolved message
* @throws NoSuchMessageException if the message wasn't found
* @see java.text.MessageFormat
@@ -71,9 +71,9 @@ public interface MessageSource {
* {@code MessageSourceResolvable} argument that was passed in.
* <p>NOTE: We must throw a {@code NoSuchMessageException} on this method
* since at the time of calling this method we aren't able to determine if the
* {@code defaultMessage} property of the resolvable is null or not.
* @param resolvable value object storing attributes required to properly resolve a message
* @param locale the Locale in which to do the lookup
* {@code defaultMessage} property of the resolvable is {@code null} or not.
* @param resolvable the value object storing attributes required to resolve a message
* @param locale the locale in which to do the lookup
* @return the resolved message
* @throws NoSuchMessageException if the message wasn't found
* @see java.text.MessageFormat

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@ package org.springframework.context;
* @see org.springframework.validation.ObjectError
* @see org.springframework.validation.FieldError
*/
@FunctionalInterface
public interface MessageSourceResolvable {
/**
@@ -38,16 +39,26 @@ public interface MessageSourceResolvable {
/**
* Return the array of arguments to be used to resolve this message.
* <p>The default implementation simply returns {@code null}.
* @return an array of objects to be used as parameters to replace
* placeholders within the message text
* @see java.text.MessageFormat
*/
Object[] getArguments();
default Object[] getArguments() {
return null;
}
/**
* Return the default message to be used to resolve this message.
* <p>The default implementation simply returns {@code null}.
* Note that the default message may be identical to the primary
* message code ({@link #getCodes()}), which effectively enforces
* {@link org.springframework.context.support.AbstractMessageSource#setUseCodeAsDefaultMessage}
* for this particular message.
* @return the default message, or {@code null} if no default
*/
String getDefaultMessage();
default String getDefaultMessage() {
return null;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -52,13 +52,12 @@ public abstract class MessageSourceSupport {
* Used for passed-in default messages. MessageFormats for resolved
* codes are cached on a specific basis in subclasses.
*/
private final Map<String, Map<Locale, MessageFormat>> messageFormatsPerMessage =
new HashMap<>();
private final Map<String, Map<Locale, MessageFormat>> messageFormatsPerMessage = new HashMap<>();
/**
* Set whether to always apply the MessageFormat rules, parsing even
* messages without arguments.
* Set whether to always apply the {@code MessageFormat} rules,
* parsing even messages without arguments.
* <p>Default is "false": Messages without arguments are by default
* returned as-is, without parsing them through MessageFormat.
* Set this to "true" to enforce MessageFormat for all messages,
@@ -112,7 +111,7 @@ public abstract class MessageSourceSupport {
* @return the formatted message (with resolved arguments)
*/
protected String formatMessage(String msg, Object[] args, Locale locale) {
if (msg == null || (!this.alwaysUseMessageFormat && ObjectUtils.isEmpty(args))) {
if (msg == null || (!isAlwaysUseMessageFormat() && ObjectUtils.isEmpty(args))) {
return msg;
}
MessageFormat messageFormat = null;
@@ -130,12 +129,12 @@ public abstract class MessageSourceSupport {
messageFormat = createMessageFormat(msg, locale);
}
catch (IllegalArgumentException ex) {
// invalid message format - probably not intended for formatting,
// rather using a message structure with no arguments involved
if (this.alwaysUseMessageFormat) {
// Invalid message format - probably not intended for formatting,
// rather using a message structure with no arguments involved...
if (isAlwaysUseMessageFormat()) {
throw ex;
}
// silently proceed with raw message if format not enforced
// Silently proceed with raw message if format not enforced...
messageFormat = INVALID_MESSAGE_FORMAT;
}
messageFormatsPerLocale.put(locale, messageFormat);