[JAVA] Shape Scala case class for easy viewing

In the comments, I was told that there is a library called PPrint that formats the case class so that it is easy to see. http://www.lihaoyi.com/PPrint/

The following is the method using commons-lang3, but since the above library is easy to use, I think that you should use PPrint unless there are special circumstances.


I wanted to format the contents of the Scala case class and display it. I will share the formatting method because there was no hit even if I searched for the method with "case class pretty print" or "scala formatting output".

Plastic surgery method

Because Scala case class is compiled into Java Bean equivalent class Format using ToStringBuilder included in Java commons-lang3.

commons-lang3: https://mvnrepository.com/artifact/org.apache.commons/commons-lang3

Objects to display

The following case class values are formatted and displayed.

code


case class Outer(title: String, note: Seq[Inner])
case class Inner(no: Int, memo: String)

val obj = Outer("title", Inner(1, "Iroha") :: Inner(2, "Nihohe")  :: Inner(3, "Dust") :: Nil)
println(obj)

console


Outer(title,List(Inner(1,Iroha), Inner(2,Nihohe), Inner(3,Dust)))

Format and display up to the first layer of the layered class

You can format the value of bean format object by using reflectionToString of ToStringBuilder.

code


val text1 = ToStringBuilder.reflectionToString(obj, ToStringStyle.MULTI_LINE_STYLE)
println(text1)

console


Outer@1df5f8ae[
  title=title
  note=List(Inner(1,Iroha), Inner(2,Nihohe), Inner(3,Dust))
]

In addition to the above formats, ToStringStyle also displays only the value and formats it into JSON format.

ToStringStyle: https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/ToStringStyle.html

Hierarchical case classes are also recursively formatted and displayed

By using MultilineRecursiveToStringStyle, it is possible to recursively format the contents of the field of case class type.

code


val text2 = ToStringBuilder.reflectionToString(obj, new MultilineRecursiveToStringStyle())
println(text2)

console


Outer@7c49cd0c[
  title=title,
  note=scala.collection.immutable.$colon$colon@772294c7[
    head=Inner@8acd465[
      no=1,
      memo=Iroha
    ],
    tl=scala.collection.immutable.$colon$colon@7d64ef3e[
      head=Inner@1e03c8f9[
        no=2,
        memo=Nihohe
      ],
      tl=scala.collection.immutable.$colon$colon@10f69849[
        head=Inner@7e9769ce[
          no=3,
          memo=Dust
        ],
        tl=scala.collection.immutable.Nil$@24d0643d[
          
        ]
      ]
    ]
  ]
]

Sample code

PrettyPrintApp.scala


import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.commons.lang3.builder.MultilineRecursiveToStringStyle;

object PrettyPrintApp extends App {

  val obj = Outer("title", Inner(1, "Iroha") :: Inner(2, "Nihohe")  :: Inner(3, "Dust") :: Nil)

  println("--------------------------------------------------------------------------------")
  
  println("obj: ")
  println(obj)

  println("--------------------------------------------------------------------------------")

  println("ToStringStyle.MULTI_LINE_STYLE: ")
  val text1 = ToStringBuilder.reflectionToString(obj, ToStringStyle.MULTI_LINE_STYLE)
  println(text1)
  
  println("--------------------------------------------------------------------------------")
  
  println("MultilineRecursiveToStringStyle: ")
  val text2 = ToStringBuilder.reflectionToString(obj, new MultilineRecursiveToStringStyle())
  println(text2)
  
  println("--------------------------------------------------------------------------------")

}


case class Outer(title: String, note: Seq[Inner])
case class Inner(no: Int, memo: String)


Recommended Posts

Shape Scala case class for easy viewing
I touched Scala ~ [Class] ~