All Downloads are FREE. Search and download functionalities are using the official Maven repository.

wei-k.sbt-class-diagram_sbt2.0.0-M2_3.0.3.0.source-code.Reflect.scala Maven / Gradle / Ivy

The newest version!
package diagram

object Reflect {

  private def getSuperClasses(clazz: Class[?]): List[Class[?]] = {

    @scala.annotation.tailrec
    def loop(o: Class[?], result: List[Class[?]]): List[Class[?]] = {
      val superClass = o.getSuperclass
      if (superClass == null) {
        result
      } else {
        loop(superClass, superClass :: result)
      }
    }

    loop(clazz, List(clazz))
  }

  def getAllClassAndTrait(classes: Class[?]*): List[Class[?]] = {

    def loop(c: Class[?], result: List[Class[?]]): List[Class[?]] = {
      val interfaces = c.getInterfaces.toList

      if (interfaces.size == 0) {
        result
      } else {
        interfaces.flatMap { i => loop(i, i :: result) }
      }
    }

    {
      {
        for {
          c <- classes
          clazz <- getSuperClasses(c)
          ret <- loop(clazz, List(clazz))
        } yield {
          ret
        }
      }.toSet -- classes
    }.toList
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy