Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I disable Android @IntDef annotation checks in special cases?

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 

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.

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.

like image 594
Nicol Eye Avatar asked Dec 14 '14 21:12

Nicol Eye


People also ask

What are android annotations and what are they used for?

Annotations allow you to provide hints to code inspections tools like Lint, to help detect these more subtle code problems. They are added as metadata tags that you attach to variables, parameters, and return values to inspect method return values, passed parameters, local variables, and fields.

What is IntDef?

public annotation IntDef. Denotes that the annotated element of integer type, represents a logical type and that its value should be one of the explicitly named constants. If the IntDef#flag() attribute is set to true, multiple constants can be combined. Example: @Retention(SOURCE)


2 Answers

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

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

    @SuppressWarnings("ResourceType") public class MainActivity extends ActionBarActivity {     ... } 
  2. Add @SuppressWarnings("ResourceType") before the definition of your method. In my case:

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

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

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

    //noinspection ResourceType fState = savedInstanceState.getInt(BUNDLE_STATE); 
like image 163
Nicol Eye Avatar answered Sep 28 '22 07:09

Nicol Eye


@Status int state1=bundle.getInt(STATE_ELEMENT1); setStatus1(state1); //instead of direct setStatus1(bundle.getInt(STATE_ELEMENT1); 
like image 26
Andrew Glukhoff Avatar answered Sep 28 '22 06:09

Andrew Glukhoff