|
| 1 | +package com.softwaremill.macwire.pekkosupport.internals |
| 2 | + |
| 3 | +import org.apache.pekko.actor.{ActorRef, ActorRefFactory, IndirectActorProducer, Props} |
| 4 | +import com.softwaremill.macwire.internals.{ConstructorCrimper, Logger, DependencyResolver} |
| 5 | + |
| 6 | +import scala.reflect.macros.blackbox |
| 7 | + |
| 8 | +private[macwire] final class Crimper[C <: blackbox.Context, T: C#WeakTypeTag](val c: C, log: Logger) { |
| 9 | + import c.universe._ |
| 10 | + lazy val dependencyResolver = DependencyResolver.throwErrorOnResolutionFailure[c.type, Type, Tree](c, log) |
| 11 | + lazy val cc = new ConstructorCrimper[c.type, T](c, new Logger)(implicitly[c.type#WeakTypeTag[T]]) |
| 12 | + |
| 13 | + lazy val targetType: Type = cc.targetType |
| 14 | + |
| 15 | + lazy val args: List[Tree] = cc |
| 16 | + .constructorArgsWithImplicitLookups(dependencyResolver) |
| 17 | + .getOrElse(c.abort(c.enclosingPosition, s"Cannot find a public constructor for [$targetType]")) |
| 18 | + .flatten |
| 19 | + |
| 20 | + lazy val propsTree = q"org.apache.pekko.actor.Props(classOf[$targetType], ..$args)" |
| 21 | + |
| 22 | + lazy val wireProps: c.Expr[Props] = log.withBlock(s"wireProps[$targetType]: at ${c.enclosingPosition}") { |
| 23 | + log("Generated code: " + showRaw(propsTree)) |
| 24 | + c.Expr[Props](propsTree) |
| 25 | + } |
| 26 | + |
| 27 | + lazy val actorRefFactoryTree: Tree = log.withBlock("Looking for ActorRefFactory") { |
| 28 | + val actorRefType = typeOf[ActorRefFactory] |
| 29 | + val tree = dependencyResolver.resolve(actorRefType.typeSymbol, actorRefType) |
| 30 | + log(s"Found ${showCode(tree)}") |
| 31 | + tree |
| 32 | + } |
| 33 | + |
| 34 | + lazy val wireAnonymousActor: c.Expr[ActorRef] = log.withBlock( |
| 35 | + s"Constructing ActorRef. Trying to find arguments for constructor of: [$targetType] at ${c.enclosingPosition}" |
| 36 | + ) { |
| 37 | + val tree = q"$actorRefFactoryTree.actorOf($propsTree)" |
| 38 | + log("Generated code: " + showRaw(tree)) |
| 39 | + c.Expr[ActorRef](tree) |
| 40 | + } |
| 41 | + |
| 42 | + def wireActor(name: c.Expr[String]): c.Expr[ActorRef] = |
| 43 | + log.withBlock(s"wireActor[$targetType]: at ${c.enclosingPosition}") { |
| 44 | + val tree = q"$actorRefFactoryTree.actorOf($propsTree, ${name.tree})" |
| 45 | + log("Generated code: " + showRaw(tree)) |
| 46 | + c.Expr[ActorRef](tree) |
| 47 | + } |
| 48 | + |
| 49 | + lazy val wirePropsWithProducer: c.Expr[Props] = weakTypeOf[T] match { |
| 50 | + case t if t <:< typeOf[IndirectActorProducer] => wireProps |
| 51 | + case _ => c.abort(c.enclosingPosition, s"wirePropsWith does not support the type: [$targetType]") |
| 52 | + } |
| 53 | + |
| 54 | + lazy val wireAnonymousActorWithProducer: c.Expr[ActorRef] = weakTypeOf[T] match { |
| 55 | + case t if t <:< typeOf[IndirectActorProducer] => wireAnonymousActor |
| 56 | + case _ => c.abort(c.enclosingPosition, s"wireAnonymousActorWith does not support the type: [$targetType]") |
| 57 | + } |
| 58 | + |
| 59 | + def wireActorWithProducer(name: c.Expr[String]): c.Expr[ActorRef] = weakTypeOf[T] match { |
| 60 | + case t if t <:< typeOf[IndirectActorProducer] => wireActor(name) |
| 61 | + case _ => c.abort(c.enclosingPosition, s"wireActorWith does not support the type: [$targetType]") |
| 62 | + } |
| 63 | + |
| 64 | + def wirePropsWithFactory(factory: c.Tree): c.Expr[Props] = |
| 65 | + log.withBlock(s"wireProps[$targetType]: at ${c.enclosingPosition}") { |
| 66 | + import com.softwaremill.macwire.MacwireMacros._ |
| 67 | + |
| 68 | + val funTree = wireWith_impl(c)(factory) |
| 69 | + val propsTree = q"org.apache.pekko.actor.Props($funTree)" |
| 70 | + log("Generated code: " + showRaw(propsTree)) |
| 71 | + c.Expr[Props](propsTree) |
| 72 | + } |
| 73 | + |
| 74 | + def wireAnonymousActorWithFactory(factory: c.Tree): c.Expr[ActorRef] = log.withBlock( |
| 75 | + s"Constructing ActorRef. Trying to find arguments for constructor of: [$targetType] at ${c.enclosingPosition}" |
| 76 | + ) { |
| 77 | + import com.softwaremill.macwire.MacwireMacros._ |
| 78 | + |
| 79 | + val funTree = wireWith_impl(c)(factory) |
| 80 | + val propsTree = q"org.apache.pekko.actor.Props($funTree)" |
| 81 | + val tree = q"$actorRefFactoryTree.actorOf($propsTree)" |
| 82 | + log("Generated code: " + showRaw(tree)) |
| 83 | + c.Expr[ActorRef](tree) |
| 84 | + } |
| 85 | + |
| 86 | + def wireActorWithFactory(factory: c.Tree)(name: c.Expr[String]): c.Expr[ActorRef] = |
| 87 | + log.withBlock(s"wireActorWithProducer[$targetType]: at ${c.enclosingPosition}") { |
| 88 | + import com.softwaremill.macwire.MacwireMacros._ |
| 89 | + |
| 90 | + val funTree = wireWith_impl(c)(factory) |
| 91 | + val propsTree = q"org.apache.pekko.actor.Props($funTree)" |
| 92 | + val tree = q"$actorRefFactoryTree.actorOf($propsTree, ${name.tree})" |
| 93 | + log("Generated code: " + showRaw(tree)) |
| 94 | + c.Expr[ActorRef](tree) |
| 95 | + } |
| 96 | +} |
0 commit comments