UnusedMethod is reported for JUnit @BeforeEach and for Java validation annotated methods
The problem observed with error-prone 2.10.0
The problem is reported for methods with well-known annotations like org.junit.jupiter.api.BeforeEach, javax.validation.constraints.AssertTrue and com.fasterxml.jackson.annotation.JsonProperty
The reported methods are indeed not used directly by our code, but by a dependency, like JUnit 5 or Jackson. It seems crude to annotate every such method with @SuppressWarnings("UnusedMethod")
There is a similar problem with "Unused variable" reported for class members annotated with org.springframework.boot.test.mock.mockito.MockBean.
Both of those checks have heuristics with lists of annotations they recognize as meaning the variable or method isn't actually unused:
- https://github.com/google/error-prone/blob/master/core/src/main/java/com/google/errorprone/bugpatterns/UnusedMethod.java#L93
- https://github.com/google/error-prone/blob/master/core/src/main/java/com/google/errorprone/bugpatterns/UnusedVariable.java#L129
I'm happy to take PRs adding annotations to those lists.
Also FYI the next release of Error Prone will have a @Keep annotation that can be applied to other annotations, which will suppress those warnings on annotated elements. Not all projects are going to want to or be able to depend on Error Prone's annotations, but it provides an extensible way to add this kind of suppression.