如何禁用的Andr​​oid @IntDef注解检查在特殊情况下? [英] How do I disable Android @IntDef annotation checks in special cases?

查看:409
本文介绍了如何禁用的Andr​​oid @IntDef注解检查在特殊情况下?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个这样的情况下读取一个捆绑一个int,并存储成限制@IndDef标注变量:

One such case is reading an int from a Bundle and storing it into the variable restricted by @IndDef annotation:

public class MainActivity extends ActionBarActivity {

@IntDef({STATE_IDLE, STATE_PLAYING, STATE_RECORDING})
@Retention(RetentionPolicy.SOURCE)
public @interface State {}

public static final int STATE_IDLE = 0;
public static final int STATE_PLAYING = 1;
public static final int STATE_RECORDING = 2;

@MainActivity.State int fPlayerState = STATE_IDLE;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState != null)
        fPlayerState = savedInstanceState.getInt(BUNDLE_STATE); //Causes "Must be one of: ..." error

必须有燮$ P $的一些方法pssing支票或从int,以在最后一行设置变量强制转换为@ MainActivity.State INT。

There must be some way of suppressing the check or casting from int to @MainActivity.State int in order to set the variable in the last line.

另一种情况是编写一个负测试调用带注释的参数故意传递了错误的参数,以测试该异常被抛出在这种情况下的功能。必须有一种方法来燮preSS注释检查,以便编译这样的测试。

The other case is to write a negative test that calls a function with annotated parameter intentionally passing the wrong parameter in order to test that the Exception is thrown in such case. There must be a way to suppress annotation check in order to compile such test.

推荐答案

我发现晚饭preSS注解检查的方式。其实,有三个人:

I've found the way to suppress the annotation checks. Actually, there are three of them:

  1. 添加 @燮pressWarnings(的ResourceType)类的定义之前。在我的情况:

  1. Add @SuppressWarnings("ResourceType") before the definition of your class. In my case:

@SuppressWarnings("ResourceType")
public class MainActivity extends ActionBarActivity {
    ...
}

  • 添加 @燮pressWarnings(的ResourceType)你的方法定义之前。在我的情况:

  • Add @SuppressWarnings("ResourceType") before the definition of your method. In my case:

    @Override
    @SuppressWarnings("ResourceType")
    protected void onCreate(Bundle savedInstanceState) {
        ...
    }
    

  • 这两种方法不适合我,因为我想对我所有的code注释检查,但只是一个声明。

    These two approaches do not work for me, because I want annotation checks on all of my code, except for just one statement.

    要想喝preSS上一行检查添加一个特殊格式的注释(!)。

    1. To suppress a check on a single line add a specially formatted comment (!!!).

    //noinspection ResourceType
    fState = savedInstanceState.getInt(BUNDLE_STATE);
    

    这篇关于如何禁用的Andr​​oid @IntDef注解检查在特殊情况下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆