Skip to content

Update sources to satisfy a new Error Prone check.#2887

Merged
eamonnmcmanus merged 2 commits intogoogle:mainfrom
eamonnmcmanus:epfix
Aug 1, 2025
Merged

Update sources to satisfy a new Error Prone check.#2887
eamonnmcmanus merged 2 commits intogoogle:mainfrom
eamonnmcmanus:epfix

Conversation

@eamonnmcmanus
Copy link
Copy Markdown
Member

The latest Error Prone release includes a new check EffectivelyPrivate, which flags class members that are public or protected when the enclosing class isn't. This is only a warning in Error Prone, but because we treat warnings as errors it currently breaks the build.

Nearly all cases were in private nested classes in tests. For the most part the members didn't need to be public. In a couple of places they were accessed through reflection in the test, and it was enough to change getField to getDeclaredField (etc). (getField only retrieves public fields.) I don't believe any of these tests depended on the public members for the correctness of what they were testing. Although getField works when the target field is public, regardless of the visibility of the containing class, actually reading or writing a field requires both the field and its containing class to be public unless Field.setAccessible(true) is called, and likewise for constructors and methods.

The latest Error Prone release includes a new check
`EffectivelyPrivate`, which flags class members that are public or
protected when the enclosing class isn't. This is only a warning in
Error Prone, but because we treat warnings as errors it currently breaks
the build.

Nearly all cases were in private nested classes in tests. For the
most part the members didn't need to be public. In a couple of places
they were accessed through reflection in the test, and it was enough
to change `getField` to `getDeclaredField` (etc). (`getField` only
retrieves public fields.) I don't believe any of these tests depended on
the public members for the correctness of what they were testing.
Although `getField` works when the target field is public, regardless of
the visibility of the containing class, actually reading or writing a
field requires both the field and its containing class to be public
unless `Field.setAccessible(true)` is called, and likewise for
constructors and methods.
@eamonnmcmanus eamonnmcmanus requested a review from cpovirk August 1, 2025 15:35
@eamonnmcmanus eamonnmcmanus self-assigned this Aug 1, 2025

private static class Foo {
@SuppressWarnings("unused")
@SuppressWarnings({"unused", "EffectivelyPrivate"})
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option is @Keep, but I don't have much reason to prefer one over the other:

  • @Keep would also satisfy Error Prone's unused-field checking, though AFAIK not any other tools' similar checking. (So it's unclear whether we'd be wise to remove "unused" at that point.) It also prevents Error Prone from suggesting things like making fields final, which may be good or bad in various cases.
  • @Keep has runtime retention, which is likely irrelevant here.
  • @Keep isn't applicable to method parameters, but that is likely irrelevant here, too.
  • @Keep can be used as a meta-annotation, but that's not relevant here.

So this is fine as it is as far as I'm concerned, as are the other suppressions.

@eamonnmcmanus eamonnmcmanus merged commit 8c23cd3 into google:main Aug 1, 2025
13 checks passed
@eamonnmcmanus eamonnmcmanus deleted the epfix branch August 1, 2025 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants