From aa725a130816f954b914e638381862d20d556af5 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 12 Jun 2026 15:59:59 +0100 Subject: Add visibility to Java methods --- .../JavaPreciselyMatchersGenerator.java | 3 ++ .../generators/javatypes/JavaTypesGenerator.java | 7 +++- .../compiler/output/lang/java/JavaWriter.java | 12 +++++- .../lang/java/ast/JavaMethodDeclaration.java | 3 ++ .../output/lang/java/ast/JavaVisibility.java | 6 +++ .../compiler/output/lang/java/JavaWriterTests.java | 49 ++++++++++++++++++++++ 6 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaVisibility.java (limited to 'src') diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javapreciselymatchers/JavaPreciselyMatchersGenerator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javapreciselymatchers/JavaPreciselyMatchersGenerator.java index 46d68ef..4b26b08 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javapreciselymatchers/JavaPreciselyMatchersGenerator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javapreciselymatchers/JavaPreciselyMatchersGenerator.java @@ -101,6 +101,7 @@ public class JavaPreciselyMatchersGenerator implements Generator { List.of( new JavaMethodDeclaration( "match", + JavaVisibility.PUBLIC, JavaMethodKind.INSTANCE, JavaTypeRef.topLevel(List.of("org", "zwobble", "precisely"), "MatchResult"), List.of(new JavaParam(JavaTypeRef.OBJECT, "actual")), @@ -116,6 +117,7 @@ public class JavaPreciselyMatchersGenerator implements Generator { ), new JavaMethodDeclaration( "describe", + JavaVisibility.PUBLIC, JavaMethodKind.INSTANCE, JavaTypeRef.topLevel(List.of("org", "zwobble", "precisely"), "TextTree"), List.of(), @@ -131,6 +133,7 @@ public class JavaPreciselyMatchersGenerator implements Generator { ), new JavaMethodDeclaration( "toMatcher", + JavaVisibility.PUBLIC, JavaMethodKind.INSTANCE, JavaTypeRef.topLevelGeneric(List.of("org", "zwobble", "precisely"), "Matcher", List.of(JavaTypeRef.OBJECT)), List.of(), 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 2b56e87..56f5659 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 @@ -6,14 +6,12 @@ import org.zwobble.hobgoblin.compiler.builtins.NativeTypes; import org.zwobble.hobgoblin.compiler.config.OutputConfig; import org.zwobble.hobgoblin.compiler.output.generators.Generator; import org.zwobble.hobgoblin.compiler.output.generators.java.JavaGenerator; -import org.zwobble.hobgoblin.compiler.output.lang.java.JavaWriter; import org.zwobble.hobgoblin.compiler.output.lang.java.ast.*; import org.zwobble.hobgoblin.compiler.parser.Parser; import org.zwobble.hobgoblin.compiler.typechecker.TypesInfo; import org.zwobble.hobgoblin.compiler.types.*; import org.zwobble.json5.reader.Json5ObjectReader; -import java.io.File; import java.io.IOException; import java.nio.file.Path; import java.util.*; @@ -142,6 +140,7 @@ public class JavaTypesGenerator implements Generator { List.of( new JavaMethodDeclaration( "build", + JavaVisibility.PUBLIC, JavaMethodKind.INSTANCE, generateTypeRef(sumDefinition.type(), context), List.of(), @@ -193,6 +192,7 @@ public class JavaTypesGenerator implements Generator { List.of( new JavaMethodDeclaration( "arbitrary", + JavaVisibility.PUBLIC, JavaMethodKind.STATIC, builderJavaTypeRef, List.of(), @@ -224,6 +224,7 @@ public class JavaTypesGenerator implements Generator { var body = new ArrayList(); body.add(new JavaMethodDeclaration( "build", + JavaVisibility.PUBLIC, JavaMethodKind.INSTANCE, generateTypeRef(structDefinition.type(), context), List.of(), @@ -240,6 +241,7 @@ public class JavaTypesGenerator implements Generator { body.add(new JavaMethodDeclaration( withMethodName, + JavaVisibility.PUBLIC, JavaMethodKind.INSTANCE, builderJavaTypeRef, List.of( @@ -258,6 +260,7 @@ public class JavaTypesGenerator implements Generator { if (fieldBuilderTypeRef.isPresent()) { body.add(new JavaMethodDeclaration( withMethodName, + JavaVisibility.PUBLIC, JavaMethodKind.INSTANCE, builderJavaTypeRef, List.of( 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 d7a5325..ec4d70f 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 @@ -236,7 +236,9 @@ public class JavaWriter implements AutoCloseable { } void writeMethodDeclaration(JavaMethodDeclaration method) throws IOException { - this.writer.write("public "); + writeVisibility(method.visibility()); + this.writer.write(" "); + switch (method.kind()) { case STATIC -> { this.writer.write("static "); @@ -244,6 +246,7 @@ public class JavaWriter implements AutoCloseable { case INSTANCE -> { } } + writeTypeRef(method.returnType()); this.writer.write(" "); this.writer.write(method.name()); @@ -386,6 +389,13 @@ public class JavaWriter implements AutoCloseable { } } + private void writeVisibility(JavaVisibility visibility) throws IOException { + this.writer.write(switch (visibility) { + case PUBLIC -> "public"; + case PRIVATE -> "private"; + }); + } + private static void writeWithSeparator( Iterable iterable, WriteElement writeElement, 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 454b41f..dc5954c 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 @@ -5,6 +5,7 @@ import java.util.Optional; public record JavaMethodDeclaration( String name, + JavaVisibility visibility, JavaMethodKind kind, JavaTypeRef returnType, List params, @@ -12,6 +13,7 @@ public record JavaMethodDeclaration( ) implements JavaClassBodyDeclaration { public JavaMethodDeclaration( String name, + JavaVisibility visibility, JavaMethodKind kind, JavaTypeRef returnType, List params, @@ -19,6 +21,7 @@ public record JavaMethodDeclaration( ) { this( name, + visibility, kind, returnType, params, diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaVisibility.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaVisibility.java new file mode 100644 index 0000000..f5c9915 --- /dev/null +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaVisibility.java @@ -0,0 +1,6 @@ +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +public enum JavaVisibility { + PUBLIC, + PRIVATE +} 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 65fa2a7..96166b0 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 @@ -117,6 +117,7 @@ public class JavaWriterTests { List.of( new JavaMethodDeclaration( "a", + JavaVisibility.PUBLIC, JavaMethodKind.INSTANCE, JavaTypeRef.VOID, List.of(), @@ -124,6 +125,7 @@ public class JavaWriterTests { ), new JavaMethodDeclaration( "b", + JavaVisibility.PUBLIC, JavaMethodKind.INSTANCE, JavaTypeRef.VOID, List.of(), @@ -268,6 +270,7 @@ public class JavaWriterTests { List.of( new JavaMethodDeclaration( "a", + JavaVisibility.PUBLIC, JavaMethodKind.STATIC, JavaTypeRef.VOID, List.of(), @@ -275,6 +278,7 @@ public class JavaWriterTests { ), new JavaMethodDeclaration( "b", + JavaVisibility.PUBLIC, JavaMethodKind.STATIC, JavaTypeRef.VOID, List.of(), @@ -400,6 +404,7 @@ public class JavaWriterTests { List.of( new JavaMethodDeclaration( "a", + JavaVisibility.PUBLIC, JavaMethodKind.INSTANCE, JavaTypeRef.VOID, List.of(), @@ -407,6 +412,7 @@ public class JavaWriterTests { ), new JavaMethodDeclaration( "b", + JavaVisibility.PUBLIC, JavaMethodKind.INSTANCE, JavaTypeRef.VOID, List.of(), @@ -437,6 +443,7 @@ public class JavaWriterTests { public void emptyMethodWithoutBody() throws IOException { var java = new JavaMethodDeclaration( "a", + JavaVisibility.PUBLIC, JavaMethodKind.INSTANCE, JavaTypeRef.VOID, List.of(), @@ -454,6 +461,7 @@ public class JavaWriterTests { public void emptyInstanceMethod() throws IOException { var java = new JavaMethodDeclaration( "a", + JavaVisibility.PUBLIC, JavaMethodKind.INSTANCE, JavaTypeRef.VOID, List.of(), @@ -468,10 +476,49 @@ public class JavaWriterTests { )); } + @Test + public void publicInstanceMethod() throws IOException { + var java = new JavaMethodDeclaration( + "a", + JavaVisibility.PUBLIC, + JavaMethodKind.INSTANCE, + JavaTypeRef.VOID, + List.of(), + JavaBlock.EMPTY + ); + + var string = write(writer -> writer.writeMethodDeclaration(java)); + + assertThat(string, equalTo(""" + public void a() { + }""" + )); + } + + @Test + public void privateInstanceMethod() throws IOException { + var java = new JavaMethodDeclaration( + "a", + JavaVisibility.PRIVATE, + JavaMethodKind.INSTANCE, + JavaTypeRef.VOID, + List.of(), + JavaBlock.EMPTY + ); + + var string = write(writer -> writer.writeMethodDeclaration(java)); + + assertThat(string, equalTo(""" + private void a() { + }""" + )); + } + @Test public void emptyStaticMethod() throws IOException { var java = new JavaMethodDeclaration( "a", + JavaVisibility.PUBLIC, JavaMethodKind.STATIC, JavaTypeRef.VOID, List.of(), @@ -490,6 +537,7 @@ public class JavaWriterTests { public void methodWithParams() throws IOException { var java = new JavaMethodDeclaration( "a", + JavaVisibility.PUBLIC, JavaMethodKind.INSTANCE, JavaTypeRef.VOID, List.of( @@ -511,6 +559,7 @@ public class JavaWriterTests { public void methodWithBody() throws IOException { var java = new JavaMethodDeclaration( "a", + JavaVisibility.PUBLIC, JavaMethodKind.INSTANCE, JavaTypeRef.INT, List.of(), -- cgit v1.2.3