From ae55e6e9f177ac8505298abc0839d82d90dddb38 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 29 May 2026 17:08:55 +0100 Subject: Support Java method without body --- .../compiler/output/lang/java/JavaWriter.java | 20 ++++++++++++-------- .../output/lang/java/ast/JavaMethodDeclaration.java | 18 +++++++++++++++++- .../compiler/output/lang/java/JavaWriterTests.java | 17 +++++++++++++++++ 3 files changed, 46 insertions(+), 9 deletions(-) (limited to 'src') 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 677b1a6..7dbcdfc 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 @@ -229,17 +229,21 @@ public class JavaWriter implements AutoCloseable { () -> writer.write(", ") ); - this.writer.write(") {"); - this.writer.indent(); + this.writer.write(")"); - if (!method.body().isEmpty()) { + if (method.body().isEmpty()) { + this.writer.write(";"); + } else { + this.writer.write(" {"); + this.writer.indent(); + if (!method.body().get().isEmpty()) { + this.writer.newLine(); + writeBlock(method.body().get()); + } + this.writer.dedent(); this.writer.newLine(); - writeBlock(method.body()); + this.writer.write("}"); } - - this.writer.dedent(); - this.writer.newLine(); - this.writer.write("}"); } private void writeNewExpression(JavaNewExpression newExpression) throws IOException { diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclaration.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclaration.java index f38625f..454b41f 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclaration.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclaration.java @@ -1,12 +1,28 @@ package org.zwobble.hobgoblin.compiler.output.lang.java.ast; import java.util.List; +import java.util.Optional; public record JavaMethodDeclaration( String name, JavaMethodKind kind, JavaTypeRef returnType, List params, - JavaBlock body + Optional body ) implements JavaClassBodyDeclaration { + public JavaMethodDeclaration( + String name, + JavaMethodKind kind, + JavaTypeRef returnType, + List params, + JavaBlock body + ) { + this( + name, + kind, + returnType, + params, + Optional.of(body) + ); + } } 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 c349ec1..fd282f9 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 @@ -308,6 +308,23 @@ public class JavaWriterTests { )); } + @Test + public void emptyMethodWithoutBody() throws IOException { + var java = new JavaMethodDeclaration( + "a", + JavaMethodKind.INSTANCE, + JavaTypeRef.VOID, + List.of(), + Optional.empty() + ); + + var string = write(writer -> writer.writeMethodDeclaration(java)); + + assertThat(string, equalTo(""" + public void a();""" + )); + } + @Test public void emptyInstanceMethod() throws IOException { var java = new JavaMethodDeclaration( -- cgit v1.2.3