Monday, September 20, 2010

Get all supertypes of a class/interface (Scala)

01  def getSuperTypes(c: Class[_]) : Seq[Class[_]] = {
02      val sc = c.getSuperclass
03      val ifaces = c.getInterfaces
04      (sc, ifaces.length) match {
05          case (null, 0) => Nil
06            
07          case (null, _) => 
08            ifaces ++ ifaces.flatMap(getSuperTypes(_))
09            
10          case (_, 0) =>  sc +: getSuperTypes(sc)
11           
12          case (_, x) =>  sc +: (getSuperTypes(sc) ++ 
13                              ifaces.flatMap(getSuperTypes(_)))
14            
15        }
16    }

No comments: