Class PrettyPrinter


  • public class PrettyPrinter
    extends java.lang.Object
    Minimalistic on-the-fly Java source code pretty-printer.

    The input must conform to some limitations, mainly:

    • The "prettifier" provides only indentation. The input must already be split correctly into lines according to the standard Java source code style.
    • Each block (like the body of an if or while statement) must be enclosed in brackets.

    Example of a valid input:

     if (something) {
     switch(a) {
     case 1:
     break;
     }
     } else {
     }
     

    This produces the output:

     if (something) {
         switch(a) {
         case 1:
             break;
         }
     } else {
     }
     
    • Constructor Summary

      Constructors 
      Constructor Description
      PrettyPrinter​(java.io.Writer output)
      Constructs the pretty printer.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void write​(java.lang.String text)
      Writes text to the output stream as-is.
      void writeLine​(java.lang.String text)
      Indents the line as necessary and writes it to the output stream.
      • Methods inherited from class java.lang.Object

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

      • PrettyPrinter

        public PrettyPrinter​(java.io.Writer output)
        Constructs the pretty printer.
        Parameters:
        output - the output to write the source code to
    • Method Detail

      • writeLine

        public void writeLine​(java.lang.String text)
        Indents the line as necessary and writes it to the output stream.
        Parameters:
        text - the line of text
      • write

        public void write​(java.lang.String text)
        Writes text to the output stream as-is.
        Parameters:
        text - text to write