diff --git a/src/java/org/apache/commons/lang/builder/ToStringBuilder.java b/src/java/org/apache/commons/lang/builder/ToStringBuilder.java index ee2abd43a..b55e1fd9a 100644 --- a/src/java/org/apache/commons/lang/builder/ToStringBuilder.java +++ b/src/java/org/apache/commons/lang/builder/ToStringBuilder.java @@ -94,26 +94,50 @@ public class ToStringBuilder { /** * The default style of output to use. */ - private static ToStringStyle defaultStyle = ToStringStyle.DEFAULT_STYLE; + private static volatile ToStringStyle defaultStyle = ToStringStyle.DEFAULT_STYLE; //---------------------------------------------------------------------------- /** *

Gets the default ToStringStyle to use.

- * - *

This could allow the ToStringStyle to be - * controlled for an entire application with one call.

- * - *

This might be used to have a verbose - * ToStringStyle during development and a compact - * ToStringStyle in production.

* - * @return the default ToStringStyle + *

This method gets a singleton default value, typically for the whole JVM. + * Changing this default should generally only be done during application startup. + * It is recommended to pass a ToStringStyle to the constructor instead + * of using this global default.

+ * + *

This method is thread-safe, as a volatile + * + *

One reason for changing the default could be to have a verbose style during + * development and a compact style in production.

+ * + * @return the default ToStringStyle, never null */ public static ToStringStyle getDefaultStyle() { return defaultStyle; } + /** + *

Sets the default ToStringStyle to use.

+ * + *

This method sets a singleton default value, typically for the whole JVM. + * Changing this default should generally only be done during application startup. + * It is recommended to pass a ToStringStyle to the constructor instead + * of changing this global default.

+ * + *

This method is thread-safe, as a volatile + * + * @param style the default ToStringStyle + * @throws IllegalArgumentException if the style is null + */ + public static void setDefaultStyle(ToStringStyle style) { + if (style == null) { + throw new IllegalArgumentException("The style must not be null"); + } + defaultStyle = style; + } + + //---------------------------------------------------------------------------- /** *

Forwards to ReflectionToStringBuilder.

* @@ -169,18 +193,7 @@ public static String reflectionToString( return ReflectionToStringBuilder.toString(object, style, outputTransients, false, reflectUpToClass); } - /** - *

Sets the default ToStringStyle to use.

- * - * @param style the default ToStringStyle - * @throws IllegalArgumentException if the style is null - */ - public static void setDefaultStyle(ToStringStyle style) { - if (style == null) { - throw new IllegalArgumentException("The style must not be null"); - } - defaultStyle = style; - } + //---------------------------------------------------------------------------- /** * Current toString buffer. @@ -208,7 +221,7 @@ public static void setDefaultStyle(ToStringStyle style) { * null */ public ToStringBuilder(Object object) { - this(object, getDefaultStyle(), null); + this(object, null, null); } /**