From 46a5066b434637dc34a704e208cd53c1be00ce1d Mon Sep 17 00:00:00 2001 From: Georgi Krastev Date: Sun, 22 Sep 2024 17:22:05 +0300 Subject: [PATCH] Use primary constructor to extract field annotations (#1374) --- .../src/main/scala/shapeless/annotation.scala | 9 ++++----- .../src/test/scala/shapeless/annotation.scala | 14 +++++++++++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/core/shared/src/main/scala/shapeless/annotation.scala b/core/shared/src/main/scala/shapeless/annotation.scala index 34a542b93..c2add02ea 100644 --- a/core/shared/src/main/scala/shapeless/annotation.scala +++ b/core/shared/src/main/scala/shapeless/annotation.scala @@ -382,11 +382,10 @@ class AnnotationMacros(val c: whitebox.Context) extends CaseClassMacros { private def getAnnotationTreeOptions(tpe: Type, typeAnnotation: Boolean): List[List[(Type, Tree)]] = { if (isProduct(tpe)) { - val constructorSyms = tpe - .member(termNames.CONSTRUCTOR) - .asMethod - .paramLists - .flatten + lazy val constructorSyms = tpe + .typeSymbol.asClass + .primaryConstructor.asMethod + .paramLists.iterator.flatten .map(sym => nameAsString(sym.name) -> sym) .toMap diff --git a/core/shared/src/test/scala/shapeless/annotation.scala b/core/shared/src/test/scala/shapeless/annotation.scala index 2b315e823..45fb6c492 100644 --- a/core/shared/src/test/scala/shapeless/annotation.scala +++ b/core/shared/src/test/scala/shapeless/annotation.scala @@ -16,10 +16,12 @@ package shapeless -import scala.annotation.{ Annotation => saAnnotation } +import org.junit.Assert._ import org.junit.Test import shapeless.test.{illTyped, typed} +import scala.annotation.{Annotation => saAnnotation} + object AnnotationTestsDefinitions { case class First() extends saAnnotation @@ -73,6 +75,10 @@ object AnnotationTestsDefinitions { type PosInt = Int @First type Email = String @Third('c') case class User(age: PosInt, email: Email) + + case class WithSecondaryCtor(i: Int) { + def this() = this(0) + } } class AnnotationTests { @@ -233,4 +239,10 @@ class AnnotationTests { typed[(First :: HNil) :: (Third :: HNil) :: HNil](user) assert(user == (First() :: HNil) :: (Third('c') :: HNil) :: HNil) } + + @Test + def annotationsWithSecondaryConstructor(): Unit = { + val annotations = Annotations[First, WithSecondaryCtor].apply() + assertEquals(annotations, None :: HNil) + } }