forked from mockito/mockito-scala
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValueClassExtractorMacro.scala
More file actions
31 lines (24 loc) · 962 Bytes
/
Copy pathValueClassExtractorMacro.scala
File metadata and controls
31 lines (24 loc) · 962 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package org.mockito.internal
import org.mockito.internal.MacroDebug.debugResult
import scala.reflect.macros.blackbox
/**
* Scala 2 macro implementation for ValueClassExtractor.
*/
trait ValueClassExtractorCompat {
implicit def instance[VC]: ValueClassExtractor[VC] = macro ValueClassExtractorMacro.materialise[VC]
}
object ValueClassExtractorMacro {
def materialise[VC: c.WeakTypeTag](c: blackbox.Context): c.Expr[ValueClassExtractor[VC]] = {
import c.universe.*
val tpe = weakTypeOf[VC]
val typeSymbol = tpe.typeSymbol
val isValueClass = typeSymbol.isClass && typeSymbol.asClass.isDerivedValueClass
val r =
if (isValueClass) {
c.Expr[ValueClassExtractor[VC]](q"new _root_.org.mockito.internal.ReflectionExtractor[$tpe]")
} else
c.Expr[ValueClassExtractor[VC]](q"new _root_.org.mockito.internal.NormalClassExtractor[$tpe]")
debugResult(c)("mockito-print-extractor")(r.tree)
r
}
}