Skip to content

Commit 07293d6

Browse files
authored
Also detect @jakarta.inject.Inject annotations (#377)
1 parent df00284 commit 07293d6

File tree

7 files changed

+38
-23
lines changed

7 files changed

+38
-23
lines changed

macros/src/main/scala-2/com/softwaremill/macwire/internals/ConstructorCrimper.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,12 @@ object ConstructorCrimper {
7474
lazy val primaryConstructor: Option[Symbol] = publicConstructors.find(_.asMethod.isPrimaryConstructor)
7575

7676
lazy val injectConstructors: Iterable[Symbol] = {
77-
val isInjectAnnotation = (a: Annotation) => a.toString == "javax.inject.Inject"
77+
val isInjectAnnotation = (a: Annotation) =>
78+
a.toString == "javax.inject.Inject" || a.toString == "jakarta.inject.Inject"
7879
val ctors = publicConstructors.filter(_.annotations.exists(isInjectAnnotation))
79-
log.withBlock(s"There are ${ctors.size} constructors annotated with @javax.inject.Inject") {
80+
log.withBlock(
81+
s"There are ${ctors.size} constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject"
82+
) {
8083
ctors.foreach(s => log(showConstructor(c)(s)))
8184
}
8285
ctors
@@ -86,7 +89,7 @@ object ConstructorCrimper {
8689
if (injectConstructors.size > 1)
8790
c.abort(
8891
c.enclosingPosition,
89-
s"Ambiguous constructors annotated with @javax.inject.Inject for type [$targetType]"
92+
s"Ambiguous constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type [$targetType]"
9093
)
9194
else injectConstructors.headOption
9295

macros/src/main/scala-3/com/softwaremill/macwire/internals/ConstructorCrimper.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,22 @@ private[macwire] class ConstructorCrimper[Q <: Quotes, T: Type](using val q: Q)(
3434
}
3535

3636
lazy val injectConstructors: Iterable[Symbol] = {
37-
val isInjectAnnotation = (a: Term) => a.tpe.typeSymbol.fullName == "javax.inject.Inject"
37+
val isInjectAnnotation = (a: Term) =>
38+
a.tpe.typeSymbol.fullName == "javax.inject.Inject" || a.tpe.typeSymbol.fullName == "jakarta.inject.Inject"
3839
val ctors = publicConstructors.filter(_.annotations.exists(isInjectAnnotation))
39-
log.withBlock(s"There are ${ctors.size} constructors annotated with @javax.inject.Inject") {
40+
log.withBlock(
41+
s"There are ${ctors.size} constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject"
42+
) {
4043
ctors.foreach(c => log(showConstructor(c)))
4144
}
4245
ctors
4346
}
4447

4548
lazy val injectConstructor: Option[Symbol] =
4649
if (injectConstructors.size > 1)
47-
abort(s"Ambiguous constructors annotated with @javax.inject.Inject for type [${targetType.typeSymbol.name}]")
50+
abort(
51+
s"Ambiguous constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type [${targetType.typeSymbol.name}]"
52+
)
4853
else injectConstructors.headOption
4954

5055
lazy val constructor: Option[Symbol] = log.withBlock(s"Looking for constructor for $targetType") {

macros/src/main/scala-3/com/softwaremill/macwire/internals/autowire/creator.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,20 @@ object Constructor:
8787
case c => None
8888

8989
val injectConstructors: Iterable[Symbol] =
90-
val isInjectAnnotation = (a: Term) => a.tpe.typeSymbol.fullName == "javax.inject.Inject"
90+
val isInjectAnnotation = (a: Term) =>
91+
a.tpe.typeSymbol.fullName == "javax.inject.Inject" || a.tpe.typeSymbol.fullName == "jakarta.inject.Inject"
9192
val ctors = publicConstructors.filter(_.annotations.exists(isInjectAnnotation))
92-
log.withBlock(s"there are ${ctors.size} constructors annotated with @javax.inject.Inject") {
93+
log.withBlock(
94+
s"there are ${ctors.size} constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject"
95+
) {
9396
ctors.foreach(c => log(c.toString))
9497
}
9598
ctors
9699

97100
val injectConstructor: Option[Symbol] =
98101
if injectConstructors.size > 1 then
99102
reportError(
100-
s"multiple constructors annotated with @javax.inject.Inject for type: ${showTypeName(forType)}"
103+
s"multiple constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type: ${showTypeName(forType)}"
101104
)
102105
else injectConstructors.headOption
103106

macrosAkkaTests/src/test/scala/com/softwaremill/macwire/akkasupport/CompileTests.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ class CompileTests extends CompileTestsSupport {
2828
"type arguments [NotActor] do not conform to macro method wireActor's type parameter bounds [T <: akka.actor.Actor]"
2929
),
3030
"wireProps-11-toManyInjectAnnotations" -> List(
31-
"Ambiguous constructors annotated with @javax.inject.Inject for type [SomeActor]"
31+
"Ambiguous constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type [SomeActor]"
3232
),
3333
"wireAnonymousActor-11-toManyInjectAnnotations" -> List(
34-
"Ambiguous constructors annotated with @javax.inject.Inject for type [SomeActor]"
34+
"Ambiguous constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type [SomeActor]"
3535
),
3636
"wireActor-11-toManyInjectAnnotations" -> List(
37-
"Ambiguous constructors annotated with @javax.inject.Inject for type [SomeActor]"
37+
"Ambiguous constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type [SomeActor]"
3838
),
3939
"wireProps-12-noPublicConstructor" -> List("Cannot find a public constructor for [SomeActor]"),
4040
"wireAnonymousActor-12-noPublicConstructor" -> List("Cannot find a public constructor for [SomeActor]"),
@@ -54,7 +54,7 @@ class CompileTests extends CompileTestsSupport {
5454
"wireActorWithProducer-6-injectAnnotationButNoDependencyInScope" -> List("Cannot find a value of type: [C]"),
5555
"wireActorWithProducer-7-notActorProducer" -> List("wireActorWith does not support the type: [NotProducer]"),
5656
"wireActorWithProducer-11-toManyInjectAnnotations" -> List(
57-
"Ambiguous constructors annotated with @javax.inject.Inject for type [SomeActorProducer]"
57+
"Ambiguous constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type [SomeActorProducer]"
5858
),
5959
"wireActorWithProducer-12-noPublicConstructor" -> List(
6060
"Cannot find a public constructor for [SomeActorProducer]"
@@ -76,7 +76,7 @@ class CompileTests extends CompileTestsSupport {
7676
"wireAnonymousActorWith does not support the type: [NotProducer]"
7777
),
7878
"wireAnonymousActorWithProducer-11-toManyInjectAnnotations" -> List(
79-
"Ambiguous constructors annotated with @javax.inject.Inject for type [SomeActorProducer]"
79+
"Ambiguous constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type [SomeActorProducer]"
8080
),
8181
"wireAnonymousActorWithProducer-12-noPublicConstructor" -> List(
8282
"Cannot find a public constructor for [SomeActorProducer]"
@@ -96,7 +96,7 @@ class CompileTests extends CompileTestsSupport {
9696
"wirePropsWithProducer-6-injectAnnotationButNoDependencyInScope" -> List("Cannot find a value of type: [C]"),
9797
"wirePropsWithProducer-7-notActorProducer" -> List("wirePropsWith does not support the type: [NotProducer]"),
9898
"wirePropsWithProducer-11-toManyInjectAnnotations" -> List(
99-
"Ambiguous constructors annotated with @javax.inject.Inject for type [SomeActorProducer]"
99+
"Ambiguous constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type [SomeActorProducer]"
100100
),
101101
"wirePropsWithProducer-12-noPublicConstructor" -> List(
102102
"Cannot find a public constructor for [SomeActorProducer]"

macrosPekkoTests/src/test/scala/com/softwaremill/macwire/pekkosupport/CompileTests.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ class CompileTests extends CompileTestsSupport {
2828
"type arguments [NotActor] do not conform to macro method wireActor's type parameter bounds [T <: org.apache.pekko.actor.Actor]"
2929
),
3030
"wireProps-11-toManyInjectAnnotations" -> List(
31-
"Ambiguous constructors annotated with @javax.inject.Inject for type [SomeActor]"
31+
"Ambiguous constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type [SomeActor]"
3232
),
3333
"wireAnonymousActor-11-toManyInjectAnnotations" -> List(
34-
"Ambiguous constructors annotated with @javax.inject.Inject for type [SomeActor]"
34+
"Ambiguous constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type [SomeActor]"
3535
),
3636
"wireActor-11-toManyInjectAnnotations" -> List(
37-
"Ambiguous constructors annotated with @javax.inject.Inject for type [SomeActor]"
37+
"Ambiguous constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type [SomeActor]"
3838
),
3939
"wireProps-12-noPublicConstructor" -> List("Cannot find a public constructor for [SomeActor]"),
4040
"wireAnonymousActor-12-noPublicConstructor" -> List("Cannot find a public constructor for [SomeActor]"),
@@ -54,7 +54,7 @@ class CompileTests extends CompileTestsSupport {
5454
"wireActorWithProducer-6-injectAnnotationButNoDependencyInScope" -> List("Cannot find a value of type: [C]"),
5555
"wireActorWithProducer-7-notActorProducer" -> List("wireActorWith does not support the type: [NotProducer]"),
5656
"wireActorWithProducer-11-toManyInjectAnnotations" -> List(
57-
"Ambiguous constructors annotated with @javax.inject.Inject for type [SomeActorProducer]"
57+
"Ambiguous constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type [SomeActorProducer]"
5858
),
5959
"wireActorWithProducer-12-noPublicConstructor" -> List(
6060
"Cannot find a public constructor for [SomeActorProducer]"
@@ -76,7 +76,7 @@ class CompileTests extends CompileTestsSupport {
7676
"wireAnonymousActorWith does not support the type: [NotProducer]"
7777
),
7878
"wireAnonymousActorWithProducer-11-toManyInjectAnnotations" -> List(
79-
"Ambiguous constructors annotated with @javax.inject.Inject for type [SomeActorProducer]"
79+
"Ambiguous constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type [SomeActorProducer]"
8080
),
8181
"wireAnonymousActorWithProducer-12-noPublicConstructor" -> List(
8282
"Cannot find a public constructor for [SomeActorProducer]"
@@ -96,7 +96,7 @@ class CompileTests extends CompileTestsSupport {
9696
"wirePropsWithProducer-6-injectAnnotationButNoDependencyInScope" -> List("Cannot find a value of type: [C]"),
9797
"wirePropsWithProducer-7-notActorProducer" -> List("wirePropsWith does not support the type: [NotProducer]"),
9898
"wirePropsWithProducer-11-toManyInjectAnnotations" -> List(
99-
"Ambiguous constructors annotated with @javax.inject.Inject for type [SomeActorProducer]"
99+
"Ambiguous constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type [SomeActorProducer]"
100100
),
101101
"wirePropsWithProducer-12-noPublicConstructor" -> List(
102102
"Cannot find a public constructor for [SomeActorProducer]"

tests/src/test/scala-2/com/softwaremill/macwire/CompileTests.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class CompileTests extends CompileTestsSupport {
3131
"Companion object for",
3232
"Target] has no apply methods constructing target type."
3333
),
34-
"toManyInjectAnnotations" -> List("Ambiguous constructors annotated with @javax.inject.Inject for type [Target]"),
34+
"toManyInjectAnnotations" -> List(
35+
"Ambiguous constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type [Target]"
36+
),
3537
"nullaryMethodUsedAsCandidate" -> List("Found multiple values of type [A]: [List(Module.foo(), a)]"),
3638
"wireWithTwoParamsLists" -> List("found : A => (B => __wrapper$1", "required: ? => __wrapper$1"),
3739
"wireRecEmptyString" -> List(valueNotFound("String"))

tests/src/test/scala-3/com/softwaremill/macwire/CompileTests.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class CompileTests extends CompileTestsSupport {
3333
"has no apply methods constructing target type",
3434
"[Target]"
3535
),
36-
"toManyInjectAnnotations" -> List("Ambiguous constructors annotated with @javax.inject.Inject for type [Target]"),
36+
"toManyInjectAnnotations" -> List(
37+
"Ambiguous constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type [Target]"
38+
),
3739
"wireWithTwoParamsLists" -> List("Found: Main.A => Main.B => Main.Test.C", "Required: Any => Main.Test.C"),
3840
"wireRecEmptyString" -> List(valueNotFound("String"))
3941
)

0 commit comments

Comments
 (0)