如何获取指定类中混合的特征列表?

发布于 2024-08-31 22:04:28 字数 137 浏览 8 评论 0原文

更具体的例子:

abstract trait A
trait B extends A
trait C extends A

如何检查指定类中混合了哪些扩展特征 A 的特征(可以从 0 到多个)?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅唱々樱花落 2024-09-07 22:04:28

其他答案的混合

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(_))

返回怎么样

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

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)
夏有森光若流苏 2024-09-07 22:04:28
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)
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)
安稳善良 2024-09-07 22:04:28

像这样的事情怎么样:

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

这会找到 clazz 实现的所有特征,这些特征本身就是 baseTrait 的子特征。具有以下特征:

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

使用如下:

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)

这只查看由传入的实例的类直接实现的接口,但可以扩展以递归地查找继承层次结构。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文