Skip to content

Commit 907622e

Browse files
committed
Add macrosPekko and macrosPekkoTests
1 parent d19c15d commit 907622e

File tree

120 files changed

+4239
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+4239
-0
lines changed

build.sbt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ val tagging = "com.softwaremill.common" %% "tagging" % "2.3.1"
6565
val scalatest = "org.scalatest" %% "scalatest" % "3.2.9"
6666
val javassist = "org.javassist" % "javassist" % "3.29.2-GA"
6767
val akkaActor = "com.typesafe.akka" %% "akka-actor" % "2.6.20"
68+
val pekkoActor = "org.apache.pekko" %% "pekko-actor" % "1.0.1"
6869
val javaxInject = "javax.inject" % "javax.inject" % "1"
6970
val cats = "org.typelevel" %% "cats-core" % "2.10.0"
7071
val catsEffect = "org.typelevel" %% "cats-effect" % "3.5.1"
@@ -160,6 +161,14 @@ lazy val macrosAkka = projectMatrix
160161
.jvmPlatform(scalaVersions = scala2)
161162
.jsPlatform(scalaVersions = scala2)
162163

164+
lazy val macrosPekko = projectMatrix
165+
.in(file("macrosPekko"))
166+
.settings(commonSettings)
167+
.settings(libraryDependencies ++= Seq(pekkoActor % "provided"))
168+
.dependsOn(macros)
169+
.jvmPlatform(scalaVersions = scala2)
170+
.jsPlatform(scalaVersions = scala2)
171+
163172
lazy val macrosAkkaTests = projectMatrix
164173
.in(file("macrosAkkaTests"))
165174
.settings(
@@ -173,6 +182,19 @@ lazy val macrosAkkaTests = projectMatrix
173182
.dependsOn(macrosAkka, testUtil)
174183
.jvmPlatform(scalaVersions = scala2)
175184

185+
lazy val macrosPekkoTests = projectMatrix
186+
.in(file("macrosPekkoTests"))
187+
.settings(
188+
// Needed to avoid cryptic EOFException crashes in forked tests in Travis
189+
// example failure: https://travis-ci.org/adamw/macwire/builds/191382122
190+
// see: https://github.com/travis-ci/travis-ci/issues/3775
191+
javaOptions += "-Xmx1G"
192+
)
193+
.settings(testSettings)
194+
.settings(libraryDependencies ++= Seq(scalatest, tagging, pekkoActor))
195+
.dependsOn(macrosPekko, testUtil)
196+
.jvmPlatform(scalaVersions = scala2)
197+
176198
lazy val macrosAutoCats = projectMatrix
177199
.in(file("macrosAutoCats"))
178200
.settings(commonSettings)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.softwaremill.macwire.pekkosupport
2+
3+
import org.apache.pekko.actor.{ActorRef, Props}
4+
import com.softwaremill.macwire.internals.Logger
5+
6+
import scala.reflect.macros.blackbox
7+
8+
object MacwirePekkoMacros {
9+
private val log = new Logger()
10+
11+
def wireProps_Impl[T: c.WeakTypeTag](c: blackbox.Context): c.Expr[Props] =
12+
new internals.Crimper[c.type, T](c, log).wireProps
13+
def wireAnonymousActor_Impl[T: c.WeakTypeTag](c: blackbox.Context): c.Expr[ActorRef] =
14+
new internals.Crimper[c.type, T](c, log).wireAnonymousActor
15+
def wireActor_Impl[T: c.WeakTypeTag](c: blackbox.Context)(name: c.Expr[String]): c.Expr[ActorRef] =
16+
new internals.Crimper[c.type, T](c, log).wireActor(name)
17+
18+
def wirePropsWithProducer_Impl[T: c.WeakTypeTag](c: blackbox.Context): c.Expr[Props] =
19+
new internals.Crimper[c.type, T](c, log).wirePropsWithProducer
20+
def wireAnonymousActorWithProducer_Impl[T: c.WeakTypeTag](c: blackbox.Context): c.Expr[ActorRef] =
21+
new internals.Crimper[c.type, T](c, log).wireAnonymousActorWithProducer
22+
def wireActorWithProducer_Impl[T: c.WeakTypeTag](c: blackbox.Context)(name: c.Expr[String]): c.Expr[ActorRef] =
23+
new internals.Crimper[c.type, T](c, log).wireActorWithProducer(name)
24+
25+
def wirePropsWithFactory_Impl[T: c.WeakTypeTag](c: blackbox.Context)(factory: c.Tree): c.Expr[Props] =
26+
new internals.Crimper[c.type, T](c, log).wirePropsWithFactory(factory)
27+
def wireAnonymousActorWithFactory_Impl[T: c.WeakTypeTag](c: blackbox.Context)(factory: c.Tree): c.Expr[ActorRef] =
28+
new internals.Crimper[c.type, T](c, log).wireAnonymousActorWithFactory(factory)
29+
def wireActorWithFactory_Impl[T: c.WeakTypeTag](c: blackbox.Context)(factory: c.Tree)(
30+
name: c.Expr[String]
31+
): c.Expr[ActorRef] = new internals.Crimper[c.type, T](c, log).wireActorWithFactory(factory)(name)
32+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)