简体   繁体   English

Scala反射错误java

[英]scala reflection error java

I'm trying to use scala.reflect to get the class attributes and to write them XML. 我正在尝试使用scala.reflect获取类属性并将其写入XML。 However I'm getting a strange error 但是我遇到一个奇怪的错误

 def toXml(): xml.Elem = {
<node>{
  for(field: scala.reflect.Field <- getClass().getDeclaredFields()) {
    val tmpString = "<" + field.name + ">" + this.getClass().getMethods.find(_.getName == field.name).get.invoke(this) + "</" + field.name + ">"
    print(tmpString)
  }
 }</node>
}

The error: 错误:

error: type mismatch;
found   : scala.reflect.Field => Unit
required: java.lang.reflect.Field => ?
for(field: scala.reflect.Field <- getClass().getDeclaredFields()) {

So even if I explicitly use scala.reflect.Field, it still is viewed as java.lang.reflect.Field? 因此,即使我显式使用scala.reflect.Field,它仍被视为java.lang.reflect.Field?

getClass().getDeclaredFields() returns java.lang.reflect.Field objects. getClass().getDeclaredFields()返回java.lang.reflect.Field对象。 Unless you find a way to convert between these two classes, you cannot declare them as scala.reflect.Field and expect them to work. 除非找到在这两个类之间进行转换的方法,否则不能将它们声明为scala.reflect.Field并期望它们起作用。

EDIT: fix for your code: 编辑:修复您的代码:

for(field: java.lang.reflect.Field <- getClass().getDeclaredFields()) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM