Type safety using IntDef

安稳与你 提交于 2020-01-05 05:31:10

问题


I am trying to replace some of the enumerations in my source with IntDef annotation. I have been following this documentation.

I have a variable for holding a ViewMode which was previously an enumeration. Now I have changed it to some thing like below.

@Retention(RetentionPolicy.SOURCE)
@IntDef({ViewMode.VIEW_MODE_LIST_VIEW, ViewMode.VIEW_MODE_CARD_VIEW})
public @interface ViewMode {
    int VIEW_MODE_LIST_VIEW = 0;
    int VIEW_MODE_CARD_VIEW = 1;
}

@ViewMode
public int currentViewMode = ViewMode.VIEW_MODE_LIST_VIEW;

Now to test whether this is safe or not I have done the following in a method

this.currentViewMode = 987; //currentViewMode should be 0 or 1. Nothing else.

But this is now not giving me a compilation error. Am I missing something here?


回答1:


You will not get a compilation error, because the enumerated annotations are just lint checks: see Improve Your Code with Lint

You should see the error-marker directly in Android Studio or when you run android lint checks from the command line: see Improve Your Code with Lint

Note: you can also configure your build to automatically run the lint checks:
see SO: Run lint when building android studio projects
But running the linter takes some time, so you may choose to run it only for your release builds or only on your CI server.



来源:https://stackoverflow.com/questions/47096278/type-safety-using-intdef

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!