• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

iheartradio / ficus / 4027269650

pending completion
4027269650

Pull #246

github

GitHub
Merge b0b1b0f86 into 6d0e09cb4
Pull Request #246: Update sbt-sonatype to 3.9.16

151 of 164 relevant lines covered (92.07%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.67
/src/main/scala-2/net/ceedubs/ficus/util/ReflectionUtils.scala
1
package net.ceedubs.ficus.util
2

3
import macrocompat.bundle
4
import scala.reflect.macros.blackbox
5

6
@bundle
7
trait ReflectionUtils {
8
  val c: blackbox.Context
9

10
  import c.universe._
11

12
  def instantiationMethod[T: c.WeakTypeTag](fail: String => Nothing): c.universe.MethodSymbol = {
13

14
    val returnType = c.weakTypeOf[T]
1✔
15

16
    val returnTypeTypeArgs = returnType match {
17
      case TypeRef(_, _, args) => args
18
      case _                   => Nil
×
19
    }
20

21
    if (returnTypeTypeArgs.nonEmpty)
1✔
22
      fail(
×
23
        s"value readers cannot be auto-generated for types with type parameters. Consider defining your own ValueReader[$returnType]"
×
24
      )
25

26
    val companionSymbol = returnType.typeSymbol.companion match {
1✔
27
      case NoSymbol => None
1✔
28
      case x        => Some(x)
1✔
29
    }
30

31
    val applyMethods = companionSymbol.toList.flatMap(_.typeSignatureIn(returnType).members collect {
1✔
32
      case m: MethodSymbol if m.name.decodedName.toString == "apply" && m.returnType <:< returnType => m
1✔
33
    })
34

35
    val applyMethod = applyMethods match {
36
      case Nil           => None
1✔
37
      case (head :: Nil) => Some(head)
1✔
38
      case _             => fail(s"its companion object has multiple apply methods that return type $returnType")
×
39
    }
40

41
    applyMethod getOrElse {
1✔
42
      val primaryConstructor = returnType.decl(termNames.CONSTRUCTOR) match {
1✔
43
        case t: TermSymbol =>
44
          val constructors            = t.alternatives collect {
1✔
45
            case m: MethodSymbol if m.isConstructor => m
1✔
46
          }
47
          val primaryScalaConstructor = constructors.find(m => m.isPrimaryConstructor && !m.isJava)
1✔
48
          primaryScalaConstructor orElse {
1✔
49
            if (constructors.length == 1) constructors.headOption else None
×
50
          }
51
        case _             => None
×
52
      }
53
      primaryConstructor getOrElse {
1✔
54
        fail(
×
55
          s"it has no apply method in a companion object that returns type $returnType, and it doesn't have a primary constructor"
×
56
        )
57
      }
58
    }
59
  }
60

61
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc