Skip to content

UnusedMethod flags @JsonValue methods as unused #3144

Closed
@hisener

Description

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.

"com.fasterxml.jackson.annotation.JsonCreator",

Let me know if that makes sense; I can create a PR, as well.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions