summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-08-10 10:22:57 +0100
committerMichael Williamson <mike@zwobble.org>2026-08-10 10:22:57 +0100
commitdb1ae9e80a9b330f399f5bdbff8ee402d41a9f0e (patch)
tree4fe8a2227d149d6b74e8dc80d732e86217e7d107 /src/main
parent5113f2157c5aa257ad7ebf555cb6a551701d169a (diff)
Extract singleton generation methods
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java41
1 files changed, 24 insertions, 17 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 3dcbd61..709ef12 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
@@ -148,23 +148,8 @@ public class JavaTypesGenerator implements Generator {
var body = new ArrayList<JavaClassBodyDeclaration>();
if (structDefinition.fields().isEmpty()) {
- body.add(new JavaFieldDeclaration(
- JavaVisibility.PUBLIC,
- JavaMemberKind.STATIC,
- javaTypeRef,
- JavaGenerator.INSTANCE_FIELD_NAME,
- Optional.of(new JavaNewExpression(
- javaTypeRef,
- List.of()
- ))
- ));
-
- body.add(new JavaConstructorDeclaration(
- javaTypeRef.typeNames().getLast(),
- JavaVisibility.PRIVATE,
- List.of(),
- new JavaBlock(List.of())
- ));
+ body.add(generateStructSingletonInstanceField(javaTypeRef));
+ body.add(generateStructSingletonConstructor(javaTypeRef));
}
body.add(new JavaMethodDeclaration(
@@ -203,6 +188,28 @@ public class JavaTypesGenerator implements Generator {
}
}
+ private static JavaFieldDeclaration generateStructSingletonInstanceField(JavaTypeRef javaTypeRef) {
+ return new JavaFieldDeclaration(
+ JavaVisibility.PUBLIC,
+ JavaMemberKind.STATIC,
+ javaTypeRef,
+ JavaGenerator.INSTANCE_FIELD_NAME,
+ Optional.of(new JavaNewExpression(
+ javaTypeRef,
+ List.of()
+ ))
+ );
+ }
+
+ private static JavaConstructorDeclaration generateStructSingletonConstructor(JavaTypeRef javaTypeRef) {
+ return new JavaConstructorDeclaration(
+ javaTypeRef.typeNames().getLast(),
+ JavaVisibility.PRIVATE,
+ List.of(),
+ new JavaBlock(List.of())
+ );
+ }
+
private JavaRecordDeclaration generateStructDefinitionBuilder(
TypedStructDefinitionNode structDefinition,
List<JavaRecordComponent> components,