I'm using a XML parser/generator 'simple-xml' that I use on Android.
我正在使用一个我在Android上使用的XML解析器/生成器‘Simple-XML’。
Since, I have been updated my model from Java to Kotlin, I had an issue with my library. After debbugging, I find out that an Annotation Element
wasn't working well.
自从我将我的模型从Java更新到Kotlin之后,我的库就出现了问题。解除错误后,我发现Annotation元素工作不正常。
'@param:' annotations could be applied only to primary constructor parameters
Here is the code in Kotlin, this compile well but shows up error at runtime when using the library.
这是用Kotlin编写的代码,编译得很好,但在运行时使用库时会显示错误。
@Entity(indices = [Index("id")])
@Root
@Order(elements = ["nom", "date"])
class Lot {
/**
* Constructeur Lot
*
* @param nom String nom du lot
* @param date Date Date du lot
*/
constructor(nom: String?, date: Date?) {
this.nom = nom
this.date = date
}
private constructor() // For Room
@JvmField
@PrimaryKey(autoGenerate = true) // For Room
var id = 0
@JvmField
@Element
var nom: String? = null
@JvmField
@Element
var date: Date? = null
This version doesn't compile :
此版本无法编译:
@JvmField
@field:Element(name = "date", required = true)
@param:Element(name = "date", required = true)
var date: Date? = null
I don't know how to turn out the problem, because a need an empty constructor for Room and one with all the parameters but without id.
我不知道如何解决这个问题,因为需要为Room创建一个空的构造函数,并且需要一个具有所有参数但没有id的构造函数。
What are the solutions ?
解决方案是什么?
更多回答
@param
is not an annotation, it's a Javadoc / KDoc thing. It's not affecting the actual compiled code in any way.
@param不是一个批注,而是一个Javadoc/KDOC的东西。它不会以任何方式影响实际编译的代码。
But why, when it's explicited on the @Element
annotations, that show up this error on compilation time, when it's omitted, it shows up the error on runtime ?
但是,当它在@Element注释上被解释时,为什么在编译时显示这个错误,而当它被省略时,它在运行时显示错误?
There is no @Element
annotation here, nor any @param:
annotation in this code. Which annotation is causing the error you get? Which file/line of code is causing the error?
这里没有@Element注释,代码中也没有@param:注释。您收到的错误是哪个注释造成的?是哪个文件/哪行代码导致了错误?
date
properties ? param:
has been omitted in this code but I can edit to add the other version that doesn't compile
日期属性?Param:在此代码中省略了,但我可以进行编辑以添加未编译的其他版本
Well, the error message is very explicit that it allows to document params only in the primary constructor. So for me it sounds like we should have a primary constructor with params and secondary no-arg constructor. Why the opposite?
错误消息非常明确,它只允许在主构造函数中记录参数。所以对我来说,听起来我们应该有一个带参数的主构造函数和一个次要的无参数构造函数。为什么会相反呢?
Remove all Order
and Element
annotations and set Transient
on the id
field if the order of tags does not matter:
如果标签的顺序无关紧要,则删除所有顺序和元素注释,并在id字段上设置Temporent:
import org.simpleframework.xml.Transient
...
@JvmField
@PrimaryKey(autoGenerate = true) // For Room
@field:Transient
var id = 0
...
Otherwise, using @field:Element
without @param:<...>
should be sufficient:
否则,使用@field:Element而不使用@param:<...>就足够了:
@Entity(indices = [Index("id")])
@Root
@Order(elements = ["nom", "date"])
class Lot {
/**
* Constructeur Lot
*
* @param nom String nom du lot
* @param date Date Date du lot
*/
constructor(nom: String?, date: Date?) {
this.nom = nom
this.date = date
}
private constructor() // For Room
@JvmField
@PrimaryKey(autoGenerate = true) // For Room
var id = 0
@JvmField
@field:Element
var nom: String? = null
@JvmField
@field:Element
var date: Date? = null
更多回答
我是一名优秀的程序员,十分优秀!