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.