Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resource annotations #55

Open
zsmb13 opened this issue Aug 7, 2017 · 1 comment
Open

Add resource annotations #55

zsmb13 opened this issue Aug 7, 2017 · 1 comment

Comments

@zsmb13
Copy link
Owner

zsmb13 commented Aug 7, 2017

This is something that's included (at least in parts) in the base library, and it would be nice to have here.

This issue will have to be dealt with once Android Studio 3.0 is stable, since as of Canary 9, checks for these annotations don't seem to work when calling the annotated methods from Kotlin.

  • Update: Android Studio 3 is now stable, but the issue still remains, misusing properties that have these annotations doesn't produce any lint warnings.

  • Update 2: here is an unanswered StackOverflow issue for the same problem.


Example of what needs to be done:

var background: Int
    @Deprecated(...)
    get() = nonReadable()
    set(@DrawableRes value) { // <- resource annotation
        builder.withHeaderBackground(value)
    }

Example of what Studio should mark as an error:

accountHeader {
    background = R.drawable.header // this is ok, this is the proper usage
    background = 1234 // should warn for this, but doesn't
}

Example of what Studio marks as a warning in Java as of 3.0 Canary 9:

AccountHeaderBuilderKt builder = new AccountHeaderBuilderKt(this);
builder.setBackground(25124); // Error: Expected resource of type drawable
@zsmb13
Copy link
Owner Author

zsmb13 commented Feb 6, 2018

Another update: as of AS 3.1 Beta 1, Java annotations generate lint warnings at Kotlin use sites, but resource annotations in Kotlin code still have no effect.

Example:

public T withDescription(@StringRes int descriptionRes) { ... }
public var descriptionRes: Int
        get() = ...
        set(@StringRes value) { ... }

image

Even though according to the decompiled bytecode of the Kotlin property, the annotations is applied properly:

public final void setDescriptionRes(@StringRes int value) { ... }

Seems like this only an issue with property setters, as function parameters annotated with these annotations in Kotlin code work as expected, for example, this generates lint warnings for hardcoded int values:

fun test(@StringRes value: Int) { ... }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant