Skip to content

Commit

Permalink
Also detect @jakarta.inject.Inject annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurz committed Jan 26, 2025
1 parent 572322a commit 0d15985
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ object ConstructorCrimper {
lazy val primaryConstructor: Option[Symbol] = publicConstructors.find(_.asMethod.isPrimaryConstructor)

lazy val injectConstructors: Iterable[Symbol] = {
val isInjectAnnotation = (a: Annotation) => a.toString == "javax.inject.Inject"
val isInjectAnnotation = (a: Annotation) =>
a.toString == "javax.inject.Inject" || a.toString == "jakarta.inject.Inject"
val ctors = publicConstructors.filter(_.annotations.exists(isInjectAnnotation))
log.withBlock(s"There are ${ctors.size} constructors annotated with @javax.inject.Inject") {
log.withBlock(
s"There are ${ctors.size} constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject"
) {
ctors.foreach(s => log(showConstructor(c)(s)))
}
ctors
Expand All @@ -86,7 +89,7 @@ object ConstructorCrimper {
if (injectConstructors.size > 1)
c.abort(
c.enclosingPosition,
s"Ambiguous constructors annotated with @javax.inject.Inject for type [$targetType]"
s"Ambiguous constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type [$targetType]"
)
else injectConstructors.headOption

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,22 @@ private[macwire] class ConstructorCrimper[Q <: Quotes, T: Type](using val q: Q)(
}

lazy val injectConstructors: Iterable[Symbol] = {
val isInjectAnnotation = (a: Term) => a.tpe.typeSymbol.fullName == "javax.inject.Inject"
val isInjectAnnotation = (a: Term) =>
a.tpe.typeSymbol.fullName == "javax.inject.Inject" || a.tpe.typeSymbol.fullName == "jakarta.inject.Inject"
val ctors = publicConstructors.filter(_.annotations.exists(isInjectAnnotation))
log.withBlock(s"There are ${ctors.size} constructors annotated with @javax.inject.Inject") {
log.withBlock(
s"There are ${ctors.size} constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject"
) {
ctors.foreach(c => log(showConstructor(c)))
}
ctors
}

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

lazy val constructor: Option[Symbol] = log.withBlock(s"Looking for constructor for $targetType") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,20 @@ object Constructor:
case c => None

val injectConstructors: Iterable[Symbol] =
val isInjectAnnotation = (a: Term) => a.tpe.typeSymbol.fullName == "javax.inject.Inject"
val isInjectAnnotation = (a: Term) =>
a.tpe.typeSymbol.fullName == "javax.inject.Inject" || a.tpe.typeSymbol.fullName == "jakarta.inject.Inject"
val ctors = publicConstructors.filter(_.annotations.exists(isInjectAnnotation))
log.withBlock(s"there are ${ctors.size} constructors annotated with @javax.inject.Inject") {
log.withBlock(
s"there are ${ctors.size} constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject"
) {
ctors.foreach(c => log(c.toString))
}
ctors

val injectConstructor: Option[Symbol] =
if injectConstructors.size > 1 then
reportError(
s"multiple constructors annotated with @javax.inject.Inject for type: ${showTypeName(forType)}"
s"multiple constructors annotated with @javax.inject.Inject or @jakarta.inject.Inject for type: ${showTypeName(forType)}"
)
else injectConstructors.headOption

Expand Down

0 comments on commit 0d15985

Please sign in to comment.