如何使用scala反射按返回类型筛选方法?

如何使用scala反射按返回类型筛选方法?,scala,scala-2.10,scala-reflect,Scala,Scala 2.10,Scala Reflect,我试图测试方法的返回类型是否与提供的类型完全匹配 不知何故,我发现了两种不同的字符串类型 class VarAndValue { @BeanProperty val value = ""; } class ScalaReflectionTest { @Test def myTest(): Unit = { val type1 = universe.typeOf[String] // Get return value of VarAndValue.g

我试图测试方法的返回类型是否与提供的类型完全匹配

不知何故,我发现了两种不同的字符串类型

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
   }
}
产量

String
String
class scala.reflect.internal.Types$TypeRef$$anon$3
class scala.reflect.internal.Types$TypeRef$$anon$6
false
如何按返回类型筛选类的方法?(若我能测试返回类型的相等性,这似乎非常困难)

Scala 2.10

更新:


我不能进入Java反射领域,因为这删除了诸如List[Int](变成List[Object]的泛型的类型信息,而实际上是List[Java.lang.Integer])。我需要我的匹配对象注意scala类型宇宙保留的通用参数信息。

结果表明,这是比较类型的方法

type1 match {
  case type2 => true
  case _ => false
}
它给出了与type1==type不同的答案


Scala是一种奇怪的野兽。

使用
=:=
来比较类型

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

根据你要做的事情,考虑一致类型可以有不同的表示:

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
scala>typeOf[List[Int]]
res11:reflect.runtime.universe.Type=scala.List[Int]
scala>type X=List[Int]
定义的类型别名X
scala>typeOf[X]。dealias
res12:reflect.runtime.universe.Type=List[scala.Int]
scala>res11=:=res12
res13:Boolean=true
scala>res11==res12
res14:布尔值=假
scala>typeOf[X]typeOf[X]=:=res11
res16:Boolean=true

No,您的
type2
是一个匹配任何内容的变量,与
type2@
相同。小写表示模式中的var。这是一个惯例的小缺点。使用反勾号来击败它。请参阅关于如何比较类型的其他问答。隐马尔可夫模型。。。搜索并错过了这个。谢谢。这是否意味着类型不应该用作映射的键?这取决于您正在做什么,但不要期望别名类型比较相等。也许这就是你想要的。通常,您对一致性感兴趣。