private def getObject[T]()

in chill-scrooge/src/main/scala/com/twitter/chill/scrooge/ScroogeThriftStructSerializer.scala [38:72]


  private def getObject[T](companionClass: Class[T]): AnyRef =
    companionClass.getField("MODULE$").get(null)

  /**
   * For unions, we split on $ after the dot. this is costly, but only done once per Class
   */
  private[this] def codecForUnion[T <: ThriftStruct](maybeUnion: Class[T]): Try[ThriftStructCodec[T]] =
    Try(
      getObject(
        Class.forName(maybeUnion.getName.reverse.dropWhile(_ != '$').reverse, true, maybeUnion.getClassLoader)
      )
    ).map(_.asInstanceOf[ThriftStructCodec[T]])

  private[this] def codecForNormal[T <: ThriftStruct](
      thriftStructClass: Class[T]
  ): Try[ThriftStructCodec[T]] =
    Try(getObject(Class.forName(thriftStructClass.getName + "$", true, thriftStructClass.getClassLoader)))
      .map(_.asInstanceOf[ThriftStructCodec[T]])

  // the companion to a ThriftStruct generated by scrooge will always be its codec
  private[this] def constructCodec[T <: ThriftStruct](thriftStructClass: Class[T]): ThriftStructCodec[T] =
    codecForNormal(thriftStructClass)
      .orElse(codecForUnion(thriftStructClass))
      .get

  private[this] def constructThriftStructSerializer[T <: ThriftStruct](
      thriftStructClass: Class[T]
  ): ThriftStructSerializer[T] = {
    // capture the codec here:
    val newCodec = constructCodec(thriftStructClass)
    new ThriftStructSerializer[T] {
      val protocolFactory = new TBinaryProtocol.Factory
      override def codec: ThriftStructCodec[T] = newCodec
    }
  }