English 中文(简体)
如何配对“豆类”类和“蓝色”类?
原标题:How to match classes of "boolean" types and "Boolean" types?

考虑以下法典:

object U { def foo(s:String) = true }
val boolType = Class.forName("java.lang.Boolean")    
val retType = U.getClass.getMethods.find(_.getName == "foo").get.getReturnType
boolType == retType // evaluates to false (*)
println (boolType) // prints "class java.lang.Boolean"
println (retType) // prints "boolean"

I would like retType to match with boolType in the line marked (*). How do I automatically equate classes of boxed and unboxed types?

[Edit:] I don t think this is the best solution, but one way is to make the comparison retType.getCanonicalName == "boolean"

[Edit2:] The context: I am writing some code to automatically invoke a method based on a form name. The code should extract the return types etc from the method and return the appropriate answer. As an example, the following snippet is used:

object someObject {}
val validTypes:Array[Class[_]] = Array(Class.forName("java.lang.String"), 
                              someObject.getClass, 
                              Class.forName("java.lang.Boolean"))
object U { def foo(s:String) = true } // can contain more methods 

def getRetType(name:String) = 
  U.getClass.getMethods.find(_.getName == name).get.getReturnType

println ("Type is "+(if (validTypes.contains(getRetType("foo"))) "valid" else "invalid"))
最佳回答

When Java reflection wants to represent a primitive return type, it uses Class instances that are not the same as the wrapper classes. So in Java, a boolean return type is represented by a java.lang.Boolean.TYPE (which in Java is also accessible as boolean.class, and in Scala as classOf[Boolean]).

因此,你想要

scala> U.getClass.getMethods.find(_.getName == "foo").get.getReturnType ==
     java.lang.Boolean.TYPE
res7: Boolean = true

Edit :我猜测,如果与regmOf[Boolean]比较,那将是比较较少的JRV特定解决办法。

问题回答

On the JVM, java.lang.Boolean is the reference type. Your routine returns a scala Boolean, which is java primitive boolean. That one is not a class in the JVM. Its type is java.lang.Boolean.TYPE, not classOf[Boolean] (java.lang.Boolean.class in java), which is what you get with Class.forName("java.lang.Boolean").

I think your only solution is to have an explicit mapping. Since you ask how to "(automatically) equate classes of boxed and unboxed types", I show an elegant way to define a comparison function.

首先,而不是<代码>Class.forName。 如在编造时知道该类型,可使用<条码>。 借助这一点,你可以确定对箱式类型中未加框框框的勘测:

import java.{lang => jl}
val map = Map[Class[_], Class[_]](classOf[Boolean] -> classOf[jl.Boolean],
  classOf[Int] -> classOf[jl.Integer]) //XXX add other entries

然后,你可以确定比较功能:

def cmp(lhs: Class[_], rhs: Class[_]) =
  //Canonicalize before comparing
  map.getOrElse(lhs, lhs) == map.getOrElse(rhs, rhs)

并测试:

scala> cmp(classOf[Boolean], classOf[jl.Boolean])
cmp(classOf[Boolean], classOf[jl.Boolean])
res13: Boolean = true
scala> cmp(classOf[Boolean], classOf[jl.Integer])
cmp(classOf[Boolean], classOf[jl.Integer])
res16: Boolean = false

澄清<条码>代谢/代码”和<条码>Boolean之间的关系。 缩略语

scala> classOf[java.lang.Boolean] == java.lang.Boolean.TYPE
res7: Boolean = false

scala> classOf[Boolean] == java.lang.Boolean.TYPE
res8: Boolean = true




相关问题
c# reflection with dynamic class

I need to execute a method "FindAll" in my page. This method returns a list of the object. This is my method that I execute "FindAll". FindAll requires an int and returns an List of these class. ...

Performance overhead of using attributes in .NET

1.. Is there any performance overhead caused by the usage of attributes? Think for a class like: public class MyClass { int Count {get;set;} } where it has 10 attibutes (...

WPF GridView, Display GetType().Name in DisplayMemberBinding

I want include the Type name of each object in my collection from my GridView. I have a collection which has four different types in it that all derive from a base class. Call them, Foo, Bar, Fizz, ...

Testing private method of an abstract class using Reflection

How can I test a private method of an abstract class using reflection (using C#)? I am specifically interested in adapting the code found in this thread. I am aware of the discussion around the ...

Adding Items to ListBox, RadioList, Combobox using reflection

I m trying to add items to a listbox,combobox, radiolist using reflection. The code I have at the moment is as follows: public static Control ConfigureControl(Control control, ControlConfig ctrlconf)...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

热门标签