Skip to content

ClassCanBeStatic: Exclude JUnit @Nested classes#3654

Closed
ljrmorgan wants to merge 2 commits intogoogle:masterfrom
ljrmorgan:junit_nested_test_classes_cannot_be_static
Closed

ClassCanBeStatic: Exclude JUnit @Nested classes#3654
ljrmorgan wants to merge 2 commits intogoogle:masterfrom
ljrmorgan:junit_nested_test_classes_cannot_be_static

Conversation

@ljrmorgan
Copy link
Copy Markdown
Contributor

Exclude inner classes annotated with JUnit's @Nested annotation from the ClassCanBeStatic check. JUnit requires them to not be static.

From the JUnit docs:

Only non-static nested classes (i.e. inner classes) can serve as @Nested test classes.

Fixes #956.

@Stephan202
Copy link
Copy Markdown
Contributor

Nice one. In our code base I find quite a number of these:

@Nested
@SuppressWarnings("ClassCanBeStatic" /* @Nested classes cannot be static. */)

@cushon
Copy link
Copy Markdown
Collaborator

cushon commented Jan 8, 2023

Thanks, LGTM overall.

We've been starting to consolidate more of the heuristics for situations where Error Prone shouldn't change modifiers etc. by using @Keep, or treating annotations as if they were annotated with @Keep.

* Indicates that the annotated element should not be removed, and that its visibility, modifiers,
* type, and name should not be modified.

What do you think about using ASTHelpers.shoudlKeep here, and adding @Nested to this list:

private static final ImmutableSet<String> ANNOTATIONS_CONSIDERED_KEEP =
ImmutableSet.of("org.apache.beam.sdk.transforms.DoFn.ProcessElement");

From the [JUnit docs](https://junit.org/junit5/docs/current/user-guide/#writing-tests-nested):

> Only non-static nested classes (i.e. inner classes) can serve as
  `@Nested` test classes.
Replace the check for the `@Nested` annotation with a check for `@Keep`,
and consider `@Nested` to be `@Keep`-annotated.
@ljrmorgan ljrmorgan force-pushed the junit_nested_test_classes_cannot_be_static branch from c13015f to 3a5bd67 Compare January 10, 2023 14:01
@ljrmorgan
Copy link
Copy Markdown
Contributor Author

@cushon thank you for taking a look at the PR, and for the suggestion!

I think that makes sense - the JavaDoc for @Keep made me think initially of checks like UnusedMethod etc., but I suppose the only reason that @Nested classes can't be static is because of how they are used via reflection, which fits the description of @Keep. I guess my only concern is that we might lose some valid ClassCanBeStatic warnings on types that happen to be annotated with @Keep, but you are much better placed than I am to say whether that is a legitimate concern 😁

I've pushed that change as a separate commit, I'm happy to squash the two commits into one if you'd prefer, just let me know.

Thanks!

@ljrmorgan
Copy link
Copy Markdown
Contributor Author

Hi @cushon, I wondered if you'd had a chance to take a look at the latest changes? Thanks!

copybara-service bot pushed a commit that referenced this pull request Jan 23, 2023
Exclude inner classes annotated with JUnit's `@Nested` annotation from the `ClassCanBeStatic` check. JUnit requires them to not be `static`.

From the [JUnit docs](https://junit.org/junit5/docs/current/user-guide/#writing-tests-nested):

> Only non-static nested classes (i.e. inner classes) can serve as `@Nested` test classes.

Fixes #956.

Fixes #3654

FUTURE_COPYBARA_INTEGRATE_REVIEW=#3654 from ljrmorgan:junit_nested_test_classes_cannot_be_static 3a5bd67
PiperOrigin-RevId: 503544057
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.

Exclude inner classes annotated with @Nested from ClassCanBeStatic rule

3 participants