Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Support Annotations - How to use IntDef/StringDef (Typedef Annotations) with generics list?

Recently I started using android's support library IntDef/StringDef (Typedef Annotations). I followed the documentation in Android Tools Project Site and couldn't find there or in any other relevant tutorial how can I use IntDef/StringDef typedef annotations with generics array. for example, assuming I have the following code snippet:

public static final String MEDIA_IMAGE = "image";
public static final String MEDIA_TEXT = "text";
public static final String MEDIA_LINK = "link";
public static final String MEDIA_AUDIO = "audio";
public static final String MEDIA_VIDEO = "video";
public static final String MEDIA_VOICE = "voice";

@StringDef ({MEDIA_IMAGE, MEDIA_TEXT, MEDIA_LINK, MEDIA_AUDIO, MEDIA_VIDEO, MEDIA_VOICE})
@Retention(RetentionPolicy.SOURCE)
public @interface Media {}

I would like to do something similar to this using generics array and typedef annotations:

public List<@Media String> getMediaItems() {
    ...
}

public void setMediaItems(List<@Media String> mediaItems) {
    ...
}

However, that seems to be impossible that way.. Since the compiler says:

"Type annotations are not supported at this language level"

Any suggestions?

like image 693
Maxim Rahlis Avatar asked Jan 05 '16 17:01

Maxim Rahlis


2 Answers

If you try to do something like private List<@Media String> mList with JavaVersion.VERSION_1_7 you'll get a

Type annotation are not supported at this language level

Though you can do something like private @Media String[] mArray.
With JavaVersion.VERSION_1_8 private @Media List<String> mList will work.

I'll be honest, didn't run to test it, but no Lint error was given.

like image 200
GuilhE Avatar answered Nov 04 '22 14:11

GuilhE


There is no way you can annotate type parameters in Java - the language does not support this. The only thing you can do with type parameters - is make them bounded.

like image 20
aga Avatar answered Nov 04 '22 13:11

aga