Class ParameterizedMessage

  • All Implemented Interfaces:
    java.io.Serializable, Message, StringBuilderFormattable

    public class ParameterizedMessage
    extends java.lang.Object
    implements Message, StringBuilderFormattable
    A Message accepting argument placeholders in the formatting pattern.

    Only "{}" strings are treated as argument placeholders. Escaped (i.e., "\"-prefixed) or incomplete argument placeholders will be ignored. Examples of argument placeholders that will be discarded and rendered intact:

     { }
     foo\{}
     {bar
     {buzz}
     
    See Also:
    Serialized Form
    • Field Detail

      • RECURSION_PREFIX

        public static final java.lang.String RECURSION_PREFIX
        Prefix for recursion.
        See Also:
        Constant Field Values
      • RECURSION_SUFFIX

        public static final java.lang.String RECURSION_SUFFIX
        Suffix for recursion.
        See Also:
        Constant Field Values
      • ERROR_PREFIX

        public static final java.lang.String ERROR_PREFIX
        Prefix for errors.
        See Also:
        Constant Field Values
      • ERROR_SEPARATOR

        public static final java.lang.String ERROR_SEPARATOR
        Separator for errors.
        See Also:
        Constant Field Values
      • ERROR_MSG_SEPARATOR

        public static final java.lang.String ERROR_MSG_SEPARATOR
        Separator for error messages.
        See Also:
        Constant Field Values
      • ERROR_SUFFIX

        public static final java.lang.String ERROR_SUFFIX
        Suffix for errors.
        See Also:
        Constant Field Values
      • pattern

        private final java.lang.String pattern
      • args

        private transient java.lang.Object[] args
      • throwable

        private final transient java.lang.Throwable throwable
      • formattedMessage

        private java.lang.String formattedMessage
    • Constructor Detail

      • ParameterizedMessage

        @InlineMe(replacement="this(pattern, Arrays.stream(args).toArray(Object[]::new), throwable)",
                  imports="java.util.Arrays")
        @Deprecated
        public ParameterizedMessage​(java.lang.String pattern,
                                    java.lang.String[] args,
                                    java.lang.Throwable throwable)
        Deprecated.
        Constructs an instance.

        The Throwable associated with the message (and returned in getThrowable()) will be determined as follows:

        1. If a throwable argument is provided
        2. If the last argument is a Throwable and is not referred to by any placeholder in the pattern
        Parameters:
        pattern - a formatting pattern
        args - arguments to be formatted
        throwable - a Throwable
      • ParameterizedMessage

        public ParameterizedMessage​(java.lang.String pattern,
                                    java.lang.Object[] args,
                                    java.lang.Throwable throwable)
        Constructs an instance.

        The Throwable associated with the message (and returned in getThrowable()) will be determined as follows:

        1. If a throwable argument is provided
        2. If the last argument is a Throwable and is not referred to by any placeholder in the pattern
        Parameters:
        pattern - a formatting pattern
        args - arguments to be formatted
        throwable - a Throwable
      • ParameterizedMessage

        public ParameterizedMessage​(java.lang.String pattern,
                                    java.lang.Object... args)
        Constructor with a pattern and multiple arguments.

        If the last argument is a Throwable and is not referred to by any placeholder in the pattern, it is returned in getThrowable().

        Parameters:
        pattern - a formatting pattern
        args - arguments to be formatted
      • ParameterizedMessage

        public ParameterizedMessage​(java.lang.String pattern,
                                    java.lang.Object arg)
        Constructor with a pattern and a single argument.

        If the argument is a Throwable and is not referred to by any placeholder in the pattern, it is returned in getThrowable().

        Parameters:
        pattern - a formatting pattern
        arg - an argument
      • ParameterizedMessage

        public ParameterizedMessage​(java.lang.String pattern,
                                    java.lang.Object arg0,
                                    java.lang.Object arg1)
        Constructor with a pattern and two arguments.

        If the last argument is a Throwable and is not referred to by any placeholder in the pattern, it is returned in getThrowable() and won't be contained in the formatted message.

        Parameters:
        pattern - a formatting pattern
        arg0 - the first argument
        arg1 - the second argument
    • Method Detail

      • getFormat

        public java.lang.String getFormat()
        Description copied from interface: Message
        This method has unclear semantics and inconsistent implementations – its usage is strongly discouraged.
        Specified by:
        getFormat in interface Message
        Returns:
        the message formatting pattern
      • getParameters

        public java.lang.Object[] getParameters()
        Description copied from interface: Message
        Gets parameter values, if any.
        Specified by:
        getParameters in interface Message
        Returns:
        the message arguments
      • getThrowable

        public java.lang.Throwable getThrowable()
        The Throwable provided along with the message by one of the following means:
        1. explicitly in the constructor
        2. as the last message argument that is not referred to by any placeholder in the formatting pattern
        Specified by:
        getThrowable in interface Message
        Returns:
        the Throwable provided along with the message
      • getFormattedMessage

        public java.lang.String getFormattedMessage()
        Returns the formatted message.

        If possible, the result will be cached for subsequent invocations.

        Specified by:
        getFormattedMessage in interface Message
        Returns:
        the formatted message
      • formatTo

        public void formatTo​(java.lang.StringBuilder buffer)
        Description copied from interface: StringBuilderFormattable
        Writes a text representation of this object into the specified StringBuilder, ideally without allocating temporary objects.
        Specified by:
        formatTo in interface StringBuilderFormattable
        Parameters:
        buffer - the StringBuilder to write into
      • format

        public static java.lang.String format​(java.lang.String pattern,
                                              java.lang.Object[] args)
        Returns the formatted message.
        Parameters:
        pattern - a message pattern containing argument placeholders
        args - arguments to be used to replace placeholders
      • equals

        public boolean equals​(java.lang.Object object)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • countArgumentPlaceholders

        public static int countArgumentPlaceholders​(java.lang.String pattern)
        Returns the number of argument placeholders.
        Parameters:
        pattern - the message pattern to be analyzed
      • deepToString

        public static java.lang.String deepToString​(java.lang.Object o)
        This method performs a deep toString of the given Object. Primitive arrays are converted using their respective Arrays.toString methods while special handling is implemented for "container types", i.e. Object[], Map and Collection because those could contain themselves.

        It should be noted that neither AbstractMap.toString() nor AbstractCollection.toString() implement such a behavior. They only check if the container is directly contained in itself, but not if a contained container contains the original one. Because of that, Arrays.toString(Object[]) isn't safe either. Confusing? Just read the last paragraph again and check the respective toString() implementation.

        This means, in effect, that logging would produce a usable output even if an ordinary System.out.println(o) would produce a relatively hard-to-debug StackOverflowError.

        Parameters:
        o - The object.
        Returns:
        The String representation.
      • identityToString

        public static java.lang.String identityToString​(java.lang.Object obj)
        This method returns the same as if Object.toString() would not have been overridden in obj.

        Note that this isn't 100% secure as collisions can always happen with hash codes.

        Copied from Object.hashCode():

        As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.)
        Parameters:
        obj - the Object that is to be converted into an identity string.
        Returns:
        the identity string as also defined in Object.toString()
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • writeObject

        private void writeObject​(java.io.ObjectOutputStream out)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • readObject

        private void readObject​(java.io.ObjectInputStream in)
                         throws java.io.IOException,
                                java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException