summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-05-29 17:08:55 +0100
committerMichael Williamson <mike@zwobble.org>2026-05-29 17:08:55 +0100
commitae55e6e9f177ac8505298abc0839d82d90dddb38 (patch)
tree0489672942a321b0491d0afe9d55862298b9e4ed
parente97fdb3a32e6c5eaae65d50001a2156ff8752f22 (diff)
Support Java method without body
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java20
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclaration.java18
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java17
3 files changed, 46 insertions, 9 deletions
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<JavaParam> params,
- JavaBlock body
+ Optional<JavaBlock> body
) implements JavaClassBodyDeclaration {
+ public JavaMethodDeclaration(
+ String name,
+ JavaMethodKind kind,
+ JavaTypeRef returnType,
+ List<JavaParam> 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
@@ -309,6 +309,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(
"a",