`UnusedMethod` flags `@JsonValue` methods as unused
hisener opened this issue · comments
We have a couple of private static nested classes and enums with @JsonValue
methods, mostly in tests. The UnusedMethod
bug pattern flags those as unused, unlike the @JsonCreator
methods.
Steps to reproduce
JsonValueExample.java
import java.util.Locale;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
public class JsonValueExample {
private JsonValueExample() {}
private enum FooBar {
FOO,
BAR;
@JsonValue
String value() {
return name().toLowerCase(Locale.ROOT);
}
@JsonCreator
static FooBar of(String value) {
return valueOf(value.toUpperCase(Locale.ROOT));
}
}
}
curl -LO https://repo1.maven.org/maven2/com/google/errorprone/error_prone_core/2.13.1/error_prone_core-2.13.1-with-dependencies.jar
curl -LO https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-annotations/2.13.1/jackson-annotations-2.13.1.jar
javac \
-J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
-J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \
-J--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \
-J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED \
-cp jackson-annotations-2.13.1.jar \
-XDcompilePolicy=simple \
-processorpath error_prone_core-2.13.1-with-dependencies.jar \
'-Xplugin:ErrorProne -XepDisableAllChecks -Xep:UnusedMethod:ERROR' \
JsonValueExample.java
JsonValueExample.java:13: error: [UnusedMethod] Method 'value' is never used.
String value() {
^
(see https://errorprone.info/bugpattern/UnusedMethod)
Did you mean 'BAR'?
1 error
A potential solution would be adding com.fasterxml.jackson.annotation.JsonValue
to exempted annotations similar to JsonCreator
.
Let me know if that makes sense; I can create a PR, as well.
We have the same situation in some of our tests for the following annotations:
org.junit.jupiter.params.ParamaterizedTest
org.junit.jupiter.api.Test
org.junit.jupiter.api.RepeatedTest
For completeness we can also add the following JUnit 5 variants (the JUnit 4 equivalents are already listed):
org.junit.jupiter.api.BeforeAll
org.junit.jupiter.api.AfterAll
org.junit.jupiter.api.AfterEach
org.junit.jupiter.api.BeforeEach
And while we are at it, we could also make the JUnit 4 list complete by adding:
org.junit.Before
org.junit.After
org.junit.Test
One last thing to consider is that instead of the org.junit.jupiter.api.Test
annotation we could add support for the meta-annotation org.junit.platform.commons.annotation.Testable
. That annotation is used on both org.junit.jupiter.api.Test
and org.junit.jupiter.api.TestTemplate
. TestTemplate
is in turn also used in the org.junit.jupiter.api.RepeatedTest
annotation.
Supporting this meta annotation requires some more work, but we are also willing to help out.
What do you think of adding these as well?
I think there's an endless number of annotations that should be allow a method to be except from UnusedMethod
, so I think the better solution would be to allow the configuration of these annotations.