Skip to content

Java 17 Sealed Classes #66

Description

@paulirwin

Support Java 17 Sealed Classes

https://docs.oracle.com/en/java/javase/17/language/sealed-classes-and-interfaces.html#GUID-0C709461-CC33-419A-82BF-61461336E65F

Note that we can only partially translate these for sealed classes, and it requires C# 15 coming out in .NET 11 with its Closed Hierarchies feature. We should do the following:

  1. For sealed interfaces, we have no alternative in C# at all. Emit as a non-sealed interface with a warning.
  2. For sealed classes, we should have an option as to whether to translate them as C# 15 closed classes. If so, we add the closed modifier to the translated class. Otherwise, we emit a warning. This option should be enabled by default.
  3. In both cases, we should add a comment on a line before the type definition outlining what the permits clause had in Java. Example below.

Example input:

publicsealedclassShape
permits Circle, Square, Rectangle {
}

Example output (C# 15 closed translation enabled):

// Java: sealed, permits Circle, Square, Rectanglepublicclosedclass Shape
{}

Example output (C# 15 closed translation disabled):

// Java: sealed, permits Circle, Square, RectanglepublicclassShape{}

Note that we also need to support the Java 17 non-sealed counterpart keyword. This keyword opens back up the hierarchy. We should translate this without emitting an equivalent keyword, with a comment added, and with a warning. i.e.:

Example input:

publicnon-sealedclassSquareextendsShape {
publicdoubleside;
}

Example output:

// Java: non-sealedpublicclassSquare:Shape{publicdoubleside;}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions