Class StringWrapper


  • public final class StringWrapper
    extends java.lang.Object
    Wraps string literals that exceed the column limit.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  StringWrapper.Reflower  
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static com.google.common.base.CharMatcher STRING_CONCAT_DELIMITER  
      private static java.lang.reflect.Method STRIP_INDENT  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private StringWrapper()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.lang.String applyReplacements​(java.lang.String javaInput, com.google.common.collect.TreeRangeMap<java.lang.Integer,​java.lang.String> replacementMap)
      Applies replacements to the given string.
      private static java.util.List<com.sun.source.tree.Tree> flatten​(java.lang.String input, com.sun.tools.javac.tree.JCTree.JCCompilationUnit unit, com.sun.source.util.TreePath path, com.sun.source.util.TreePath parent, java.util.concurrent.atomic.AtomicBoolean firstInChain)
      Flattens the given binary expression tree, and extracts the subset that contains the given path and any adjacent nodes that are also string literals.
      private static int getEndPosition​(com.sun.tools.javac.tree.JCTree.JCCompilationUnit unit, com.sun.source.tree.Tree tree)  
      private static com.google.common.collect.TreeRangeMap<java.lang.Integer,​java.lang.String> getReflowReplacements​(int columnLimit, java.lang.String input)  
      private static int getStartPosition​(com.sun.source.tree.Tree tree)  
      private static @Nullable java.lang.reflect.Method getStripIndent()  
      (package private) static int hasEscapedNewlineAt​(java.lang.String input, int idx)  
      (package private) static int hasEscapedWhitespaceAt​(java.lang.String input, int idx)  
      private static boolean needWrapping​(int columnLimit, java.lang.String input)
      Returns true if any lines in the given Java source exceed the column limit, or contain a """ that could indicate a text block.
      private static boolean noComments​(java.lang.String input, com.sun.tools.javac.tree.JCTree.JCCompilationUnit unit, com.sun.source.tree.Tree one, com.sun.source.tree.Tree two)  
      private static com.sun.tools.javac.tree.JCTree.JCCompilationUnit parse​(java.lang.String source, boolean allowStringFolding)
      Parses the given Java source.
      private static java.lang.String reflow​(java.lang.String separator, int columnLimit, int startColumn, int trailing, com.google.common.collect.ImmutableList<java.lang.String> components, boolean first0)
      Reflows the given source text, trying to split on word boundaries.
      private static com.google.common.collect.ImmutableList<java.lang.String> stringComponents​(java.lang.String input, com.sun.tools.javac.tree.JCTree.JCCompilationUnit unit, java.util.List<com.sun.source.tree.Tree> flat)
      Returns the source text of the given string literal trees, excluding the leading and trailing double-quotes and the `+` operator.
      private static java.lang.String stripIndent​(java.lang.String input)  
      (package private) static java.lang.String wrap​(int columnLimit, java.lang.String input, Formatter formatter)
      Reflows string literals in the given Java source code that extend past the given column limit.
      static java.lang.String wrap​(java.lang.String input, Formatter formatter)
      Reflows long string literals in the given Java source code.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • STRIP_INDENT

        private static final java.lang.reflect.Method STRIP_INDENT
      • STRING_CONCAT_DELIMITER

        public static final com.google.common.base.CharMatcher STRING_CONCAT_DELIMITER
    • Constructor Detail

      • StringWrapper

        private StringWrapper()
    • Method Detail

      • wrap

        static java.lang.String wrap​(int columnLimit,
                                     java.lang.String input,
                                     Formatter formatter)
                              throws FormatterException
        Reflows string literals in the given Java source code that extend past the given column limit.
        Throws:
        FormatterException
      • getReflowReplacements

        private static com.google.common.collect.TreeRangeMap<java.lang.Integer,​java.lang.String> getReflowReplacements​(int columnLimit,
                                                                                                                              java.lang.String input)
                                                                                                                       throws FormatterException
        Throws:
        FormatterException
      • getStripIndent

        private static @Nullable java.lang.reflect.Method getStripIndent()
      • stripIndent

        private static java.lang.String stripIndent​(java.lang.String input)
      • stringComponents

        private static com.google.common.collect.ImmutableList<java.lang.String> stringComponents​(java.lang.String input,
                                                                                                  com.sun.tools.javac.tree.JCTree.JCCompilationUnit unit,
                                                                                                  java.util.List<com.sun.source.tree.Tree> flat)
        Returns the source text of the given string literal trees, excluding the leading and trailing double-quotes and the `+` operator.
      • hasEscapedWhitespaceAt

        static int hasEscapedWhitespaceAt​(java.lang.String input,
                                          int idx)
      • hasEscapedNewlineAt

        static int hasEscapedNewlineAt​(java.lang.String input,
                                       int idx)
      • reflow

        private static java.lang.String reflow​(java.lang.String separator,
                                               int columnLimit,
                                               int startColumn,
                                               int trailing,
                                               com.google.common.collect.ImmutableList<java.lang.String> components,
                                               boolean first0)
        Reflows the given source text, trying to split on word boundaries.
        Parameters:
        separator - the line separator
        columnLimit - the number of columns to wrap at
        startColumn - the column position of the beginning of the original text
        trailing - extra space to leave after the last line, to accommodate a ; or )
        components - the text to reflow. This is a list of “words” of a single literal. Its first and last quotes have been stripped
        first0 - true if the text includes the beginning of its enclosing concat chain
      • flatten

        private static java.util.List<com.sun.source.tree.Tree> flatten​(java.lang.String input,
                                                                        com.sun.tools.javac.tree.JCTree.JCCompilationUnit unit,
                                                                        com.sun.source.util.TreePath path,
                                                                        com.sun.source.util.TreePath parent,
                                                                        java.util.concurrent.atomic.AtomicBoolean firstInChain)
        Flattens the given binary expression tree, and extracts the subset that contains the given path and any adjacent nodes that are also string literals.
      • noComments

        private static boolean noComments​(java.lang.String input,
                                          com.sun.tools.javac.tree.JCTree.JCCompilationUnit unit,
                                          com.sun.source.tree.Tree one,
                                          com.sun.source.tree.Tree two)
      • getEndPosition

        private static int getEndPosition​(com.sun.tools.javac.tree.JCTree.JCCompilationUnit unit,
                                          com.sun.source.tree.Tree tree)
      • getStartPosition

        private static int getStartPosition​(com.sun.source.tree.Tree tree)
      • needWrapping

        private static boolean needWrapping​(int columnLimit,
                                            java.lang.String input)
        Returns true if any lines in the given Java source exceed the column limit, or contain a """ that could indicate a text block.
      • parse

        private static com.sun.tools.javac.tree.JCTree.JCCompilationUnit parse​(java.lang.String source,
                                                                               boolean allowStringFolding)
                                                                        throws FormatterException
        Parses the given Java source.
        Throws:
        FormatterException
      • applyReplacements

        private static java.lang.String applyReplacements​(java.lang.String javaInput,
                                                          com.google.common.collect.TreeRangeMap<java.lang.Integer,​java.lang.String> replacementMap)
                                                   throws FormatterException
        Applies replacements to the given string.
        Throws:
        FormatterException