I am creating a custom annotation for firebase events. My inspiration comes from official android docs and other @StringDef
annotations. It looks like this:
@Retention(SOURCE)
@StringDef({
EVENT_1, EVENT_2
})
@Size(max = 32) // firebase limit
public @interface UserActivityEvents {
String EVENT_1 = "ev_1";
String EVENT_2 = "really_long_event_name_that_exceeds_32_chars_limit";
...
}
The class that encapsulates the logging is defined as follows:
public class UserActivityEvent {
private @UserActivityEvents String eventName;
private UserActivityEvent() {
}
public UserActivityEvent(@UserActivityEvents String eventName) {
this.eventName = eventName;
}
...
}
The problem is that the AS lint is never triggered, no matter what I specify in the constructor, even if I do something like this:
new UserActivityEvent("asd");
My expectations for @UserActivityEvents
are:
- The parameter in constructor of
UserActivityEvent
can only be one of the strings defined in @StringDef - Event length doesn't exceed the 32 chars limit
- If one of these rules are violated, the code is underlined (classic AS error highlight) and the error message is displayed when hovering over the code.
Edit: I found out that if I define a new event EVENT_3
in UserActivityEvents
but don't include it in @UserActivityEvents
and I try to reference it as new UserActivityEvent(UserActivityEvents.EVENT_3)
, the lint is properly triggered and says this: "Must be one of: EVENT_1, EVENT_2 or length must be at most 32". In case of using new UserActivityEvent("asd")
it doesn't work
@Size
annotation suppresses the@StringDef
annotation, so it's not possible to use both at once. It's not really an answer since it might actually be a bug or there is another workaround, so I am posting this as a comment.