How to get list of traits that were mixed in the s

2019-06-21 04:06发布

问题:

And more specific example:

abstract trait A
trait B extends A
trait C extends A

How to check what traits that extend trait A (it can be from 0 to many) were mixed in specified class?

回答1:

How about a hybrid of the other answers

abstract trait A //interested in this one
trait B extends A //and this one
trait C extends A //this one too
trait D //don't care about this one though

val x = new A with B with D
x.getClass.getInterfaces.filter(classOf[A].isAssignableFrom(_))

returns

Array[java.lang.Class[_]] = Array(interface A, interface B)


回答2:

scala> val x = new A with B with C
x: java.lang.Object with A with B with C = $anon$1@8ea25fa

scala> x.getClass.getInterfaces
res11: Array[java.lang.Class[_]] = Array(interface A, interface B, interface C)


回答3:

How about something like this:

def getTraitsExtending(clazz:Class[_], baseTrait:Class[_]): Seq[Class[_]] = {
  clazz.getInterfaces().filter { baseTrait isAssignableFrom _ }
}

This finds all traits that clazz implements that are themselves subtraits of baseTrait. With the following traits:

trait A
trait B extends A
trait C extends A
trait D

Use as follows:

scala> val x1 = new C with B
x1: java.lang.Object with C with B = $anon$1@51d92803

scala> getTraitsExtending(x1.getClass, classOf[A])
res0: Seq[Class[_]] = WrappedArray(interface C, interface B)

scala> val x2 = new C with A            
x2: java.lang.Object with C with A = $anon$1@f8db08

scala> getTraitsExtending(x2.getClass, classOf[A])
res1: Seq[Class[_]] = WrappedArray(interface C, interface A)

scala> val x3 = new C with D             
x3: java.lang.Object with C with D = $anon$1@2bbd83d

scala> getTraitsExtending(x3.getClass, classOf[A])
res3: Seq[Class[_]] = WrappedArray(interface C)

This only looks at the interfaces that are the directly implemented by the class of the instance passed in, but could be extended to recursively look up the inheritance hierarchy.



标签: scala traits