26

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.

2 Answers 2

52

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);
    
1
  • 1
    Wouldn't a type safe way to get an @IntDef back out of a bundle be to do a switch on it and assign the final result based on the values? e.g. "state = getInt(); switch (state) case 1: fState = STATE_IDLE ..." The down side is that it moves the verification from compile time to run-time, which was the whole point of @IntDef to begin with. (The result isn't particularly maintainable, either, if you add values frequently. ) You could also do a secondary lookup table to mimic the result of "Enum.values()".
    – lilbyrdie
    Sep 2, 2015 at 18:52
5
@Status int state1=bundle.getInt(STATE_ELEMENT1);
setStatus1(state1);
//instead of direct setStatus1(bundle.getInt(STATE_ELEMENT1);
1
  • I guess this does not check whether the value is valid, so use only if you know it must be a @Status in your case. upvoted though, especially since OP asked to not be bothered by the warning Jun 27, 2017 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.