Skip to content

Commit 9a4cfc6

Browse files
authored
Merge pull request #559 from hughsimpson/annotation_tests
uncomment and fix java annotation tests
2 parents e046c3d + 5e533af commit 9a4cfc6

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

core/src/main/scala/magnolia1/macro.scala

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -178,14 +178,14 @@ object Macro:
178178

179179
private val tpe: TypeRepr = TypeRepr.of[T]
180180

181-
def anns: Expr[List[Any]] =
181+
def anns: Expr[List[scala.annotation.Annotation]] =
182182
Expr.ofList {
183183
tpe.typeSymbol.annotations
184184
.filter(filterAnnotation)
185-
.map(_.asExpr.asInstanceOf[Expr[Any]])
185+
.map(_.asExpr.asInstanceOf[Expr[scala.annotation.Annotation]])
186186
}
187187

188-
def inheritedAnns: Expr[List[Any]] =
188+
def inheritedAnns: Expr[List[scala.annotation.Annotation]] =
189189
Expr.ofList {
190190
tpe.baseClasses
191191
.filterNot(isObjectOrScala)
@@ -194,10 +194,10 @@ object Macro:
194194
} // skip self
195195
.flatten
196196
.filter(filterAnnotation)
197-
.map(_.asExpr.asInstanceOf[Expr[Any]])
197+
.map(_.asExpr.asInstanceOf[Expr[scala.annotation.Annotation]])
198198
}
199199

200-
def typeAnns: Expr[List[Any]] = {
200+
def typeAnns: Expr[List[scala.annotation.Annotation]] = {
201201

202202
def getAnnotations(t: TypeRepr): List[Term] = t match
203203
case AnnotatedType(inner, ann) => ann :: getAnnotations(inner)
@@ -212,28 +212,28 @@ object Macro:
212212
.collect { case t: TypeTree => t.tpe }
213213
.flatMap(getAnnotations)
214214
.filter(filterAnnotation)
215-
.map(_.asExpr.asInstanceOf[Expr[Any]])
215+
.map(_.asExpr.asInstanceOf[Expr[scala.annotation.Annotation]])
216216
case _ =>
217217
// Best effort in case whe -Yretain-trees is not used
218218
// Does not support class parent annotations (in the extends clouse)
219219
tpe.baseClasses
220220
.map(tpe.baseType(_))
221221
.flatMap(getAnnotations(_))
222222
.filter(filterAnnotation)
223-
.map(_.asExpr)
223+
.map(_.asExprOf[scala.annotation.Annotation])
224224
}
225225
}
226226
}
227227

228-
def paramAnns: Expr[List[(String, List[Any])]] =
228+
def paramAnns: Expr[List[(String, List[scala.annotation.Annotation])]] =
229229
Expr.ofList {
230230
groupByParamName {
231231
(fromConstructor(tpe.typeSymbol) ++ fromDeclarations(tpe.typeSymbol))
232232
.filter { case (_, anns) => anns.nonEmpty }
233233
}
234234
}
235235

236-
def inheritedParamAnns: Expr[List[(String, List[Any])]] =
236+
def inheritedParamAnns: Expr[List[(String, List[scala.annotation.Annotation])]] =
237237
Expr.ofList {
238238
groupByParamName {
239239
tpe.baseClasses
@@ -248,25 +248,25 @@ object Macro:
248248
}
249249
}
250250

251-
private def fromConstructor(from: Symbol): List[(String, List[Expr[Any]])] =
251+
private def fromConstructor(from: Symbol): List[(String, List[Expr[scala.annotation.Annotation]])] =
252252
from.primaryConstructor.paramSymss.flatten.map { field =>
253253
field.name -> field.annotations
254254
.filter(filterAnnotation)
255-
.map(_.asExpr.asInstanceOf[Expr[Any]])
255+
.map(_.asExpr.asInstanceOf[Expr[scala.annotation.Annotation]])
256256
}
257257

258258
private def fromDeclarations(
259259
from: Symbol,
260260
inherited: Boolean = false
261-
): List[(String, List[Expr[Any]])] =
261+
): List[(String, List[Expr[scala.annotation.Annotation]])] =
262262
from.fieldMembers.collect { case field: Symbol =>
263263
val annotations = if (!inherited) field.annotations else field.allOverriddenSymbols.flatMap(_.annotations).toList
264264
field.name -> annotations
265265
.filter(filterAnnotation)
266-
.map(_.asExpr.asInstanceOf[Expr[Any]])
266+
.map(_.asExpr.asInstanceOf[Expr[scala.annotation.Annotation]])
267267
}
268268

269-
private def groupByParamName(anns: List[(String, List[Expr[Any]])]) =
269+
private def groupByParamName(anns: List[(String, List[Expr[scala.annotation.Annotation]])]) =
270270
anns
271271
.groupBy { case (name, _) => name }
272272
.toList
@@ -277,7 +277,8 @@ object Macro:
277277
bc.name.contains("java.lang.Object") || bc.fullName.startsWith("scala.")
278278

279279
private def filterAnnotation(a: Term): Boolean =
280-
a.tpe.typeSymbol.maybeOwner.isNoSymbol ||
281-
(a.tpe.typeSymbol.owner.fullName != "scala.annotation.internal" &&
282-
a.tpe.typeSymbol.owner.fullName != "jdk.internal")
280+
scala.util.Try(a.tpe <:< TypeRepr.of[scala.annotation.Annotation]).toOption.contains(true) &&
281+
(a.tpe.typeSymbol.maybeOwner.isNoSymbol ||
282+
(a.tpe.typeSymbol.owner.fullName != "scala.annotation.internal" &&
283+
a.tpe.typeSymbol.owner.fullName != "jdk.internal"))
283284
}

test/src/test/scala/magnolia1/tests/AnnotationsTests.scala

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,15 @@ class AnnotationsTests extends munit.FunSuite:
7474
assertEquals(subtypeAnnotations(1).mkString, "MyAnnotation(2)")
7575
}
7676

77-
// TODO - not compiling
78-
// test("serialize case class with Java annotations by skipping them") {
79-
// val res = Show.derived[MyDto].show(MyDto("foo", 42))
80-
// assertEquals(res, "MyDto{MyAnnotation(0)}(foo=foo,bar=42)")
81-
// }
82-
83-
// TODO - not compiling
84-
// test("serialize case class with Java annotations which comes from external module by skipping them") {
85-
// val res = Show.derived[JavaAnnotatedCase].show(JavaAnnotatedCase(1))
86-
// assertEquals(res, "JavaAnnotatedCase(v=1)")
87-
// }
77+
test("serialize case class with Java annotations by skipping them") {
78+
val res = Show.derived[MyDto].show(MyDto("foo", 42))
79+
assertEquals(res, "MyDto{MyAnnotation(0)}(foo=foo,bar=42)")
80+
}
81+
82+
test("serialize case class with Java annotations which comes from external module by skipping them") {
83+
val res = Show.derived[JavaAnnotatedCase].show(JavaAnnotatedCase(1))
84+
assertEquals(res, "JavaAnnotatedCase(v=1)")
85+
}
8886

8987
object AnnotationsTests:
9088

0 commit comments

Comments
 (0)