Class ParameterFormatter
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final charprivate static final char(package private) static final StringSeparator for error messages.(package private) static final StringPrefix for errors.(package private) static final StringSeparator for errors.(package private) static final StringSuffix for errors.private static final char(package private) static final StringPrefix for recursion.(package private) static final StringSuffix for recursion.private static final ThreadLocal<SimpleDateFormat> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static voidappendArray(Object o, StringBuilder str, Set<Object> dejaVu, Class<?> oClass) private static voidappendCollection(Object o, StringBuilder str, Set<Object> dejaVu) Specialized handler forCollections.private static booleanappendDate(Object o, StringBuilder str) private static voidappendMap(Object o, StringBuilder str, Set<Object> dejaVu) Specialized handler forMaps.private static voidappendPotentiallyRecursiveValue(Object o, StringBuilder str, Set<Object> dejaVu) private static booleanappendSpecialTypes(Object o, StringBuilder str) cloneDejaVu(Set<Object> dejaVu) (package private) static intcountArgumentPlaceholders(String messagePattern) Counts the number of unescaped placeholders in the given messagePattern.(package private) static intcountArgumentPlaceholders2(String messagePattern, int[] indices) Counts the number of unescaped placeholders in the given messagePattern.(package private) static intcountArgumentPlaceholders3(char[] messagePattern, int length, int[] indices) Counts the number of unescaped placeholders in the given messagePattern.(package private) static StringThis method performs a deep toString of the given Object.(package private) static StringReplace placeholders in the given messagePattern with arguments.(package private) static voidformatMessage(StringBuilder buffer, String messagePattern, Object[] arguments, int argCount) Replace placeholders in the given messagePattern with arguments.(package private) static voidformatMessage2(StringBuilder buffer, String messagePattern, Object[] arguments, int argCount, int[] indices) Replace placeholders in the given messagePattern with arguments.(package private) static voidformatMessage3(StringBuilder buffer, char[] messagePattern, int patternLength, Object[] arguments, int argCount, int[] indices) Replace placeholders in the given messagePattern with arguments.getOrCreateDejaVu(Set<Object> dejaVu) private static voidhandleErrorInObjectToString(Object o, StringBuilder str, Throwable t) private static voidhandleLastChar(StringBuilder buffer, int escapeCounter, char curChar) Processes the last unprocessed character and returns the resulting position in the result char array.private static voidhandleLiteralChar(StringBuilder buffer, int escapeCounter, char curChar) Processes a literal char (neither an '\' escape char nor a "{}" delimiter pair) and returns the resulting position.private static voidhandleRemainingCharIfAny(String messagePattern, int len, StringBuilder buffer, int escapeCounter, int i) Detects whether the message pattern has been fully processed or if an unprocessed character remains and processes it if necessary, returning the resulting position in the result char array.(package private) static StringidentityToString(Object obj) This method returns the same as if Object.toString() would not have been overridden in obj.private static booleanisDelimPair(char curChar, String messagePattern, int curCharIndex) Returnstrueif the specified char and the char atcurCharIndex + 1in the specified message pattern together form a "{}" delimiter pair, returnsfalseotherwise.private static booleanReturnstrueif the specified object is an array, a Map or a Collection.private static booleanisOdd(int number) Returnstrueif the specified parameter is odd.(package private) static voidrecursiveDeepToString(Object o, StringBuilder str) This method performs a deeptoString()of the givenObject.private static voidrecursiveDeepToString(Object o, StringBuilder str, Set<Object> dejaVu) This method performs a deeptoString()of the givenObject.private static voidtryObjectToString(Object o, StringBuilder str) private static voidwriteArgOrDelimPair(Object[] arguments, int argCount, int currentArgument, StringBuilder buffer) Appends the argument at the specified argument index (or, if no such argument exists, the "{}" delimiter pair) to the specified result char array at the specified position and returns the resulting position.private static voidwriteDelimPair(StringBuilder buffer) Writes "{}" to the specified result array at the specified position and returns the resulting position.private static voidwriteEscapedEscapeChars(int escapeCounter, StringBuilder buffer) Writes a '\' char to the specified result array (starting at the specified position) for each pair of '\' escape chars encountered in the message format and returns the resulting position.private static voidwriteUnescapedEscapeChars(int escapeCounter, StringBuilder buffer) Writes the specified number of '\' chars to the specified result array (starting at the specified position) and returns the resulting position.
-
Field Details
-
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:
-
DELIM_START
private static final char DELIM_START- See Also:
-
DELIM_STOP
private static final char DELIM_STOP- See Also:
-
ESCAPE_CHAR
private static final char ESCAPE_CHAR- See Also:
-
SIMPLE_DATE_FORMAT_REF
-
-
Constructor Details
-
ParameterFormatter
private ParameterFormatter()
-
-
Method Details
-
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.
-
countArgumentPlaceholders2
Counts the number of unescaped placeholders in the given messagePattern.- Parameters:
messagePattern- the message pattern to be analyzed.- Returns:
- the number of unescaped placeholders.
-
countArgumentPlaceholders3
static int countArgumentPlaceholders3(char[] messagePattern, int length, int[] indices) Counts the number of unescaped placeholders in the given messagePattern.- Parameters:
messagePattern- the message pattern to be analyzed.- Returns:
- the number of unescaped placeholders.
-
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.
-
formatMessage2
static void formatMessage2(StringBuilder buffer, String messagePattern, Object[] arguments, int argCount, int[] indices) Replace placeholders in the given messagePattern with arguments.- Parameters:
buffer- the buffer to write the formatted message intomessagePattern- the message pattern containing placeholders.arguments- the arguments to be used to replace placeholders.
-
formatMessage3
static void formatMessage3(StringBuilder buffer, char[] messagePattern, int patternLength, Object[] arguments, int argCount, int[] indices) Replace placeholders in the given messagePattern with arguments.- Parameters:
buffer- the buffer to write the formatted message intomessagePattern- the message pattern containing placeholders.arguments- the arguments to be used to replace placeholders.
-
formatMessage
static void formatMessage(StringBuilder buffer, String messagePattern, Object[] arguments, int argCount) Replace placeholders in the given messagePattern with arguments.- Parameters:
buffer- the buffer to write the formatted message intomessagePattern- the message pattern containing placeholders.arguments- the arguments to be used to replace placeholders.
-
isDelimPair
Returnstrueif the specified char and the char atcurCharIndex + 1in the specified message pattern together form a "{}" delimiter pair, returnsfalseotherwise. -
handleRemainingCharIfAny
private static void handleRemainingCharIfAny(String messagePattern, int len, StringBuilder buffer, int escapeCounter, int i) Detects whether the message pattern has been fully processed or if an unprocessed character remains and processes it if necessary, returning the resulting position in the result char array. -
handleLastChar
Processes the last unprocessed character and returns the resulting position in the result char array. -
handleLiteralChar
Processes a literal char (neither an '\' escape char nor a "{}" delimiter pair) and returns the resulting position. -
writeDelimPair
Writes "{}" to the specified result array at the specified position and returns the resulting position. -
isOdd
private static boolean isOdd(int number) Returnstrueif the specified parameter is odd. -
writeEscapedEscapeChars
Writes a '\' char to the specified result array (starting at the specified position) for each pair of '\' escape chars encountered in the message format and returns the resulting position. -
writeUnescapedEscapeChars
Writes the specified number of '\' chars to the specified result array (starting at the specified position) and returns the resulting position. -
writeArgOrDelimPair
private static void writeArgOrDelimPair(Object[] arguments, int argCount, int currentArgument, StringBuilder buffer) Appends the argument at the specified argument index (or, if no such argument exists, the "{}" delimiter pair) to the specified result char array at the specified position and returns the resulting position. -
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.
-
recursiveDeepToString
This method performs a deeptoString()of the givenObject.Primitive arrays are converted using their respective
Arrays.toString()methods, while special handling is implemented for container types, i.e.Object[],MapandCollection, because those could contain themselves.It should be noted that neither
AbstractMap.toString()norAbstractCollection.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 respectivetoString()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-debugStackOverflowError.- Parameters:
o- theObjectto convert into aStringstr- theStringBuilderthatowill be appended to
-
recursiveDeepToString
This method performs a deeptoString()of the givenObject.Primitive arrays are converted using their respective
Arrays.toString()methods, while special handling is implemented for container types, i.e.Object[],MapandCollection, because those could contain themselves.dejaVuis used in case of those container types to prevent an endless recursion.It should be noted that neither
AbstractMap.toString()norAbstractCollection.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 respectivetoString()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-debugStackOverflowError.- Parameters:
o- theObjectto convert into aStringstr- theStringBuilderthatowill be appended todejaVu- a set of container objects directly or transitively containingo
-
appendSpecialTypes
-
appendDate
-
isMaybeRecursive
Returnstrueif the specified object is an array, a Map or a Collection. -
appendPotentiallyRecursiveValue
private static void appendPotentiallyRecursiveValue(Object o, StringBuilder str, Set<Object> dejaVu) -
appendArray
-
appendMap
Specialized handler forMaps. -
appendCollection
Specialized handler forCollections. -
getOrCreateDejaVu
-
createDejaVu
-
cloneDejaVu
-
tryObjectToString
-
handleErrorInObjectToString
-
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
Objectdoes 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()
-