Finding objects using scala's runtime reflection

前端 未结 1 696
忘了有多久
忘了有多久 2021-01-14 18:59

Context

I use scala 2.11.6 currently, possibly 2.11.7 in the future. Given compiled class files in the classpath, I want to do 2 things:

  • Find the

相关标签:
1条回答
  • 2021-01-14 19:36

    For the first question, you could use ClassUtil library. Note that it'll find Java classes, and the ones which correspond to objects will have names ending in $class. See also Scala Reflection - Loading or finding classes based on trait.

    For the second, objects are called "modules" in Scala reflection, so you don't need isSingletonObject(name); if it isn't, rm.staticModule(name).asModule in your code will fail. There doesn't seem to be a way to check if it's a compiler-generated empty companion object (isSynthetic returns false), but they will be ruled out by the subtyping check anyway (so will the static parts of Java classes, but you can also filter those out using isJava).

    0 讨论(0)
提交回复
热议问题