scala反射场类

发布于 2024-12-14 19:02:31 字数 559 浏览 2 评论 0 原文

我试图获取引用自定义类的属性的类型,我只是得到它的类型是 Object

我的代码:

class Edge[N <% Node](var from : N, var to : N) {

  def toXml(c: Class): xml.Elem = {
    <edge>{
      for(field: Field <- classOf[this.type].getDeclaredFields)
        yield <field name={field.name} tpe={field.tpe.toString()}>{ this.getClass().getMethods.find(_.getName() == field.name).get.invoke(this) }</field>
    }</edge>
  }

所以这里的问题是我需要在 java 字段和 scala 字段之间切换:显然有scala 中没有 this.getClass 这样的东西吗?那么我需要通过Java来获取类吗? 然而,这似乎只会导致对象作为类型?

I'm trying to get the type of an attribute that refers to a custom class, I just get that it's of type Object

My code:

class Edge[N <% Node](var from : N, var to : N) {

  def toXml(c: Class): xml.Elem = {
    <edge>{
      for(field: Field <- classOf[this.type].getDeclaredFields)
        yield <field name={field.name} tpe={field.tpe.toString()}>{ this.getClass().getMethods.find(_.getName() == field.name).get.invoke(this) }</field>
    }</edge>
  }

So the problem here is that I need to switch between the java Field and scala Field: apparently there is no such thing as this.getClass in scala? So I need to go through Java to get the class?
However this seems to only result in Objects as types?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぃ双果 2024-12-21 19:02:31

编辑:修改后的问题是:应该使用 scala.reflect.Field 还是 java.lang.reflect.Field?

答案:总是[*]使用java.lang.reflect.Field,并且一般来说java反射有两个原因:

  1. 这就是xxx.getClass().getDeclaredFields()返回
  2. 的内容以下注释位于scala.reflect.Field

/** This type is required by the compiler and <b>should not be used in client code</b>. */
case class Field(override val fullname: String, tpe: Type) extends GlobalSymbol(fullname)

[*] 至少现在是这样。 Scala 即将推出反射。

--
原始答案:

如果您也发布了类代码,将会有所帮助,但该字段似乎被声明为 Object。 getType 返回字段的声明类。

来自 Field#getType( )

返回一个 Class 对象,该对象标识字段的声明类型
由此 Field 对象表示。

class Foo {
  var bar: String = "string"
  var bar2: java.lang.Object = "string"
}

for (field <- new Foo().getClass.getDeclaredFields())  {
   println("field=" + field.getName() + " " + field.getType.toString())
}

如果您想要

field=bar class java.lang.String
field=bar2 class java.lang.Object

实例的类型,那么您必须以正常方式在实例上执行 .getClass() 。

EDIT: The revised question is: Should scala.reflect.Field or java.lang.reflect.Field be used?

Answer: Always[*] use java.lang.reflect.Field, and in general java reflection for two reasons:

  1. That is what is returned by xxx.getClass().getDeclaredFields()
  2. The following comment is next to the definition of scala.reflect.Field

.

/** This type is required by the compiler and <b>should not be used in client code</b>. */
case class Field(override val fullname: String, tpe: Type) extends GlobalSymbol(fullname)

[*] At least for now. reflection is coming soon to Scala.

--
Original answer:

It would help if you posted the class code as well, but it seems that the field is declared as Object. getType returns the declaration class of the field.

From Field#getType():

Returns a Class object that identifies the declared type for the field
represented by this Field object.

class Foo {
  var bar: String = "string"
  var bar2: java.lang.Object = "string"
}

for (field <- new Foo().getClass.getDeclaredFields())  {
   println("field=" + field.getName() + " " + field.getType.toString())
}

gives

field=bar class java.lang.String
field=bar2 class java.lang.Object

If you want the type of the instance, then you will have to do a .getClass() on the instance in the normal way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文