Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An annotation can't be used as the annotations argument

I get a error, although do exactly the same that documentation says. Documentation:

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

My problem:

Error

data class FileEntryWithTags(
        @Embedded val fileEntry: FileEntry,
        @Relation(
                parentColumn = FileEntry.COLUMN_UUID,
                entityColumn = Tag.COLUMN_ID,
                associateBy = @Junction(FileEntryTagCrossRef::class)
        )
        val tags: List<Tag>
)
like image 521
Andrew Churilo Avatar asked May 31 '20 08:05

Andrew Churilo


People also ask

What is use of annotation in Kotlin?

In Java, an annotation type is a form of an interface, so you can implement it and use an instance. As an alternative to this mechanism, Kotlin lets you call a constructor of an annotation class in arbitrary code and similarly use the resulting instance.

What is @GET annotation in Kotlin?

By prefixing the annotation with @get: the resulting bytecode will have the annotation on the generated getter function: // Decompiled from the resulting Kotlin Bytecode @SomeAnnotation @Nullable public final String getSomeProperty() { return this. someProperty; }

What is the use of @annotated methods?

annotated methods will get called for each HTTP request if you don’t specify the value element of this annotation. The value element can be a single or multiple form names or request parameters that the init binder method is applied to. This annotation is used on fields. The annotation is a meta annotation that indicates a web mapping annotation.

How to add a class as an argument to an annotation?

If you need to specify a class as an argument of an annotation, use a Kotlin class ( KClass ). The Kotlin compiler will automatically convert it to a Java class, so that the Java code can access the annotations and arguments normally. Copied!

Can Anan annotation be used as the annotations argument?

An annotation can't be used as the annotations argument. I wonder: how come (what I think is) the same concept working in Java but not in Kotlin? Also, is there a way to go around this?

Can you use an annotation as the annotations argument in Kotlin?

Kotlin doesn't let you do that. You get the error 'Kotlin: An annotation can't be used as the annotations argument' Well hell, we have annotations which require you do exactly that. Here is the fragment giving us problems


Video Answer


1 Answers

Looks like the Android documentation has a mistake in it. The Annotations - Kotlin Programming Language page from the Kotlin reference tells us:

If an annotation is used as a parameter of another annotation, its name is not prefixed with the @ character:

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"))

So your code should be:

data class FileEntryWithTags(
        @Embedded val fileEntry: FileEntry,
        @Relation(
                parentColumn = FileEntry.COLUMN_UUID,
                entityColumn = Tag.COLUMN_ID,
                associateBy =  Junction(FileEntryTagCrossRef::class)
        )
        val tags: List<Tag>
)
like image 98
Slaw Avatar answered Oct 17 '22 20:10

Slaw