diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-06-12 15:43:02 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-06-12 15:43:02 +0100 |
| commit | deee2d3f92e3f09b711e1fc68a25129bc554df2a (patch) | |
| tree | 71319a012b885f33430e81628881986617bd6efd | |
| parent | 4455f5d14c3f80d13758f10e726e422d183b5f31 (diff) | |
Add implementsTypes to JavaClassDeclaration
4 files changed, 117 insertions, 12 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javapreciselymatchers/JavaPreciselyMatchersGenerator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javapreciselymatchers/JavaPreciselyMatchersGenerator.java index cbdcc90..b999deb 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javapreciselymatchers/JavaPreciselyMatchersGenerator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javapreciselymatchers/JavaPreciselyMatchersGenerator.java @@ -8,7 +8,6 @@ import org.zwobble.hobgoblin.compiler.ast.typed.TypedSumDefinitionNode; import org.zwobble.hobgoblin.compiler.config.OutputConfig; import org.zwobble.hobgoblin.compiler.output.generators.Generator; import org.zwobble.hobgoblin.compiler.output.generators.java.JavaGenerator; -import org.zwobble.hobgoblin.compiler.output.generators.javatypes.JavaTypesGenerator; import org.zwobble.hobgoblin.compiler.output.lang.java.ast.*; import org.zwobble.hobgoblin.compiler.typechecker.TypesInfo; import org.zwobble.hobgoblin.compiler.types.*; @@ -18,7 +17,6 @@ import java.io.IOException; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; -import java.util.Optional; public class JavaPreciselyMatchersGenerator implements Generator { public static final String NAME = "java-precisely-matchers"; @@ -94,6 +92,7 @@ public class JavaPreciselyMatchersGenerator implements Generator { return new JavaClassDeclaration( matcherTypeName, List.of(), + List.of(), JavaCustomArea.type(javaTypeRef, "body"), DocComment.EMPTY ); 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 3b7987b..87da849 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 @@ -7,6 +7,7 @@ import org.zwobble.hobgoblin.compiler.output.lang.java.ast.*; import java.io.IOException; import java.io.StringWriter; import java.nio.file.Path; +import java.util.List; public class JavaWriter implements AutoCloseable { private static final String LINE_COMMENT_START = "//"; @@ -93,7 +94,11 @@ public class JavaWriter implements AutoCloseable { this.writer.write("public class "); this.writer.write(classDeclaration.name()); - this.writer.write(" {"); + this.writer.write(" "); + + this.writeImplements(classDeclaration.implementsTypes()); + + this.writer.write("{"); this.writer.indent(); for (var bodyDeclaration: classDeclaration.body()) { @@ -157,6 +162,18 @@ public class JavaWriter implements AutoCloseable { } } + private void writeImplements(List<JavaTypeRef> implementsTypes) throws IOException { + if (!implementsTypes.isEmpty()) { + this.writer.write("implements "); + writeWithSeparator( + implementsTypes, + this::writeTypeRef, + () -> writer.write(", ") + ); + writer.write(" "); + } + } + private void writeIntegerLiteral(JavaIntegerLiteral integerLiteral) throws IOException { this.writer.write(Long.toString(integerLiteral.value())); } @@ -286,15 +303,7 @@ public class JavaWriter implements AutoCloseable { writer.write(") "); - if (!recordDeclaration.implementsTypes().isEmpty()) { - this.writer.write("implements "); - writeWithSeparator( - recordDeclaration.implementsTypes(), - this::writeTypeRef, - () -> writer.write(", ") - ); - writer.write(" "); - } + this.writeImplements(recordDeclaration.implementsTypes()); this.writer.write("{"); this.writer.indent(); diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaClassDeclaration.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaClassDeclaration.java index 4e6c4b7..7a099ef 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaClassDeclaration.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaClassDeclaration.java @@ -6,6 +6,7 @@ import java.util.List; public record JavaClassDeclaration( String name, + List<JavaTypeRef> implementsTypes, List<JavaClassBodyDeclaration> body, JavaCustomArea customArea, DocComment docComment 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 798de28..53a3e65 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 @@ -23,6 +23,7 @@ public class JavaWriterTests { new JavaClassDeclaration( "Rectangle", List.of(), + List.of(), new JavaCustomArea(JavaTypeRef.topLevel(List.of(), "Rectangle"), "body"), DocComment.EMPTY ) @@ -48,6 +49,7 @@ public class JavaWriterTests { var java = new JavaClassDeclaration( "Rectangle", List.of(), + List.of(), new JavaCustomArea(JavaTypeRef.topLevel(List.of(), "Rectangle"), "body"), DocComment.EMPTY ); @@ -63,9 +65,55 @@ public class JavaWriterTests { } @Test + public void classDeclarationImplementingSingleInterface() throws IOException { + var java = new JavaClassDeclaration( + "Rectangle", + List.of( + JavaTypeRef.topLevel(List.of("com.example"), "Shape") + ), + List.of(), + new JavaCustomArea(JavaTypeRef.topLevel(List.of(), "Rectangle"), "body"), + DocComment.EMPTY + ); + + var string = write(writer -> writer.writeTypeDeclaration(java)); + + assertThat(string, equalTo(""" + public class Rectangle implements com.example.Shape { + // Custom area start: Rectangle body + // Custom area end: Rectangle body + }""" + )); + } + + @Test + public void classDeclarationImplementingMultipleInterfaces() throws IOException { + var java = new JavaClassDeclaration( + "Rectangle", + List.of( + JavaTypeRef.topLevel(List.of("com.example"), "Shape"), + JavaTypeRef.topLevel(List.of("com.example"), "Drawable") + ), + List.of(), + new JavaCustomArea(JavaTypeRef.topLevel(List.of(), "Rectangle"), "body"), + DocComment.EMPTY + ); + + var string = write(writer -> writer.writeTypeDeclaration(java)); + + assertThat(string, equalTo(""" + public class Rectangle implements com.example.Shape, com.example.Drawable { + // Custom area start: Rectangle body + // Custom area end: Rectangle body + }""" + )); + } + + @Test public void classDeclarationWithMethods() throws IOException { var java = new JavaClassDeclaration( "Rectangle", + List.of(), List.of( new JavaMethodDeclaration( "a", @@ -107,6 +155,7 @@ public class JavaWriterTests { var java = new JavaClassDeclaration( "Point", List.of(), + List.of(), new JavaCustomArea(JavaTypeRef.topLevel(List.of(), "Point"), "body"), new DocComment("A 2D point.") ); @@ -296,6 +345,53 @@ public class JavaWriterTests { } @Test + public void recordDeclarationImplementingOneInterface() throws IOException { + var java = new JavaRecordDeclaration( + "Rectangle", + List.of(), + List.of( + JavaTypeRef.topLevel(List.of("com.example"), "Shape") + ), + List.of(), + new JavaCustomArea(JavaTypeRef.topLevel(List.of(), "Rectangle"), "body"), + DocComment.EMPTY + ); + + var string = write(writer -> writer.writeTypeDeclaration(java)); + + assertThat(string, equalTo(""" + public record Rectangle() implements com.example.Shape { + // Custom area start: Rectangle body + // Custom area end: Rectangle body + }""" + )); + } + + @Test + public void recordDeclarationImplementingMultipleInterfaces() throws IOException { + var java = new JavaRecordDeclaration( + "Rectangle", + List.of(), + List.of( + JavaTypeRef.topLevel(List.of("com.example"), "Shape"), + JavaTypeRef.topLevel(List.of("com.example"), "Drawable") + ), + List.of(), + new JavaCustomArea(JavaTypeRef.topLevel(List.of(), "Rectangle"), "body"), + DocComment.EMPTY + ); + + var string = write(writer -> writer.writeTypeDeclaration(java)); + + assertThat(string, equalTo(""" + public record Rectangle() implements com.example.Shape, com.example.Drawable { + // Custom area start: Rectangle body + // Custom area end: Rectangle body + }""" + )); + } + + @Test public void recordDeclarationWithMethods() throws IOException { var java = new JavaRecordDeclaration( "Rectangle", |
