I'm trying to use scala.reflect to get the class attributes and to write them 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?
getClass().getDeclaredFields()
returns java.lang.reflect.Field
objects. Unless you find a way to convert between these two classes, you cannot declare them as scala.reflect.Field
and expect them to work.
EDIT: fix for your code:
for(field: java.lang.reflect.Field <- getClass().getDeclaredFields()) {
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.