From 89fea0eb309e943a01d45aa5bcc2cb1901d3b1c7 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 18 May 2026 19:16:03 +0100 Subject: Extract context for JavaTypesGenerator --- .../generators/javatypes/JavaTypesGenerator.java | 54 ++++++++++++++-------- 1 file changed, 36 insertions(+), 18 deletions(-) (limited to 'src/main') 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 82ef0cd..42839c9 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 @@ -64,21 +64,7 @@ public class JavaTypesGenerator implements Generator { private List generateNamespace(TypedNamespaceNode namespace) { var packageParts = namespaceToJavaPackageParts(namespace.namespaceName()); - var javaClassToImplementsType = new HashMap>(); - for (var statement : namespace.body()) { - if (statement instanceof TypedSumDefinitionNode sumDefinition) { - for (var variant : sumDefinition.variants()) { - // TODO: handle not struct types - var variantTypeName = ((StructType) variant.type().value()).name(); - javaClassToImplementsType.putIfAbsent( - variantTypeName, - new ArrayList<>() - ); - javaClassToImplementsType.get(variantTypeName) - .add(generateTypeRef(sumDefinition.type())); - } - } - } + var context = contextForNamespace(namespace); var javaCompilationUnits = new ArrayList(); @@ -87,7 +73,7 @@ public class JavaTypesGenerator implements Generator { case TypedStructDefinitionNode structDefinition -> { var javaCompilationUnit = new JavaCompilationUnit( packageParts, - generateStructDefinition(structDefinition, javaClassToImplementsType) + generateStructDefinition(structDefinition, context) ); javaCompilationUnits.add(javaCompilationUnit); @@ -115,7 +101,7 @@ public class JavaTypesGenerator implements Generator { private JavaRecordDeclaration generateStructDefinition( TypedStructDefinitionNode structDefinition, - HashMap> javaClassToImplementsType + Context context ) { var components = structDefinition.fields().stream() .map(field -> new JavaRecordComponent( @@ -127,7 +113,7 @@ public class JavaTypesGenerator implements Generator { return new JavaRecordDeclaration( structDefinition.name(), components, - javaClassToImplementsType.getOrDefault(structDefinition.name(), List.of()), + context.implementsType(structDefinition.type()), List.of( new JavaRecordDeclaration( "Builder", @@ -188,4 +174,36 @@ public class JavaTypesGenerator implements Generator { throw new UnsupportedOperationException("TODO"); }; } + + private Context contextForNamespace(JavaTypesGenerator this, TypedNamespaceNode namespace) { + var javaClassToImplementsType = new HashMap>(); + for (var statement : namespace.body()) { + if (statement instanceof TypedSumDefinitionNode sumDefinition) { + for (var variant : sumDefinition.variants()) { + // TODO: handle not struct types + var variantTypeName = ((StructType) variant.type().value()).name(); + javaClassToImplementsType.putIfAbsent( + variantTypeName, + new ArrayList<>() + ); + javaClassToImplementsType.get(variantTypeName) + .add(generateTypeRef(sumDefinition.type())); + } + } + } + + return new Context(javaClassToImplementsType); + } + + private static class Context { + private final HashMap> javaClassToImplementsType; + + public Context(HashMap> javaClassToImplementsType) { + this.javaClassToImplementsType = javaClassToImplementsType; + } + + public List implementsType(StructType type) { + return this.javaClassToImplementsType.getOrDefault(type.name(), List.of()); + } + } } -- cgit v1.2.3