diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-05-29 17:03:35 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-05-29 17:03:35 +0100 |
| commit | e97fdb3a32e6c5eaae65d50001a2156ff8752f22 (patch) | |
| tree | 67d368d3a1bcfba034b81b30bbe215a314e028cc /src | |
| parent | ec184c5b7baccdc77a3dab790709838256546d21 (diff) | |
Support non-sealed interfaces in Java AST
Diffstat (limited to 'src')
4 files changed, 43 insertions, 4 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java index 8f6bd48..867ba23 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java @@ -139,6 +139,7 @@ public class JavaTypesGenerator implements Generator { packageParts, new JavaInterfaceDeclaration( sumDefinition.name(), + JavaInterfaceDeclaration.Openness.SEALED, sumDefinition.variants().stream() .map(variant -> generateTypeRef(variant.type(), context)) .toList(), diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java index 3cf58ce..677b1a6 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java @@ -155,7 +155,15 @@ public class JavaWriter implements AutoCloseable { private void writeInterfaceDeclaration(JavaInterfaceDeclaration interfaceDeclaration) throws IOException { writeDocComment(interfaceDeclaration.docComment()); - this.writer.write("public sealed interface "); + this.writer.write("public "); + switch (interfaceDeclaration.openness()) { + case OPEN -> { + } + case SEALED -> { + this.writer.write("sealed "); + } + } + this.writer.write("interface "); this.writer.write(interfaceDeclaration.name()); this.writer.write(" "); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaInterfaceDeclaration.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaInterfaceDeclaration.java index d0bd0a1..a35de51 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaInterfaceDeclaration.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaInterfaceDeclaration.java @@ -6,9 +6,14 @@ import java.util.List; public record JavaInterfaceDeclaration( String name, + Openness openness, List<JavaTypeRef> permitsTypes, List<JavaClassBodyDeclaration> body, JavaCustomArea customArea, DocComment docComment ) implements JavaTypeDeclaration { + public enum Openness { + OPEN, + SEALED + } } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java index a53b20f..c349ec1 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java @@ -94,9 +94,31 @@ public class JavaWriterTests { } @Test - public void emptyInterfaceDeclaration() throws IOException { + public void openInterfaceDeclaration() throws IOException { var java = new JavaInterfaceDeclaration( "Shape", + JavaInterfaceDeclaration.Openness.OPEN, + List.of(), + List.of(), + new JavaCustomArea(JavaTypeRef.topLevel(List.of(), "Shape"), "body"), + DocComment.EMPTY + ); + + var string = write(writer -> writer.writeTypeDeclaration(java)); + + assertThat(string, equalTo(""" + public interface Shape { + // Custom area start: Shape body + // Custom area end: Shape body + }""" + )); + } + + @Test + public void sealedInterfaceDeclaration() throws IOException { + var java = new JavaInterfaceDeclaration( + "Shape", + JavaInterfaceDeclaration.Openness.SEALED, List.of(), List.of(), new JavaCustomArea(JavaTypeRef.topLevel(List.of(), "Shape"), "body"), @@ -117,6 +139,7 @@ public class JavaWriterTests { public void sealedInterfaceDeclarationWithPermitsTypes() throws IOException { var java = new JavaInterfaceDeclaration( "Shape", + JavaInterfaceDeclaration.Openness.SEALED, List.of( JavaTypeRef.topLevel(List.of("abc", "def"), "One"), JavaTypeRef.topLevel(List.of("abc", "def"), "Two") @@ -140,6 +163,7 @@ public class JavaWriterTests { public void interfaceDeclarationWithDocComment() throws IOException { var java = new JavaInterfaceDeclaration( "Shape", + JavaInterfaceDeclaration.Openness.OPEN, List.of(), List.of(), new JavaCustomArea(JavaTypeRef.topLevel(List.of(), "Shape"), "body"), @@ -150,7 +174,7 @@ public class JavaWriterTests { assertThat(string, equalTo(""" /// A 2D shape. - public sealed interface Shape { + public interface Shape { // Custom area start: Shape body // Custom area end: Shape body }""" @@ -161,6 +185,7 @@ public class JavaWriterTests { public void interfaceDeclarationWithBody() throws IOException { var java = new JavaInterfaceDeclaration( "Shape", + JavaInterfaceDeclaration.Openness.OPEN, List.of(), List.of( new JavaMethodDeclaration( @@ -185,7 +210,7 @@ public class JavaWriterTests { var string = write(writer -> writer.writeTypeDeclaration(java)); assertThat(string, equalTo(""" - public sealed interface Shape { + public interface Shape { public static void a() { } |
