Skip to content

Commit

Permalink
Deprecate in-traint bindInstance[X] (#317)
Browse files Browse the repository at this point in the history
* Remove bindInstance
* Distinguish provider and factory binding method signatures
* Provider binding should return singleton by default
* Refine singleton check
* Report injectee to LifeCycleManager
* Wrap injectee with Injectee class
  • Loading branch information
xerial committed Nov 25, 2018
1 parent fa78beb commit 1f15aef
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 281 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,6 @@ private[wvlet] object AirframeMacros {
}"""
}

def bindSingleton(session: c.Tree, t: c.Type): c.Tree = {
q"""{
val session = ${session}
${newSingletonBinder(t)}(session)
}"""
}

def findSession: c.Tree = {
q"""wvlet.airframe.Session.findSession(this)"""
}
Expand All @@ -96,16 +89,16 @@ private[wvlet] object AirframeMacros {
}
}

def newSingletonBinder(t: c.Type): c.Tree = {
def createNewInstanceOf(t: c.Type): c.Tree = {
if (shouldGenerateTrait(t)) {
q"""{
session : wvlet.airframe.Session =>
session.getOrElseSingleton[$t](${surfaceOf(t)},
session.createNewInstanceOf(${surfaceOf(t)},
(new $t with wvlet.airframe.SessionHolder { def airframeSession = session}).asInstanceOf[$t]
)
}"""
} else {
q"""{ session : wvlet.airframe.Session => session.get[$t](${surfaceOf(t)}) }"""
q"""{ session : wvlet.airframe.Session => session.createNewInstanceOf[$t](${surfaceOf(t)}) }"""
}
}

Expand Down Expand Up @@ -517,38 +510,37 @@ private[wvlet] object AirframeMacros {
"""
}

def bindInstanceImpl[A: c.WeakTypeTag](c: sm.Context): c.Tree = {
def bindImpl[A: c.WeakTypeTag](c: sm.Context): c.Tree = {
val t = implicitly[c.WeakTypeTag[A]].tpe
val h = new BindHelper[c.type](c)
h.bind(h.findSession, t)
}

def bindInstance0Impl[A: c.WeakTypeTag](c: sm.Context)(factory: c.Tree): c.Tree = {
def bind0Impl[A: c.WeakTypeTag](c: sm.Context)(provider: c.Tree): c.Tree = {
import c.universe._
val t = implicitly[c.WeakTypeTag[A]].tpe
val h = new BindHelper[c.type](c)
q"""{
val session = ${h.findSession}
session.getOrElse(${h.surfaceOf(t)}, $factory)
session.getOrElse(${h.surfaceOf(t)}, $provider)
}
"""
}

def bindInstance1Impl[A: c.WeakTypeTag, D1: c.WeakTypeTag](c: sm.Context)(factory: c.Tree): c.Tree = {
def bind1Impl[A: c.WeakTypeTag, D1: c.WeakTypeTag](c: sm.Context)(provider: c.Tree): c.Tree = {
import c.universe._
val h = new BindHelper[c.type](c)
val t = implicitly[c.WeakTypeTag[A]].tpe
val d1 = implicitly[c.WeakTypeTag[D1]].tpe
val dep1 = h.newInstanceBinder(d1)
q"""{
val session = ${h.findSession}
session.getOrElse(${h.surfaceOf(t)}, $factory($dep1(session)))
session.getOrElse(${h.surfaceOf(t)}, $provider($dep1(session)))
}
"""
}

def bindInstance2Impl[A: c.WeakTypeTag, D1: c.WeakTypeTag, D2: c.WeakTypeTag](c: sm.Context)(
factory: c.Tree): c.Tree = {
def bind2Impl[A: c.WeakTypeTag, D1: c.WeakTypeTag, D2: c.WeakTypeTag](c: sm.Context)(provider: c.Tree): c.Tree = {
import c.universe._
val h = new BindHelper[c.type](c)
val t = implicitly[c.WeakTypeTag[A]].tpe
Expand All @@ -558,13 +550,13 @@ private[wvlet] object AirframeMacros {
val dep2 = h.newInstanceBinder(d2)
q"""{
val session = ${h.findSession}
session.getOrElse(${h.surfaceOf(t)}, $factory($dep1(session), $dep2(session)))
session.getOrElse(${h.surfaceOf(t)}, $provider($dep1(session), $dep2(session)))
}
"""
}

def bindInstance3Impl[A: c.WeakTypeTag, D1: c.WeakTypeTag, D2: c.WeakTypeTag, D3: c.WeakTypeTag](c: sm.Context)(
factory: c.Tree): c.Tree = {
def bind3Impl[A: c.WeakTypeTag, D1: c.WeakTypeTag, D2: c.WeakTypeTag, D3: c.WeakTypeTag](c: sm.Context)(
provider: c.Tree): c.Tree = {
import c.universe._
val h = new BindHelper[c.type](c)
val t = implicitly[c.WeakTypeTag[A]].tpe
Expand All @@ -576,13 +568,13 @@ private[wvlet] object AirframeMacros {
val dep3 = h.newInstanceBinder(d3)
q"""{
val session = ${h.findSession}
session.getOrElse(${h.surfaceOf(t)}, $factory($dep1(session),$dep2(session),$dep3(session)))
session.getOrElse(${h.surfaceOf(t)}, $provider($dep1(session),$dep2(session),$dep3(session)))
}
"""
}

def bindInstance4Impl[A: c.WeakTypeTag, D1: c.WeakTypeTag, D2: c.WeakTypeTag, D3: c.WeakTypeTag, D4: c.WeakTypeTag](
c: sm.Context)(factory: c.Tree): c.Tree = {
def bind4Impl[A: c.WeakTypeTag, D1: c.WeakTypeTag, D2: c.WeakTypeTag, D3: c.WeakTypeTag, D4: c.WeakTypeTag](
c: sm.Context)(provider: c.Tree): c.Tree = {
import c.universe._
val h = new BindHelper[c.type](c)
val t = implicitly[c.WeakTypeTag[A]].tpe
Expand All @@ -597,18 +589,18 @@ private[wvlet] object AirframeMacros {
q"""{
val session = ${h.findSession}
session.getOrElse(${h.surfaceOf(t)},
$factory($dep1(session),$dep2(session),$dep3(session),$dep4(session))
$provider($dep1(session),$dep2(session),$dep3(session),$dep4(session))
)
}
"""
}

def bindInstance5Impl[A: c.WeakTypeTag,
D1: c.WeakTypeTag,
D2: c.WeakTypeTag,
D3: c.WeakTypeTag,
D4: c.WeakTypeTag,
D5: c.WeakTypeTag](c: sm.Context)(factory: c.Tree): c.Tree = {
def bind5Impl[A: c.WeakTypeTag,
D1: c.WeakTypeTag,
D2: c.WeakTypeTag,
D3: c.WeakTypeTag,
D4: c.WeakTypeTag,
D5: c.WeakTypeTag](c: sm.Context)(provider: c.Tree): c.Tree = {
import c.universe._
val h = new BindHelper[c.type](c)
val t = implicitly[c.WeakTypeTag[A]].tpe
Expand All @@ -625,7 +617,7 @@ private[wvlet] object AirframeMacros {
q"""{
val session = ${h.findSession}
session.getOrElse(${h.surfaceOf(t)},
$factory($dep1(session),$dep2(session),$dep3(session),$dep4(session),$dep5(session))
$provider($dep1(session),$dep2(session),$dep3(session),$dep4(session),$dep5(session))
)
}
"""
Expand All @@ -638,9 +630,9 @@ private[wvlet] object AirframeMacros {
val i1 = t.typeArgs(0) // I1
val a = t.typeArgs(1) // A
val h = new BindHelper[c.type](c)
q"""{ x: ${i1} =>
val session = ${h.findSession}.newSharedChildSession(wvlet.airframe.newDesign.bind(${h.surfaceOf(i1)}).toLazyInstance(x))
${h.newInstanceBinder(a)}(session)
q"""{ i1: ${i1} =>
val session = ${h.findSession}.newSharedChildSession(wvlet.airframe.newDesign.bind(${h.surfaceOf(i1)}).toLazyInstance(i1))
${h.createNewInstanceOf(a)}(session)
}
"""
}
Expand All @@ -659,7 +651,7 @@ private[wvlet] object AirframeMacros {
.bind(${h.surfaceOf(i1)}).toLazyInstance(i1)
.bind(${h.surfaceOf(i2)}).toLazyInstance(i2)
)
${h.newInstanceBinder(a)}(session)
${h.createNewInstanceOf(a)}(session)
}
"""
}
Expand All @@ -680,7 +672,7 @@ private[wvlet] object AirframeMacros {
.bind(${h.surfaceOf(i2)}).toLazyInstance(i2)
.bind(${h.surfaceOf(i3)}).toLazyInstance(i3)
)
${h.newInstanceBinder(a)}(session)
${h.createNewInstanceOf(a)}(session)
}
"""
}
Expand All @@ -703,7 +695,7 @@ private[wvlet] object AirframeMacros {
.bind(${h.surfaceOf(i3)}).toLazyInstance(i3)
.bind(${h.surfaceOf(i4)}).toLazyInstance(i4)
)
${h.newInstanceBinder(a)}(session)
${h.createNewInstanceOf(a)}(session)
}
"""
}
Expand All @@ -728,121 +720,7 @@ private[wvlet] object AirframeMacros {
.bind(${h.surfaceOf(i4)}).toLazyInstance(i4)
.bind(${h.surfaceOf(i5)}).toLazyInstance(i5)
)
${h.newInstanceBinder(a)}(session)
}
"""
}

def bindSingletonImpl[A: c.WeakTypeTag](c: sm.Context): c.Tree = {
val h = new BindHelper[c.type](c)
val t = implicitly[c.WeakTypeTag[A]].tpe
h.bindSingleton(h.findSession, t)
}

def bind0SingletonImpl[A: c.WeakTypeTag](c: sm.Context)(factory: c.Tree): c.Tree = {
import c.universe._
val t = implicitly[c.WeakTypeTag[A]].tpe
val h = new BindHelper[c.type](c)
q"""{
val session = ${h.findSession}
session.getOrElseSingleton(${h.surfaceOf(t)}, $factory)
}
"""
}

def bind1SingletonImpl[A: c.WeakTypeTag, D1: c.WeakTypeTag](c: sm.Context)(factory: c.Tree): c.Tree = {
import c.universe._
val h = new BindHelper[c.type](c)
val t = implicitly[c.WeakTypeTag[A]].tpe
val d1 = implicitly[c.WeakTypeTag[D1]].tpe
val dep1 = h.newInstanceBinder(d1)
q"""{
val session = ${h.findSession}
session.getOrElseSingleton(${h.surfaceOf(t)}, $factory($dep1(session)))
}
"""
}

def bind2SingletonImpl[A: c.WeakTypeTag, D1: c.WeakTypeTag, D2: c.WeakTypeTag](c: sm.Context)(
factory: c.Tree): c.Tree = {
import c.universe._
val h = new BindHelper[c.type](c)
val t = implicitly[c.WeakTypeTag[A]].tpe
val d1 = implicitly[c.WeakTypeTag[D1]].tpe
val d2 = implicitly[c.WeakTypeTag[D2]].tpe
val dep1 = h.newInstanceBinder(d1)
val dep2 = h.newInstanceBinder(d2)
q"""{
val session = ${h.findSession}
session.getOrElseSingleton(${h.surfaceOf(t)}, $factory($dep1(session), $dep2(session)))
}
"""
}

def bind3SingletonImpl[A: c.WeakTypeTag, D1: c.WeakTypeTag, D2: c.WeakTypeTag, D3: c.WeakTypeTag](c: sm.Context)(
factory: c.Tree): c.Tree = {
import c.universe._
val h = new BindHelper[c.type](c)
val t = implicitly[c.WeakTypeTag[A]].tpe
val d1 = implicitly[c.WeakTypeTag[D1]].tpe
val d2 = implicitly[c.WeakTypeTag[D2]].tpe
val d3 = implicitly[c.WeakTypeTag[D3]].tpe
val dep1 = h.newInstanceBinder(d1)
val dep2 = h.newInstanceBinder(d2)
val dep3 = h.newInstanceBinder(d3)
q"""{
val session = ${h.findSession}
session.getOrElseSingleton(${h.surfaceOf(t)}, $factory($dep1(session),$dep2(session),$dep3(session)))
}
"""
}

def bind4SingletonImpl[A: c.WeakTypeTag, D1: c.WeakTypeTag, D2: c.WeakTypeTag, D3: c.WeakTypeTag, D4: c.WeakTypeTag](
c: sm.Context)(factory: c.Tree): c.Tree = {
import c.universe._
val h = new BindHelper[c.type](c)
val t = implicitly[c.WeakTypeTag[A]].tpe
val d1 = implicitly[c.WeakTypeTag[D1]].tpe
val d2 = implicitly[c.WeakTypeTag[D2]].tpe
val d3 = implicitly[c.WeakTypeTag[D3]].tpe
val d4 = implicitly[c.WeakTypeTag[D4]].tpe
val dep1 = h.newInstanceBinder(d1)
val dep2 = h.newInstanceBinder(d2)
val dep3 = h.newInstanceBinder(d3)
val dep4 = h.newInstanceBinder(d4)
q"""{
val session = ${h.findSession}
session.getOrElseSingleton(${h.surfaceOf(t)},
$factory($dep1(session),$dep2(session),$dep3(session),$dep4(session))
)
}
"""
}

def bind5SingletonImpl[A: c.WeakTypeTag,
D1: c.WeakTypeTag,
D2: c.WeakTypeTag,
D3: c.WeakTypeTag,
D4: c.WeakTypeTag,
D5: c.WeakTypeTag](c: sm.Context)(factory: c.Tree): c.Tree = {
import c.universe._
val h = new BindHelper[c.type](c)
val t = implicitly[c.WeakTypeTag[A]].tpe
val d1 = implicitly[c.WeakTypeTag[D1]].tpe
val d2 = implicitly[c.WeakTypeTag[D2]].tpe
val d3 = implicitly[c.WeakTypeTag[D3]].tpe
val d4 = implicitly[c.WeakTypeTag[D4]].tpe
val d5 = implicitly[c.WeakTypeTag[D5]].tpe
val dep1 = h.newInstanceBinder(d1)
val dep2 = h.newInstanceBinder(d2)
val dep3 = h.newInstanceBinder(d3)
val dep4 = h.newInstanceBinder(d4)
val dep5 = h.newInstanceBinder(d5)
q"""{
val session = ${h.findSession}
session.getOrElseSingleton(${h.surfaceOf(t)},
$factory($dep1(session),$dep2(session),$dep3(session),$dep4(session),$dep5(session))
)
${h.createNewInstanceOf(a)}(session)
}
"""
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import wvlet.airframe.surface.Surface

import scala.reflect.ClassTag

class MethodCallHook[A](val surface: Surface, obj: A, method: jl.reflect.Method) extends LifeCycleHook {
class MethodCallHook(val injectee: Injectee, method: jl.reflect.Method) extends LifeCycleHook {
override def toString: String = s"MethodCallHook for [${surface}]"
def execute: Unit = {
method.invoke(obj)
method.invoke(injectee.injectee)
}
}

Expand All @@ -45,11 +45,13 @@ object JSR250LifeCycleExecutor extends LifeCycleEventHandler with LogSupport {

override def onInit(lifeCycleManager: LifeCycleManager, t: Surface, injectee: AnyRef): Unit = {
for (m <- t.rawType.getDeclaredMethods; a <- findAnnotation[PostConstruct](m.getDeclaredAnnotations)) {
lifeCycleManager.addInitHook(new MethodCallHook(t, injectee, m))
trace(s"[${t}] Found @PostConstruct annotation")
lifeCycleManager.addInitHook(new MethodCallHook(new Injectee(t, injectee), m))
}

for (m <- t.rawType.getDeclaredMethods; a <- findAnnotation[PreDestroy](m.getDeclaredAnnotations)) {
lifeCycleManager.addShutdownHook(new MethodCallHook(t, injectee, m))
trace(s"[${t}] Found @PreDestroy annotation")
lifeCycleManager.addShutdownHook(new MethodCallHook(new Injectee(t, injectee), m))
}
}
}
Loading

0 comments on commit 1f15aef

Please sign in to comment.