Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix annotation of classes with companion objects #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/main/scala/com/cmhteixeira/delegatemacro/Delegate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ object delegateMacro {
case _ => false
}

val annotateeClass: ClassDef = annottees.map(_.tree).toList match {
case (claz: ClassDef) :: Nil => claz
val (annotateeClass, rest) = annottees.map(_.tree).toList match {
case (claz: ClassDef) :: rest => (claz, rest)
case _ => c.abort(c.enclosingPosition, "Unexpected annottee. Only applicable to class definitions.")
}

Expand All @@ -79,6 +79,8 @@ object delegateMacro {
val superClassTypedTree = annotteeClassParents.toList match {
case head :: _ =>
c.typecheck(head, mode = c.TYPEmode)
case Nil =>
c.abort(c.enclosingPosition, "Annotated class should at least have one parent.")
}

val delegateeParamOpt = annotteeClassParams
Expand Down Expand Up @@ -132,11 +134,17 @@ object delegateMacro {
.map(_.map(_.name.toTermName))})"
}

val resTree = annotateeClass match {
val classTree = annotateeClass match {
case q"$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents { $self => ..$stats }" =>
q"$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents { $self => ..${stats.toList ::: interfaceMethods} }"
}

val resTree =
q"""
$classTree
..$rest
"""

c.info(
c.enclosingPosition,
"\n###### Expanded macro ######\n" + resTree.toString() + "\n###### Expanded macro ######\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,28 @@ class SimplestInterfaceSpec extends FlatSpec with Matchers {
s"FirstList(param1: 30, param2: Bar); SecondList(param1: Qux, param2: 35)"
}

"Delegation" should "work with companion objects" in {
class FooImpl extends Foo {
def bar(a1: Int, a2: String)(b1: String, b2: Int): String =
s"FirstList(param1: $a1, param2: $a2); SecondList(param1: $b1, param2: $b2)"

def baz(a: Int, b: String): String = "not-relevant"

override def barBaz(a: String): String = s"FooImpl=$a"
}

@Delegate
class MyFoo(delegate: Foo) extends Foo

object MyFoo {
val notRemoved: Boolean = true
}

new MyFoo(new FooImpl).barBaz("Monkey") shouldBe s"Default-Implementation-Monkey"
new MyFoo(new FooImpl).baz(30, "Baz") shouldBe "not-relevant"
new MyFoo(new FooImpl).bar(30, "Bar")("Qux", 35) shouldBe
s"FirstList(param1: 30, param2: Bar); SecondList(param1: Qux, param2: 35)"
MyFoo.notRemoved shouldBe true
}

}