SonarSource/sonarqube · error · IllegalArgumentException

Only level 1, 2, 3 and 4 are supported

Error message

Only level 1, 2, 3 and 4 are supported

What it means

PlatformLevelPredicates validates @PlatformLevel annotations and only accepts levels 1-4. Passing any other int to checkSupportedLevel throws IllegalArgumentException (with object context appended when available).

Solutions

  1. Use a platform level between 1 and 4
  2. Use the PlatformLevel enum constants instead of raw ints
  3. Review the annotated object named in the appended error message

Example fix

// before
Predicate<Object> p = PlatformLevelPredicates.hasPlatformLevel(5);
// after
Predicate<Object> p = PlatformLevelPredicates.hasPlatformLevel(4);
Defensive patterns

Strategy: validation

Validate before calling

if (level < 1 || level > 4) {
  throw new IllegalArgumentException("Platform level must be 1-4, got: " + level);
}
Predicate<Object> p = PlatformLevelPredicates.hasPlatformLevel(level);

Try / catch

try {
  return PlatformLevelPredicates.hasPlatformLevel(level);
} catch (IllegalArgumentException e) {
  LOG.warn("Invalid platform level, defaulting to 1", e);
  return PlatformLevelPredicates.hasPlatformLevel(1);
}

Prevention

When it happens

Trigger: Using PlatformLevelPredicates.hasPlatformLevel(n) or hasPlatformLevel4OrNone with n outside 1-4, or an annotated object carrying an out-of-range level value.

Common situations: Custom predicates built with a hardcoded level like 0 or 5; refactors that changed level enum ordinals; incorrect custom annotation processing.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09). Data as JSON: /api/errors/0b397429ba9de1c2. Report an issue: GitHub.

Appendix: source

Thrown at sonar-core/src/main/java/org/sonar/core/extension/PlatformLevelPredicates.java:45

  private PlatformLevelPredicates() {
    // prevents instantiation
  }

  public static Predicate<Object> hasPlatformLevel(int i) {
    checkSupportedLevel(i, null);
    return o -> {
      PlatformLevel platformLevel = AnnotationUtils.getAnnotation(o, PlatformLevel.class);
      return platformLevel != null && checkSupportedLevel(platformLevel.value(), o) == i;
    };
  }

  private static int checkSupportedLevel(int i, @Nullable Object annotatedObject) {
    boolean supported = i >= 1 && i <= 4;
    if (supported) {
      return i;
    }

    throw new IllegalArgumentException(buildErrorMsgFrom(annotatedObject));
  }

  private static String buildErrorMsgFrom(@Nullable Object annotatedObject) {
    String baseErrorMsg = "Only level 1, 2, 3 and 4 are supported";
    if (annotatedObject == null) {
      return baseErrorMsg;
    } else if (annotatedObject instanceof Class classAnnotatedObject) {
      return String.format("Invalid value for annotation %s on class '%s'. %s",
        PlatformLevel.class.getName(), classAnnotatedObject.getName(),
        baseErrorMsg);
    } else {
      return String.format("Invalid value for annotation %s on object of type %s. %s",
        PlatformLevel.class.getName(), annotatedObject.getClass().getName(), baseErrorMsg);
    }
  }

  public static Predicate<Object> hasPlatformLevel4OrNone() {
    return o -> {

View on GitHub (pinned to 184c821202)