Java beans

Beside Scala case classes, Chimney supports transformation of Java beans.

Reading from Java beans

Chimney supports automatic field renaming for classes that follow Java beans naming convention. Let’s assume the following classes.

class MyBean(private var id: Long,
             private var name: String,
             private var flag: Boolean) {
    def getId: Long = id
    def getName: String = name
    def isFlag: Boolean = flag

case class MyCaseClass(id: Long, name: String, flag: Boolean)

The conversion works only if you explicitly enable it with .enableBeanGetters operation.

new MyBean(1L, "beanie", true)
//  MyCaseClass(1L, "beanie", true)


Chimney matches accessor methods solely based on name and return type, and has no way of ensuring that a method named similarly to a getter is idempotent and does not actually perform side effects in its body.

Writing to Java beans

Dual to reading, Chimney supports transforming types into Java beans.

Chimney considers as bean a class that:

  • primary constructor is public and parameter-less

  • contains at least one, single-parameter setter method that returns Unit

Chimney will then require data sources for all such setters.

class MyBean {
  private var id: Long = _
  private var name: String = _
  private var flag: Boolean = _

  def getId: Long = id
  def setId(id: Long): Unit = { = id }

  def getName: String = name
  def setName(name: String): Unit = { = name }

  def isFlag: Boolean = flag
  def setFlag(flag: Boolean): Unit = { this.flag = flag }

The conversion works if you explicitly enable it with .enableBeanSetters.

val obj = MyCaseClass(10L, "beanie", true)
val bean = obj

Chimney generates code equivalent to:

val bean = new MyBean

Current limitations

Currently it’s not possible to override or provide values for missing setters.