Skip to content

filtering SCALAC.CASE

Evgeny Mandrikov edited this page Mar 1, 2019 · 5 revisions

This page discusses a not yet available feature!

Description

Scala allows case classes that generate boilerplate code such as toString, equals and hashCode.

Filtering

Methods from case-classes are completely removed.

Source Example

case class Foo(a: Int)(b: String)

Bytecode Pattern

The class Foo and the companion class Foo$ has synthetic methods

public boolean Foo.equals(java.lang.Object)
public java.lang.String Foo.toString()
public int Foo.hashCode()
public Foo Foo.copy(int,java.lang.String)
public java.lang.String Foo.productPrefix()
public int Foo.productArity()
public java.lang.Object Foo.productElement(int)
public scala.collection.Iterator Foo.productIterator()
public boolean Foo.canEqual(java.lang.Object)
public int Foo.copy$default$1()

public final java.lang.String Foo$.toString()
private java.lang.Object Foo$.readResolve()
public Foo Foo$.apply(int,java.lang.String)
public scala.Option Foo$.unapply(Foo)

@retronym: These can be recognized by the fact that they are emitted on a single line, which is the same as the one of the constructors.

@timezra: The auto-generated methods that have the same line number as a constructor can be removed using the SCALAC.MIXIN filter. The other auto-generated methods will be either "curried()" or "tupled()" (neither of which is emitted with a source line number) or will follow the pattern "(\w|$)+$default$\d+". Currently the filter in use in the jacoco-scala-maven-plugin (https://github.com/timezra/jacoco-scala-maven-plugin) can be used as a reference.

⚠️ @Godin: line numbers are not totally reliable, because their generation can be disabled, which will lead to inconsistency between reports created with and without debug information. Case classes can have multiple constructors:

case class C(i: Int) {
  def this(s: String) = this(s.length)
}