Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alias an Annotation that takes an argument

I'm trying to convert my Android library to a Kotlin multiplatform library.

One of the things I want to preserve are all the android specific annotations for Android Lint. I was able to convert most of them by doing simple things like

@MustBeDocumented
@Retention(AnnotationRetention.BINARY)

@Target(
    AnnotationTarget.FUNCTION,
    AnnotationTarget.PROPERTY_GETTER,
    AnnotationTarget.PROPERTY_SETTER,
    AnnotationTarget.CONSTRUCTOR,
    AnnotationTarget.ANNOTATION_CLASS,
    AnnotationTarget.CLASS,
    AnnotationTarget.VALUE_PARAMETER
)
expect annotation class MainThread()

actual typealias MainThread = androidx.annotation.MainThread

This did not work with RestrictTo because it takes an argument.

The android RestrictTo annotation looks like

@Retention(CLASS)
@Target({ANNOTATION_TYPE,TYPE,METHOD,CONSTRUCTOR,FIELD,PACKAGE})
public @interface RestrictTo {

    /**
     * The scope to which usage should be restricted.
     */
    Scope[] value();

    enum Scope {
    }
}

I cannot seem to make the compiler happy with the type for value.

If I make the expect look like

@Target(
    AnnotationTarget.FUNCTION,
    AnnotationTarget.PROPERTY_GETTER,
    AnnotationTarget.PROPERTY_SETTER,
    AnnotationTarget.FIELD,
    AnnotationTarget.CONSTRUCTOR,
    AnnotationTarget.ANNOTATION_CLASS,
    AnnotationTarget.CLASS
)
@MustBeDocumented
@Retention(AnnotationRetention.BINARY)
expect annotation class RestrictTo(vararg val value: RestrictScope)

I get a compile error

public expect final val value: Array<out RestrictScope /* = RestrictTo.Scope */>

The following declaration is incompatible because return type is different:
    public final val value: Array<RestrictTo.Scope>

If I change the value from a vararg to an Array I get this error.

public constructor RestrictTo(value: Array<RestrictScope /* = RestrictTo.Scope */>)

The following declaration is incompatible because parameter types are different:
    public constructor RestrictTo(vararg value: RestrictTo.Scope)

Is there anyway to make the types work for both the constructor and the values method?

like image 505
Michael Krussel Avatar asked Jan 27 '20 20:01

Michael Krussel


1 Answers

This is a bug - https://youtrack.jetbrains.com/issue/KT-20900

Feel free to upvote the issue. enter image description here

like image 71
vanyochek Avatar answered Sep 29 '22 18:09

vanyochek