Consider this class:
public class MyClassOfMystery {
public static final int NO_FLAGS = ~0;
public static final int FIRST_FLAG = 1;
public static final int SECOND_FLAG = 1 << 1;
public static final int THIRD_FLAG = 1 << 2;
public static final int FOURTH_FLAG = 1 << 3;
@Retention(RetentionPolicy.SOURCE)
@IntDef(flag = true, value = {NO_FLAGS, FIRST_FLAG, SECOND_FLAG, THIRD_FLAG, FOURTH_FLAG})
public @interface MysteryFlags { }
... set flags, get flags, and use flags stuff.
}
I have often created something like this, and found that it would be useful to be able to iterate through all flags available in MysteryFlags
.
Can I iterate through my values set in MysteryFlags
?
This is what I have tried:
This printed ANNOTATION: @java.lang.annotation.Retention(value=SOURCE)
:
for (Annotation annotation : Flag.class.getAnnotations()) {
Log.d(TAG, String.format("ANNOTATION: %s", String.valueOf(annotation)));
}
This threw NPE on a null Array access
for (ExtraAction enm : Flag.class.getEnumConstants()) {
Log.d(TAG, String.format("ENUM: %s", String.valueOf(enm)));
}
These did not print anything out:
for (Field field : Flag.class.getFields()) {
Log.d(TAG, String.format("FIELD: %s", String.valueOf(field)));
}
and
for (Class<?> aClass : ExtraAction.class.getClasses()) {
Log.d(TAG, String.format("CLASS: %s", String.valueOf(aClass)));
}
I know I can just add the values to an array and iterate through that, but that requires storing another array. It is what I have done, but still wonder if there is a better way.
NO_FLAGS = 0
? – Cleft~
– Multicellular