user48956
user48956

Reputation: 15788

How to filter methods by return type with scala reflection?

I'm trying to test whether a method's return type exactly matches a supplied type.

Somehow, I've found two String Types that are not equal.

class VarAndValue {
  @BeanProperty
  val value = "";    
}

class ScalaReflectionTest {

  @Test
  def myTest(): Unit = {

    val type1 = universe.typeOf[String]

    // Get return value of VarAndValue.getValue
    val type2 = universe.typeOf[VarAndValue].
      declarations.
      filter { m => m.name.decoded == "getValue" && m.isMethod }.
      head.
      asInstanceOf[universe.MethodSymbol].
      returnType

      println(type1)   // String
      println(type2)   // String
      println(type1.getClass())
      println(type2.getClass()) // !=type1.getClass()   !!
      println(type1==type2)     // False
   }
}

yields...

String
String
class scala.reflect.internal.Types$TypeRef$$anon$3
class scala.reflect.internal.Types$TypeRef$$anon$6
false

How can I filter methods of a class by their return type? (It seems very difficult if I can test for equality of the return types).

Scala 2.10

UPDATE:

I can't drop into the Java reflection universe because this erased type information for generics like List[Int] (which become List[Object] is java-land where they're really List[java.lang.Integer]). I need my matched to pay attention to the generic parameter information that the scala type universe preserves.

Upvotes: 1

Views: 965

Answers (2)

som-snytt
som-snytt

Reputation: 39577

Use =:= to compare types.

scala> import beans._
import beans._

scala> :pa
// Entering paste mode (ctrl-D to finish)

class VarAndValue {
  @BeanProperty
  val value = "";    
}

// Exiting paste mode, now interpreting.

defined class VarAndValue

scala> import reflect.runtime._, universe._
import reflect.runtime._
import universe._

scala> val str = typeOf[String]
str: reflect.runtime.universe.Type = String

scala> val ms = typeOf[VarAndValue].declarations filter (m => m.isMethod && m.name.decoded == "getValue")
warning: there were two deprecation warnings; re-run with -deprecation for details
ms: Iterable[reflect.runtime.universe.Symbol] = SynchronizedOps(method getValue)

scala> val mm = ms.toList
mm: List[reflect.runtime.universe.Symbol] = List(method getValue)

scala> val m = mm.head.asInstanceOf[MethodSymbol]
m: reflect.runtime.universe.MethodSymbol = method getValue

scala> val t = m.returnType
t: reflect.runtime.universe.Type = java.lang.String

scala> println(t)
java.lang.String

scala> println(str)
String

scala> str == t
res2: Boolean = false

scala> str match { case `t` => }
scala.MatchError: String (of class scala.reflect.internal.Types$AliasNoArgsTypeRef)
  ... 33 elided

scala> str =:= t
res4: Boolean = true

scala> str match { case TypeRef(a,b,c) => (a,b,c) }
res5: (reflect.runtime.universe.Type, reflect.runtime.universe.Symbol, List[reflect.runtime.universe.Type]) = (scala.Predef.type,type String,List())

scala> t match { case TypeRef(a,b,c) => (a,b,c) }
res6: (reflect.runtime.universe.Type, reflect.runtime.universe.Symbol, List[reflect.runtime.universe.Type]) = (java.lang.type,class String,List())

Depending on what you're going for, consider that conforming types can have differing representations:

scala> typeOf[List[Int]]
res11: reflect.runtime.universe.Type = scala.List[Int]

scala> type X = List[Int]
defined type alias X

scala> typeOf[X].dealias
res12: reflect.runtime.universe.Type = List[scala.Int]

scala> res11 =:= res12
res13: Boolean = true

scala> res11 == res12
res14: Boolean = false

scala> typeOf[X] <:< res11
res15: Boolean = true

scala> typeOf[X] =:= res11
res16: Boolean = true

Upvotes: 3

user48956
user48956

Reputation: 15788

Turns out this is the way to compare types ...

type1 match {
  case type2 => true
  case _ => false
}

which gives a different answer than type1==type.

Scala is a strange beast.

Upvotes: 0

Related Questions