Class ParameterizedMessage
- All Implemented Interfaces:
Serializable
,Message
,StringBuilderFormattable
This class was originally written for Lilith by Joern Huxhorn where it is licensed under the LGPL. It has been relicensed here with his permission providing that this attribution remain.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Object[]
private static final int
static final String
Separator for error messages.static final String
Prefix for errors.static final String
Separator for errors.static final String
Suffix for errors.private String
private static final int
private int[]
private String
static final String
Prefix for recursion.static final String
Suffix for recursion.private static final long
private static ThreadLocal
<StringBuilder> private Throwable
private int
-
Constructor Summary
ConstructorsConstructorDescriptionParameterizedMessage
(String messagePattern, Object arg) Constructor with a pattern and a single parameter.ParameterizedMessage
(String messagePattern, Object... arguments) Constructs a ParameterizedMessage which contains the arguments converted to String as well as an optional Throwable.ParameterizedMessage
(String messagePattern, Object[] arguments, Throwable throwable) Creates a parameterized message.ParameterizedMessage
(String messagePattern, Object arg0, Object arg1) Constructor with a pattern and two parameters.ParameterizedMessage
(String messagePattern, String[] arguments, Throwable throwable) Deprecated.Use constructor ParameterizedMessage(String, Object[], Throwable) instead -
Method Summary
Modifier and TypeMethodDescriptionstatic int
countArgumentPlaceholders
(String messagePattern) Counts the number of unescaped placeholders in the given messagePattern.static String
This method performs a deep toString of the given Object.boolean
static String
Replace placeholders in the given messagePattern with arguments.void
formatTo
(StringBuilder buffer) Writes a text representation of this object into the specifiedStringBuilder
, ideally without allocating temporary objects.Returns the message pattern.Returns the formatted message.Object[]
Returns the message parameters.private static StringBuilder
Returns the Throwable that was given as the last argument, if any.int
hashCode()
static String
identityToString
(Object obj) This method returns the same as if Object.toString() would not have been overridden in obj.private void
private void
initThrowable
(Object[] params, int usedParams) toString()
-
Field Details
-
DEFAULT_STRING_BUILDER_SIZE
private static final int DEFAULT_STRING_BUILDER_SIZE- See Also:
-
RECURSION_PREFIX
Prefix for recursion.- See Also:
-
RECURSION_SUFFIX
Suffix for recursion.- See Also:
-
ERROR_PREFIX
Prefix for errors.- See Also:
-
ERROR_SEPARATOR
Separator for errors.- See Also:
-
ERROR_MSG_SEPARATOR
Separator for error messages.- See Also:
-
ERROR_SUFFIX
Suffix for errors.- See Also:
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
HASHVAL
private static final int HASHVAL- See Also:
-
threadLocalStringBuilder
-
messagePattern
-
argArray
-
formattedMessage
-
throwable
-
indices
private int[] indices -
usedCount
private int usedCount
-
-
Constructor Details
-
ParameterizedMessage
@Deprecated public ParameterizedMessage(String messagePattern, String[] arguments, Throwable throwable) Deprecated.Use constructor ParameterizedMessage(String, Object[], Throwable) insteadCreates a parameterized message.- Parameters:
messagePattern
- The message "format" string. This will be a String containing "{}" placeholders where parameters should be substituted.arguments
- The arguments for substitution.throwable
- A Throwable.
-
ParameterizedMessage
Creates a parameterized message.- Parameters:
messagePattern
- The message "format" string. This will be a String containing "{}" placeholders where parameters should be substituted.arguments
- The arguments for substitution.throwable
- A Throwable.
-
ParameterizedMessage
Constructs a ParameterizedMessage which contains the arguments converted to String as well as an optional Throwable.If the last argument is a Throwable and is NOT used up by a placeholder in the message pattern it is returned in
getThrowable()
and won't be contained in the created String[]. If it is used upgetThrowable()
will return null even if the last argument was a Throwable!- Parameters:
messagePattern
- the message pattern that to be checked for placeholders.arguments
- the argument array to be converted.
-
ParameterizedMessage
Constructor with a pattern and a single parameter.- Parameters:
messagePattern
- The message pattern.arg
- The parameter.
-
ParameterizedMessage
Constructor with a pattern and two parameters.- Parameters:
messagePattern
- The message pattern.arg0
- The first parameter.arg1
- The second parameter.
-
-
Method Details
-
init
-
initThrowable
-
getFormat
Returns the message pattern. -
getParameters
Returns the message parameters.- Specified by:
getParameters
in interfaceMessage
- Returns:
- the message parameters.
-
getThrowable
Returns the Throwable that was given as the last argument, if any. It will not survive serialization. The Throwable exists as part of the message primarily so that it can be extracted from the end of the list of parameters and then be added to the LogEvent. As such, the Throwable in the event should not be used once the LogEvent has been constructed.- Specified by:
getThrowable
in interfaceMessage
- Returns:
- the Throwable, if any.
-
getFormattedMessage
Returns the formatted message.- Specified by:
getFormattedMessage
in interfaceMessage
- Returns:
- the formatted message.
-
getThreadLocalStringBuilder
-
formatTo
Description copied from interface:StringBuilderFormattable
Writes a text representation of this object into the specifiedStringBuilder
, ideally without allocating temporary objects.- Specified by:
formatTo
in interfaceStringBuilderFormattable
- Parameters:
buffer
- the StringBuilder to write into
-
format
Replace placeholders in the given messagePattern with arguments.- Parameters:
messagePattern
- the message pattern containing placeholders.arguments
- the arguments to be used to replace placeholders.- Returns:
- the formatted message.
-
equals
-
hashCode
public int hashCode() -
countArgumentPlaceholders
Counts the number of unescaped placeholders in the given messagePattern.- Parameters:
messagePattern
- the message pattern to be analyzed.- Returns:
- the number of unescaped placeholders.
-
deepToString
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
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
-