使用Scala 2.10反射如何列出Enumeration的值?

Nik*_*kov 8 reflection enumeration scala scala-2.10

有以下枚举

object ResponseType extends Enumeration {
  val Listing, Album = Value
}
Run Code Online (Sandbox Code Playgroud)

我如何获得其vals列表?

Tra*_*own 16

如果您想彻底了解这一点,您需要检查您的符号是否Value具有超类型:

def valueSymbols[E <: Enumeration: TypeTag] = {
  val valueType = typeOf[E#Value]
  typeOf[E].members.filter(sym => !sym.isMethod &&
    sym.typeSignature.baseType(valueType.typeSymbol) =:= valueType
  )
}
Run Code Online (Sandbox Code Playgroud)

现在即使你有以下(这是完全合法的):

object ResponseType extends Enumeration {
  val Listing, Album = Value
  val someNonValueThing = "You don't want this in your list of value symbols!"
}
Run Code Online (Sandbox Code Playgroud)

你仍然得到正确的答案:

scala> valueSymbols[ResponseType.type] foreach println
value Album
value Listing
Run Code Online (Sandbox Code Playgroud)

您目前的方法将包括value someNonValueThing此处.

  • 请注意,"Enumeration#Value"的类型别名与上面的过滤谓词匹配.为了解决这个问题,还要检查`!sym.isType`. (4认同)

Nik*_*kov 7

以下代码获取Symbol表示"vals" 的对象列表:

import reflect.runtime.universe._ // Access the reflection api

typeOf[ResponseType.Value]  //  - A `Type`, the basic unit of reflection api
  .asInstanceOf[TypeRef]    //  - A specific kind of Type with knowledge of
                            //    the place it's being referred from
  .pre                      //  - AFAIK the referring Type
  .members                  //  - A list of `Symbol` objects representing
                            //    all members of this type, including 
                            //    private and inherited ones, classes and 
                            //    objects - everything.
                            //    `Symbol`s are the second basic unit of 
                            //    reflection api.
  .view                     //  - For lazy filtering
  .filter(_.isTerm)         //  - Leave only the symbols which extend the  
                            //    `TermSymbol` type. This is a very broad 
                            //    category of symbols, which besides values
                            //    includes methods, classes and even 
                            //    packages.
  .filterNot(_.isMethod)    //  - filter out the rest
  .filterNot(_.isModule)
  .filterNot(_.isClass)
  .toList                   //  - force the view into a final List
Run Code Online (Sandbox Code Playgroud)

应该注意的是,不是对is-clauses进行过滤,而是可以通过.collect测试特定类型来实现,如下所示:

.collect{ case symbol : MethodSymbol => symbol }
Run Code Online (Sandbox Code Playgroud)

反射api中的其他任务可能需要这种方法.

还应该注意的是,使用.view不是强制性的话,那只是让使用一系列的filter应用程序(多达许多其他功能,如map,flatMap等)更有效,通过遍历输入集合只有一次,在实际上它被强制进入一些具体的集合(.toList在我们的例子中).

更新

正如Travis Brown所建议的那样,也可以ResponseType直接引用该对象.所以typeOf[ResponseType.Value].asInstanceOf[TypeRef].pre部件可以替换为 typeOf[ResponseType.type]