Enums and android annotation intDef

AliSh picture AliSh · Aug 16, 2015 · Viewed 25.9k times · Source

I have an enum:

public enum AppEnums {
    SERVICE_ERROR,
    CONNECTION_ERROR;
}

and I want to use it in an intDef of Android Annotation:

@IntDef({AppEnums.CONNECTION_ERROR, AppEnums.SERVICE_ERROR})
public @interface ServiceErrors {
}

error shows:

incompatible types found, required: 'long'

What I can do with this incompatibility?

I don't want to handle values of AppEnum parameters manually, Enum create values automatically ordinarily. AppEnums.CONNECTION_ERROR.ordinal() return int value of enum parameter but don't work here.

Answer

Oleksii K. picture Oleksii K. · Feb 7, 2016

The main idea of IntDef annotation is to use set of int constants like an enum, but without enum. In this case you have to declare all constants manually.

@IntDef({Status.IDLE, Status.PROCESSING, Status.DONE, Status.CANCELLED})
@Retention(RetentionPolicy.SOURCE)
@interface Status {
    int IDLE = 0;
    int PROCESSING = 1;
    int DONE = 2;
    int CANCELLED = 3;
}

You can see detailed example here.