@IntDef注解和无法注解的其他代码的返回值或如何暂时禁用注解影响代码? [英] @IntDef annotation and return value from other's code that cannot be annotated or how to temporarily disable annotation from affecting the code?
问题描述
我正在使用 IntDef 来自 Android 支持注释 在我的代码中(但我的问题范围更广所以请继续阅读 :) 像这样:
I am using IntDef from Android Support annotation in my code (but my question is wider in scope so please keep reading :) like this:
public class UiLockMode
{
@IntDef({DEFAULT, NONE, VISIBLE, TRANSPARENT})
@Retention(RetentionPolicy.SOURCE)
public @interface AllowedValues {}
public static final int DEFAULT = 0;
public static final int NONE = 1;
public static final int VISIBLE = 2;
public static final int TRANSPARENT = 3;
}
接下来,我用它注释了一些其他方法:
Next, I got some other methods annotated with it like this:
protected void setLockMode(@UiLockMode.AllowedValues int lockMode) {
...
那时一切都很好,但是每当我想将其他方法的返回值传递给 setLockMode()
时就会出现问题,例如来自 Parcelable 实现:
At that point is all fine and nice but the problem shows up whenever I'd like to pass return value from other methods to setLockMode()
, like i.e. from Parcelable implementation:
private Foo(Parcel in) {
...
setLockMode(in.getInt());
在这种情况下,我的 IDE 抱怨我只能将 DEFAULT、NONE、VISIBLE、TRANSPARENT
与 setLockMode()
一起使用.但是 getInt()
不是我的方法,所以我不能注释它的返回值并让这一切变得快乐.我也几乎可以肯定这不是唯一的用例,但我没有找到方法来临时禁用 AllowedValues
注释从这里抱怨或转换"来自 getInt()
的返回值code> 使 AllowedValue
不抱怨.
In such case my IDE complains that I am only allowed to use DEFAULT, NONE, VISIBLE, TRANSPARENT
with setLockMode()
. But getInt()
is not my method so I cannot annotate its return value and make all this happy. I am also almost sure this is not unique use case but I failed to find the way to either temporarily disable AllowedValues
annotation from complaining here or to "cast" return value from getInt()
to make AllowedValue
not complaining.
所以我的问题是:有没有办法解决这个问题?也许我遗漏了一些明显的注释,但也许我会创建错误报告来让 Google 解决这个问题?
So my questions are: is there any way of solving this problem? Maybe I am missing something obvious about annotations but maybe I shall be creating bug report to make Google address this problem instead?
感谢任何输入或想法.
推荐答案
您可以通过使用以下注释来抑制方法的 IntDef
Lint 警告:
You can suppress IntDef
Lint warnings for a method by annotating it with the following:
@SuppressWarnings("ResourceType")
您还可以为单个语句和整个类禁用这些警告 - 请参阅 Android 工具站点 获取更多文档.
You can also disable these warnings for individual statements and whole classes - see the Android Tools site for further documentation.
这篇关于@IntDef注解和无法注解的其他代码的返回值或如何暂时禁用注解影响代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!