Style Guide Proposal: IntDef NUM_ENTRIES constant

15 views
Skip to first unread message

Trevor Perrier

unread,
Aug 29, 2023, 6:37:42 PM8/29/23
to java
I ran into the WrongConstant and found Issue 1300585 but noticed that the style guide suggestion will still cause the lint error.

I have seen two proposals for fixing this:
  1. Add the NUM_ENTRIES value to the @IntDef
    • Requires adding NUM_ENTRIES to switch statements which isn't desirable.
  2. Add a public static final int <PREFIX>_NUM_ENTRIES below the interface.
    • Not quite as contained as including it in the interface but avoids the switch issue.
I've created https://crrev.com/c/4824407 to update the style guide with proposal two above. PLMK if this makes should be changed or can be submitted.

Thanks,

- Trevor

Peter Wen

unread,
Aug 30, 2023, 9:06:31 AM8/30/23
to Trevor Perrier, java
For what it's worth, option 2 is what Google3 goes with when I last investigated this issue.

Peter

--
You received this message because you are subscribed to the Google Groups "java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java+uns...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/java/1a5cd720-4e4b-4c2c-b451-de3bde857ae7n%40chromium.org.

Andrew Grieve

unread,
Aug 30, 2023, 9:47:10 AM8/30/23
to Peter Wen, Trevor Perrier, java
Yeah, it doesn't read as nicely, but I think it's the only style that avoids having to suppress lint warnings, so +1 to 2).


Nate Fischer

unread,
Aug 30, 2023, 1:44:54 PM8/30/23
to Andrew Grieve, Peter Wen, Trevor Perrier, java
I'm a little confused by crbug/1300585. My understanding is that this is a bug in Android Lint: the @IntDef isn't really defined by which constants are defined inside the interface, it's defined by which constants are passed into the annotation. If I don't pass NUM_ENTRIES into the annotation, why does Android Lint expect our code to handle it as if it's a valid value of that particular IntDef?

This seems to me like it's just a regression in Android Lint. If that's the case, then I think we should file a bug against the Android Lint project rather than changing our style guide to workaround this.

Nate Fischer | Software Engineer | ntf...@google.com



Peter Wen

unread,
Sep 1, 2023, 2:30:54 PM9/1/23
to Nate Fischer, Andrew Grieve, Trevor Perrier, java
Andrew filed http://b/298283135 (internal bug against Android Lint) so we'll see how that goes.

Andrew Grieve

unread,
Sep 21, 2023, 4:17:19 PM9/21/23
to java, Peter Wen, Andrew Grieve, Trevor Perrier, java, Nate Fischer
Happy to report that the bug is fixed, lint has rolled, and our baselines have been update to remove the false-positives.

Great outcome!

Nate Fischer

unread,
Sep 27, 2023, 8:07:04 PM9/27/23
to Andrew Grieve, java, Peter Wen, Andrew Grieve, Trevor Perrier
This is awesome news! Thanks for collaborating with the Lint team to get this fixed!


Nate Fischer | Software Engineer | ntf...@google.com


Reply all
Reply to author
Forward
0 new messages