Skip to content

Sealed classes - error: expected token: 'sealed'; generated class instead #603

Description

@ZacSweers

When trying to use GJF with JDK 16, I get the following error

Caused by: com.google.googlejavaformat.java.FormatterException: 8:52: error: expected token: 'sealed'; generated class instead
        at com.google.googlejavaformat.java.Formatter.getFormatReplacements(Formatter.java:293)
        at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:267)
        at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:233)
        ... 135 more

With this source code

package dev.zacsweers.moshix.sealed.sample.java;

import com.squareup.moshi.JsonClass;

import dev.zacsweers.moshix.sealed.annotations.TypeLabel;

public interface FailureTestCases {
  @JsonClass(generateAdapter = false, generator = "sealed:type")
  sealed class DuplicateLabels permits DuplicateLabels.TypeA, DuplicateLabels.TypeB {
    @TypeLabel(label = "a")
    static final class TypeA extends DuplicateLabels {}

    @TypeLabel(label = "a")
    static final class TypeB extends DuplicateLabels {}
  }

  @JsonClass(generateAdapter = false, generator = "sealed:type")
  sealed class DuplicateAlternateLabels permits DuplicateAlternateLabels.TypeA, DuplicateAlternateLabels.TypeB {
    @TypeLabel(label = "a", alternateLabels = {"aa"})
    static final class TypeA extends DuplicateAlternateLabels {}

    @TypeLabel(label = "b", alternateLabels = {"aa"})
    static final class TypeB extends DuplicateAlternateLabels {}
  }

  @JsonClass(generateAdapter = false, generator = "sealed:type")
  sealed class GenericSubtypes<T> permits GenericSubtypes.TypeA, GenericSubtypes.TypeB {
    // This form is ok
    @TypeLabel(label = "a", alternateLabels = {"aa"})
    static final class TypeA extends GenericSubtypes<String> {}

    // This form is not ok
    @TypeLabel(label = "b", alternateLabels = {"aa"})
    static final class TypeB<T> extends GenericSubtypes<T> {}
  }
}

I know sealed classes are still in preview though, so understand if this isn't expected to be supported yet. The error message is a little surprising though

Activity

  1. kevinb9n commented on Jun 14, 2021

    @kevinb9n
    Contributor

    We'd like to support preview features -- if not by formatting them well, that at least through the magic of not crashing.

    If for some reason this is a nontrivial fix (?), we might at least fix the error message first.

  2. added a commit that references this issue on Jul 23, 2021
    c186e3d
  3. added a commit that references this issue on Sep 13, 2024
    cc8b96f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

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