Question

I have this code that gets the abstract type pattern reflect.runtime.universe.MethodSymbol is unchecked since it is eliminated by erasure warnings in every place that the case keyword is used. I have a feeling it's something strange about the reflection APIs. The thing is, it works when I run it (both passing and failing code paths) so it seems like the warning is erroneous. How do I eliminate the warning?

  import scala.reflect.runtime.{universe => u}

  val docs = {
    val ann = u.typeOf[T].members
      .collect { case m: u.MethodSymbol if m.isGetter => m }
      .find(_.name.decoded == prop.propertyName)

    val docAnnotation = ann.flatMap(_.annotations.find(_.tpe.typeSymbol.name.decoded == "docs"))
    val trees = docAnnotation.map(_.scalaArgs).getOrElse(Nil)
    val args = trees.map {
      case u.Apply(_, List(u.Literal(u.Constant(value)))) => Some(value.asInstanceOf[String])
      case u.Select(_, name) if name.decoded == "None" => None
    }
    val safeGetArg = args.lift(_: Int).flatten

    Documentation(safeGetArg(0), safeGetArg(1))
  }
Was it helpful?

Solution

Eliminate the u, eliminate the warning?

apm@mara:~$ scala210
Welcome to Scala version 2.10.4 (OpenJDK 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import scala.reflect.runtime.universe
import scala.reflect.runtime.universe

scala> import universe._
import universe._

scala> class X { val x = 7 ; var y = "hi" }
defined class X

scala> typeOf[X].members collect { case m: MethodSymbol => m }
res0: Iterable[reflect.runtime.universe.MethodSymbol] = List(method y_=, method y, value x, constructor X, method $asInstanceOf, method $isInstanceOf, method synchronized, method ##, method !=, method ==, method ne, method eq, method notifyAll, method notify, method clone, method getClass, method hashCode, method toString, method equals, method wait, method wait, method wait, method finalize, method asInstanceOf, method isInstanceOf, method !=, method ==)

scala> typeOf[X].members collect { case m: MethodSymbol if m.isGetter => m }
res1: Iterable[reflect.runtime.universe.MethodSymbol] = List(method y, value x)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top