Java: s3985 false positive for @JsonValue

Product: SonarQube Community (self-hosted)
sonar-java version: sonar-java 8.28.0.43176 on SonarQube Community Build 26.4.0.121862
Java source level: 21 (javac 21, source/target 17)

Rule

java:S3985 — “Unused private classes should be removed”

Description

S3985 inconsistently handles Jackson-based usages of private nested types: enums referenced via @JsonCreator are recognized as used, while equivalent usage through @JsonValue is incorrectly reported as unused.

Reproducer

package demo;

import java.util.Locale;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

public class JsonValueExample {
  private JsonValueExample() {}

  // Not flagged — @JsonCreator is recognized as a framework usage.
  private enum FooBarOk {
    FOO, BAR;

    @JsonCreator
    static FooBarOk of(String value) {
      return valueOf(value.toUpperCase(Locale.ROOT));
    }
  }

  // Flagged by S3985 as "unused private class", even though @JsonValue
  // makes this enum reachable from {NAME} exactly like the one above.
  private enum FooBarBad {
    FOO, BAR;

    @JsonValue
    String value() {
      return name().toLowerCase(Locale.ROOT);
    }
  }
}

Hi @Emilyaxe ,

About this rule: it flags classes / enums which symbols aren’t used in code. It doesn’t take into account any annotations, only actual references.

In your examples, the reason of the first example not flagged is that it has self-referencing method of(value), so semantic model sees that it’s used. In the second example enum is described but not referenced, that’s why the issue is raised. When you’ll use this enum somewhere in your project’s code, the issue will disappeared.

Let me know if you have any questions.