【问题标题】:An annotation can't be used as the annotations argument注释不能用作注释参数
【发布时间】:2020-05-31 08:32:42
【问题描述】:

我得到了一个错误,虽然做的和文档说的完全一样。 Documentation:

data class PlaylistWithSongs(
    @Embedded val playlist: Playlist,
    @Relation(
         parentColumn = "playlistId",
         entityColumn = "songId",
         associateBy = @Junction(PlaylistSongCrossRef::class)
    )
    val songs: List<Song>
)

我的问题:

data class FileEntryWithTags(
        @Embedded val fileEntry: FileEntry,
        @Relation(
                parentColumn = FileEntry.COLUMN_UUID,
                entityColumn = Tag.COLUMN_ID,
                associateBy = @Junction(FileEntryTagCrossRef::class)
        )
        val tags: List<Tag>
)

【问题讨论】:

  • 非常有趣的问题 - 按照这个来查看解决方案,看看这是否是开发者文档中的错误。
  • @Ryan 这是 Android 文档中的一个错误。 Kotlin 参考明确指出,@ 不应在此上下文中作为注解名称的前缀。

标签: android kotlin android-room


【解决方案1】:

Android 文档中似乎有错误。 Kotlin 参考资料中的Annotations - Kotlin Programming Language 页面告诉我们:

如果一个注解作为另一个注解的参数,它的名称不以@字符为前缀:

annotation class ReplaceWith(val expression: String)

annotation class Deprecated(
        val message: String,
        val replaceWith: ReplaceWith = ReplaceWith(""))

@Deprecated("This function is deprecated, use === instead", ReplaceWith("this === other"))

所以你的代码应该是:

data class FileEntryWithTags(
        @Embedded val fileEntry: FileEntry,
        @Relation(
                parentColumn = FileEntry.COLUMN_UUID,
                entityColumn = Tag.COLUMN_ID,
                associateBy =  Junction(FileEntryTagCrossRef::class)
        )
        val tags: List<Tag>
)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-14
  • 1970-01-01
  • 1970-01-01
  • 2017-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多