Class ParameterFormatter

java.lang.Object
org.apache.logging.log4j.message.ParameterFormatter

final class ParameterFormatter extends Object
Supports parameter formatting as used in ParameterizedMessage and ReusableParameterizedMessage.
  • Field Details

  • Constructor Details

    • ParameterFormatter

      private ParameterFormatter()
  • Method Details

    • analyzePattern

      static ParameterFormatter.MessagePatternAnalysis analyzePattern(String pattern, int argCount)
      Analyzes – finds argument placeholder (i.e., "{}") occurrences, etc. – the given message pattern.

      Only "{}" strings are treated as argument placeholders. Escaped or incomplete argument placeholders will be ignored. Some invalid argument placeholder examples:

      { }
      foo\{}
      {bar
      {buzz}
      
      Parameters:
      pattern - a message pattern to be analyzed
      argCount - The number of arguments to be formatted. For instance, for a parametrized message containing 7 placeholders in the pattern and 4 arguments for formatting, analysis will only need to store the index of the first 4 placeholder characters. A negative value indicates no limit.
      Returns:
      the analysis result
    • analyzePattern

      static void analyzePattern(String pattern, int argCount, ParameterFormatter.MessagePatternAnalysis analysis)
      Analyzes – finds argument placeholder (i.e., "{}") occurrences, etc. – the given message pattern.

      Only "{}" strings are treated as argument placeholders. Escaped or incomplete argument placeholders will be ignored. Some invalid argument placeholder examples:

      { }
      foo\{}
      {bar
      {buzz}
      
      Parameters:
      pattern - a message pattern to be analyzed
      argCount - The number of arguments to be formatted. For instance, for a parametrized message containing 7 placeholders in the pattern and 4 arguments for formatting, analysis will only need to store the index of the first 4 placeholder characters. A negative value indicates no limit.
      analysis - an object to store the results
    • format

      static String format(String pattern, Object[] args, int argCount)
      Format the given pattern using provided arguments.
      Parameters:
      pattern - a formatting pattern
      args - arguments to be formatted
      Returns:
      the formatted message
      Throws:
      IllegalArgumentException - on invalid input
    • formatMessage

      static void formatMessage(StringBuilder buffer, String pattern, Object[] args, int argCount, ParameterFormatter.MessagePatternAnalysis analysis)
      Format the given pattern using provided arguments into the buffer pointed.
      Parameters:
      buffer - a buffer the formatted output will be written to
      pattern - a formatting pattern
      args - arguments to be formatted
      Throws:
      IllegalArgumentException - on invalid input
    • isLastArgumentThrowable

      private static boolean isLastArgumentThrowable(Object[] args, int argCount)
    • formatMessageContainingNoEscapes

      private static void formatMessageContainingNoEscapes(StringBuilder buffer, String pattern, Object[] args, int argCount, ParameterFormatter.MessagePatternAnalysis analysis)
    • formatMessageContainingEscapes

      private static void formatMessageContainingEscapes(StringBuilder buffer, String pattern, Object[] args, int argCount, ParameterFormatter.MessagePatternAnalysis analysis)
    • copyMessagePatternContainingEscapes

      private static void copyMessagePatternContainingEscapes(StringBuilder buffer, String pattern, int startIndex, int endIndex)
    • deepToString

      static String deepToString(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.
    • recursiveDeepToString

      static void recursiveDeepToString(Object o, StringBuilder str)
      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 to convert into a String
      str - the StringBuilder that o will be appended to
    • recursiveDeepToString

      private static void recursiveDeepToString(Object o, StringBuilder str, Set<Object> dejaVu)
      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.

      dejaVu is used in case of those container types to prevent an endless recursion.

      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 to convert into a String
      str - the StringBuilder that o will be appended to
      dejaVu - a set of container objects directly or transitively containing o
    • appendSpecialTypes

      private static boolean appendSpecialTypes(Object o, StringBuilder str)
    • appendDate

      private static boolean appendDate(Object o, StringBuilder str)
    • isMaybeRecursive

      private static boolean isMaybeRecursive(Object o)
      Returns true if the specified object is an array, a Map or a Collection.
    • appendPotentiallyRecursiveValue

      private static void appendPotentiallyRecursiveValue(Object o, StringBuilder str, Set<Object> dejaVu)
    • appendArray

      private static void appendArray(Object o, StringBuilder str, Set<Object> dejaVu, Class<?> oClass)
    • appendMap

      private static void appendMap(Object o, StringBuilder str, Set<Object> dejaVu)
      Specialized handler for Maps.
    • appendCollection

      private static void appendCollection(Object o, StringBuilder str, Set<Object> dejaVu)
      Specialized handler for Collections.
    • getOrCreateDejaVu

      private static Set<Object> getOrCreateDejaVu(Set<Object> dejaVu)
    • createDejaVu

      private static Set<Object> createDejaVu()
    • cloneDejaVu

      private static Set<Object> cloneDejaVu(Set<Object> dejaVu)
    • tryObjectToString

      private static void tryObjectToString(Object o, StringBuilder str)
    • handleErrorInObjectToString

      private static void handleErrorInObjectToString(Object o, StringBuilder str, Throwable t)
    • identityToString

      static String identityToString(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()
    • appendArray

      private static void appendArray(byte[] a, StringBuilder str)
      See Also:
    • appendArray

      private static void appendArray(short[] a, StringBuilder str)
      See Also:
    • appendArray

      static void appendArray(int[] a, StringBuilder str)
      See Also:
    • appendArray

      private static void appendArray(long[] a, StringBuilder str)
      See Also:
    • appendArray

      private static void appendArray(float[] a, StringBuilder str)
      See Also:
    • appendArray

      private static void appendArray(double[] a, StringBuilder str)
      See Also:
    • appendArray

      private static void appendArray(boolean[] a, StringBuilder str)
      See Also:
    • appendArray

      private static void appendArray(char[] a, StringBuilder str)
      See Also: