summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-07-31 11:14:00 +0100
committerMichael Williamson <mike@zwobble.org>2026-07-31 11:14:00 +0100
commit69ab5f074540a550393806de1a32d70d1c895121 (patch)
treeda8cd3c36332ac0a1aaf10d374e6fa87b5635cd5 /src/main
parent20f60d8b72a27655abef227d3a213946d61c2370 (diff)
Use class for singleton structs
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java38
1 files changed, 28 insertions, 10 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 e8ecaa7..e1aca6d 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
@@ -118,10 +118,12 @@ public class JavaTypesGenerator implements Generator {
);
}
- private JavaRecordDeclaration generateStructDefinition(
+ private JavaTypeDeclaration generateStructDefinition(
TypedStructDefinitionNode structDefinition,
Context context
) {
+ var javaTypeRef = generateTypeRef(structDefinition.type(), context);
+
var components = structDefinition.fields().orElse(List.of()).stream()
.map(field -> new JavaRecordComponent(
generateTypeRef(field.type(), context),
@@ -148,13 +150,20 @@ public class JavaTypesGenerator implements Generator {
body.add(new JavaFieldDeclaration(
JavaVisibility.PUBLIC,
JavaMemberKind.STATIC,
- this.generateTypeRef(structDefinition.type(), context),
+ javaTypeRef,
JavaGenerator.INSTANCE_FIELD_NAME,
Optional.of(new JavaNewExpression(
- generateTypeRef(structDefinition.type(), context),
+ javaTypeRef,
List.of()
))
));
+
+ body.add(new JavaConstructorDeclaration(
+ javaTypeRef.typeNames().getLast(),
+ JavaVisibility.PRIVATE,
+ List.of(),
+ new JavaBlock(List.of())
+ ));
}
body.add(new JavaMethodDeclaration(
@@ -175,13 +184,22 @@ public class JavaTypesGenerator implements Generator {
context
));
- return new JavaRecordDeclaration(
- generateTypeRef(structDefinition.type(), context),
- components,
- implementsTypes,
- body,
- structDefinition.docComment()
- );
+ if (structDefinition.fields().isPresent()) {
+ return new JavaRecordDeclaration(
+ javaTypeRef,
+ components,
+ implementsTypes,
+ body,
+ structDefinition.docComment()
+ );
+ } else {
+ return new JavaClassDeclaration(
+ javaTypeRef,
+ implementsTypes,
+ body,
+ structDefinition.docComment()
+ );
+ }
}
private JavaRecordDeclaration generateStructDefinitionBuilder(