summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javapreciselymatchers/JavaPreciselyMatchersGenerator.java5
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatypes/JavaTypesGenerator.java10
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java9
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclaration.java62
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java222
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclarationMatcher.java6
7 files changed, 195 insertions, 121 deletions
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 69e8e94..a2db183 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
@@ -108,6 +108,7 @@ public class JavaPreciselyMatchersGenerator implements Generator {
JavaMethodKind.STATIC,
javaMatcherTypeRef,
List.of(),
+ List.of(),
new JavaBlock(List.of(
new JavaReturn(new JavaNewExpression(
javaMatcherTypeRef,
@@ -147,6 +148,7 @@ public class JavaPreciselyMatchersGenerator implements Generator {
JavaMethodKind.INSTANCE,
MATCH_RESULT_REF,
List.of(new JavaParam(JavaTypeRef.OBJECT, JavaIdentifier.of("actual"))),
+ List.of(),
new JavaBlock(List.of(
new JavaReturn(
new JavaMethodCall(
@@ -163,6 +165,7 @@ public class JavaPreciselyMatchersGenerator implements Generator {
JavaMethodKind.INSTANCE,
TEXT_TREE_REF,
List.of(),
+ List.of(),
new JavaBlock(List.of(
new JavaReturn(
new JavaMethodCall(
@@ -179,6 +182,7 @@ public class JavaPreciselyMatchersGenerator implements Generator {
JavaMethodKind.INSTANCE,
genericMatcherRef(JavaTypeRef.OBJECT),
List.of(),
+ List.of(),
new JavaBlock(List.of(
new JavaReturn(new JavaStaticMethodCall(
MATCHERS_REF,
@@ -206,6 +210,7 @@ public class JavaPreciselyMatchersGenerator implements Generator {
List.of(
new JavaParam(genericMatcherRef(JavaTypeArg.superType(javaFieldTypeRef)), javaFieldName)
),
+ List.of(),
new JavaBlock(List.of(
new JavaLocalVariableDeclaration(
submatchersIdentifier,
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java
index 2649e7c..5f91c1f 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/javatransient0/JavaTransient0Generator.java
@@ -129,6 +129,7 @@ public class JavaTransient0Generator implements Generator {
outputStreamName
)
),
+ List.of(),
new JavaBlock(List.of())
),
new JavaMethodDeclaration(
@@ -142,6 +143,7 @@ public class JavaTransient0Generator implements Generator {
inputStreamName
)
),
+ List.of(),
new JavaBlock(List.of(
new JavaReturn(new JavaNullLiteral())
))
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 1aa88b2..aa194e8 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
@@ -159,6 +159,7 @@ public class JavaTypesGenerator implements Generator {
JavaMethodKind.STATIC,
builderJavaTypeRef,
List.of(),
+ List.of(),
new JavaBlock(List.of(
new JavaReturn(new JavaNewExpression(
builderJavaTypeRef,
@@ -191,6 +192,7 @@ public class JavaTypesGenerator implements Generator {
JavaMethodKind.INSTANCE,
generateTypeRef(structDefinition.type(), context),
List.of(),
+ List.of(),
new JavaBlock(List.of(
new JavaReturn(new JavaNewExpression(
generateTypeRef(structDefinition.type(), context),
@@ -232,6 +234,7 @@ public class JavaTypesGenerator implements Generator {
List.of(
new JavaParam(generateTypeRef(field.type(), context), javaFieldName)
),
+ List.of(),
new JavaBlock(List.of(
new JavaReturn(new JavaNewExpression(
builderJavaTypeRef,
@@ -251,6 +254,7 @@ public class JavaTypesGenerator implements Generator {
List.of(
new JavaParam(fieldBuilderTypeRef.get(), javaFieldName)
),
+ List.of(),
new JavaBlock(List.of(
new JavaReturn(new JavaNewExpression(
builderJavaTypeRef,
@@ -288,6 +292,7 @@ public class JavaTypesGenerator implements Generator {
List.of(
new JavaParam(javaElementType, javaElementName)
),
+ List.of(),
new JavaBlock(List.of(
new JavaLocalVariableDeclaration(
javaFieldName,
@@ -319,6 +324,7 @@ public class JavaTypesGenerator implements Generator {
List.of(
new JavaParam(elementBuilderTypeRef.get(), javaElementName)
),
+ List.of(),
new JavaBlock(List.of(
new JavaLocalVariableDeclaration(
javaFieldName,
@@ -356,6 +362,7 @@ public class JavaTypesGenerator implements Generator {
List.of(
new JavaParam(javaElementType, javaFieldName)
),
+ List.of(),
new JavaBlock(List.of(
new JavaReturn(new JavaNewExpression(
builderJavaTypeRef,
@@ -385,6 +392,7 @@ public class JavaTypesGenerator implements Generator {
List.of(
new JavaParam(elementBuilderTypeRef.get(), javaFieldName)
),
+ List.of(),
new JavaBlock(List.of(
new JavaReturn(new JavaNewExpression(
builderJavaTypeRef,
@@ -422,6 +430,7 @@ public class JavaTypesGenerator implements Generator {
JavaMethodKind.INSTANCE,
generateTypeRef(field.type().value(), context),
List.of(),
+ List.of(),
Optional.empty()
));
}
@@ -453,6 +462,7 @@ public class JavaTypesGenerator implements Generator {
JavaMethodKind.INSTANCE,
generateTypeRef(sumDefinition.type(), context),
List.of(),
+ List.of(),
Optional.empty()
)
),
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 393447c..253b469 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
@@ -485,6 +485,15 @@ public class JavaWriter implements AutoCloseable {
this.writer.write(")");
+ if (!method.throws_().isEmpty()) {
+ this.writer.write(" throws ");
+ writeWithSeparator(
+ method.throws_(),
+ exceptionType -> writeTypeRef(exceptionType),
+ () -> this.writer.write(", ")
+ );
+ }
+
if (method.body().isEmpty()) {
this.writer.write(";");
} else {
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 b3f7057..fff2413 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,62 +5,75 @@ package org.zwobble.hobgoblin.compiler.output.lang.java.ast;
// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration imports
// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration imports
-public record JavaMethodDeclaration(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier name, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaVisibility visibility, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodKind kind, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef returnType, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaParam> params, java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock> body) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaClassBodyDeclaration {
+import java.util.List;
+import java.util.Optional;
+
+public record JavaMethodDeclaration(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier name, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaVisibility visibility, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodKind kind, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef returnType, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaParam> params, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef> throws_, java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock> body) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaClassBodyDeclaration {
public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder arbitrary() {
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaVisibility.PUBLIC, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodKind.STATIC, org.zwobble.hobgoblin.compiler.output.lang.java.ast.HobgoblinNativeAst.arbitraryJavaTypeRef(), java.util.List.of(), java.util.Optional.empty());
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier.arbitrary().build(), org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaVisibility.PUBLIC, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodKind.STATIC, org.zwobble.hobgoblin.compiler.output.lang.java.ast.HobgoblinNativeAst.arbitraryJavaTypeRef(), java.util.List.of(), java.util.List.of(), java.util.Optional.empty());
}
- public record Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier name, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaVisibility visibility, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodKind kind, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef returnType, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaParam> params, java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock> body) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaClassBodyDeclaration.Builder {
+ public record Builder(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier name, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaVisibility visibility, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodKind kind, org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef returnType, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaParam> params, java.util.List<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef> throws_, java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock> body) implements org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaClassBodyDeclaration.Builder {
public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration build() {
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration(name, visibility, kind, returnType, params, body);
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration(name, visibility, kind, returnType, params, throws_, body);
}
public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier name) {
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, body);
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, throws_, body);
}
public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder withName(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier.Builder name) {
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name.build(), visibility, kind, returnType, params, body);
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name.build(), visibility, kind, returnType, params, throws_, body);
}
public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder withVisibility(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaVisibility visibility) {
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, body);
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, throws_, body);
}
public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder withKind(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodKind kind) {
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, body);
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, throws_, body);
}
public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder withReturnType(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef returnType) {
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, body);
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, throws_, body);
}
public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder withParams(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaParam> params) {
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, body);
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, throws_, body);
}
public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder addParam(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaParam param) {
var params = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaParam>(this.params);
params.add(param);
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, body);
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, throws_, body);
}
public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder addParam(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaParam.Builder param) {
var params = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaParam>(this.params);
params.add(param.build());
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, body);
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, throws_, body);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder withThrows(java.util.List<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef> throws_) {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, throws_, body);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder addThrow(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef throw_) {
+ var throws_ = new java.util.ArrayList<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef>(this.throws_);
+ throws_.add(throw_);
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, throws_, body);
}
public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder withBody(java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock> body) {
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, body);
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, throws_, body);
}
public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder withBody(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock body) {
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, java.util.Optional.of(body));
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, throws_, java.util.Optional.of(body));
}
public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder withBody(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock.Builder body) {
- return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, java.util.Optional.of(body.build()));
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder(name, visibility, kind, returnType, params, throws_, java.util.Optional.of(body.build()));
}
// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration.Builder body
@@ -68,12 +81,30 @@ public record JavaMethodDeclaration(org.zwobble.hobgoblin.compiler.output.lang.j
}
// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration body
+ public static Builder builder(
+ JavaIdentifier name,
+ JavaVisibility visibility,
+ JavaMethodKind kind,
+ JavaTypeRef returnType
+ ) {
+ return new Builder(
+ name,
+ visibility,
+ kind,
+ returnType,
+ List.of(),
+ List.of(),
+ Optional.empty()
+ );
+ }
+
public JavaMethodDeclaration(
JavaIdentifier name,
JavaVisibility visibility,
JavaMethodKind kind,
JavaTypeRef returnType,
java.util.List<JavaParam> params,
+ java.util.List<JavaTypeRef> throws_,
JavaBlock body
) {
this(
@@ -82,6 +113,7 @@ public record JavaMethodDeclaration(org.zwobble.hobgoblin.compiler.output.lang.j
kind,
returnType,
params,
+ throws_,
java.util.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 c487515..53e946f 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
@@ -120,22 +120,20 @@ public class JavaWriterTests {
JavaIdentifier.of("Rectangle"),
List.of(),
List.of(
- new JavaMethodDeclaration(
- JavaIdentifier.of("a"),
- JavaVisibility.PUBLIC,
- JavaMethodKind.INSTANCE,
- JavaTypeRef.VOID,
- List.of(),
- JavaBlock.EMPTY
- ),
- new JavaMethodDeclaration(
- JavaIdentifier.of("b"),
- JavaVisibility.PUBLIC,
- JavaMethodKind.INSTANCE,
- JavaTypeRef.VOID,
- List.of(),
- JavaBlock.EMPTY
- )
+ JavaMethodDeclaration.arbitrary()
+ .withName(JavaIdentifier.of("a"))
+ .withVisibility(JavaVisibility.PUBLIC)
+ .withKind(JavaMethodKind.INSTANCE)
+ .withReturnType(JavaTypeRef.VOID)
+ .withBody(JavaBlock.EMPTY)
+ .build(),
+ JavaMethodDeclaration.arbitrary()
+ .withName(JavaIdentifier.of("b"))
+ .withVisibility(JavaVisibility.PUBLIC)
+ .withKind(JavaMethodKind.INSTANCE)
+ .withReturnType(JavaTypeRef.VOID)
+ .withBody(JavaBlock.EMPTY)
+ .build()
),
new JavaCustomArea(JavaTypeRef.topLevel(JavaPackageName.EMPTY, JavaIdentifier.of("Rectangle")), "body"),
DocComment.EMPTY
@@ -335,22 +333,20 @@ public class JavaWriterTests {
JavaInterfaceOpenness.OPEN,
List.of(),
List.of(
- new JavaMethodDeclaration(
- JavaIdentifier.of("a"),
- JavaVisibility.PUBLIC,
- JavaMethodKind.STATIC,
- JavaTypeRef.VOID,
- List.of(),
- JavaBlock.EMPTY
- ),
- new JavaMethodDeclaration(
- JavaIdentifier.of("b"),
- JavaVisibility.PUBLIC,
- JavaMethodKind.STATIC,
- JavaTypeRef.VOID,
- List.of(),
- JavaBlock.EMPTY
- )
+ JavaMethodDeclaration.arbitrary()
+ .withName(JavaIdentifier.of("a"))
+ .withVisibility(JavaVisibility.PUBLIC)
+ .withKind(JavaMethodKind.STATIC)
+ .withReturnType(JavaTypeRef.VOID)
+ .withBody(JavaBlock.EMPTY)
+ .build(),
+ JavaMethodDeclaration.arbitrary()
+ .withName(JavaIdentifier.of("b"))
+ .withVisibility(JavaVisibility.PUBLIC)
+ .withKind(JavaMethodKind.STATIC)
+ .withReturnType(JavaTypeRef.VOID)
+ .withBody(JavaBlock.EMPTY)
+ .build()
),
new JavaCustomArea(JavaTypeRef.topLevel(JavaPackageName.EMPTY, JavaIdentifier.of("Shape")), "body"),
DocComment.EMPTY
@@ -469,22 +465,20 @@ public class JavaWriterTests {
List.of(),
List.of(),
List.of(
- new JavaMethodDeclaration(
- JavaIdentifier.of("a"),
- JavaVisibility.PUBLIC,
- JavaMethodKind.INSTANCE,
- JavaTypeRef.VOID,
- List.of(),
- JavaBlock.EMPTY
- ),
- new JavaMethodDeclaration(
- JavaIdentifier.of("b"),
- JavaVisibility.PUBLIC,
- JavaMethodKind.INSTANCE,
- JavaTypeRef.VOID,
- List.of(),
- JavaBlock.EMPTY
- )
+ JavaMethodDeclaration.arbitrary()
+ .withName(JavaIdentifier.of("a"))
+ .withVisibility(JavaVisibility.PUBLIC)
+ .withKind(JavaMethodKind.INSTANCE)
+ .withReturnType(JavaTypeRef.VOID)
+ .withBody(JavaBlock.EMPTY)
+ .build(),
+ JavaMethodDeclaration.arbitrary()
+ .withName(JavaIdentifier.of("b"))
+ .withVisibility(JavaVisibility.PUBLIC)
+ .withKind(JavaMethodKind.INSTANCE)
+ .withReturnType(JavaTypeRef.VOID)
+ .withBody(JavaBlock.EMPTY)
+ .build()
),
new JavaCustomArea(JavaTypeRef.topLevel(JavaPackageName.EMPTY, JavaIdentifier.of("Rectangle")), "body"),
DocComment.EMPTY
@@ -508,14 +502,13 @@ public class JavaWriterTests {
@Test
public void emptyMethodWithoutBody() throws IOException {
- var java = new JavaMethodDeclaration(
- JavaIdentifier.of("a"),
- JavaVisibility.PUBLIC,
- JavaMethodKind.INSTANCE,
- JavaTypeRef.VOID,
- List.of(),
- Optional.empty()
- );
+ var java = JavaMethodDeclaration.arbitrary()
+ .withName(JavaIdentifier.of("a"))
+ .withVisibility(JavaVisibility.PUBLIC)
+ .withKind(JavaMethodKind.INSTANCE)
+ .withReturnType(JavaTypeRef.VOID)
+ .withBody(Optional.empty())
+ .build();
var string = write(writer -> writer.writeMethodDeclaration(java));
@@ -526,14 +519,13 @@ public class JavaWriterTests {
@Test
public void emptyInstanceMethod() throws IOException {
- var java = new JavaMethodDeclaration(
- JavaIdentifier.of("a"),
- JavaVisibility.PUBLIC,
- JavaMethodKind.INSTANCE,
- JavaTypeRef.VOID,
- List.of(),
- JavaBlock.EMPTY
- );
+ var java = JavaMethodDeclaration.arbitrary()
+ .withName(JavaIdentifier.of("a"))
+ .withVisibility(JavaVisibility.PUBLIC)
+ .withKind(JavaMethodKind.INSTANCE)
+ .withReturnType(JavaTypeRef.VOID)
+ .withBody(JavaBlock.EMPTY)
+ .build();
var string = write(writer -> writer.writeMethodDeclaration(java));
@@ -545,14 +537,13 @@ public class JavaWriterTests {
@Test
public void publicInstanceMethod() throws IOException {
- var java = new JavaMethodDeclaration(
- JavaIdentifier.of("a"),
- JavaVisibility.PUBLIC,
- JavaMethodKind.INSTANCE,
- JavaTypeRef.VOID,
- List.of(),
- JavaBlock.EMPTY
- );
+ var java = JavaMethodDeclaration.arbitrary()
+ .withName(JavaIdentifier.of("a"))
+ .withVisibility(JavaVisibility.PUBLIC)
+ .withKind(JavaMethodKind.INSTANCE)
+ .withReturnType(JavaTypeRef.VOID)
+ .withBody(JavaBlock.EMPTY)
+ .build();
var string = write(writer -> writer.writeMethodDeclaration(java));
@@ -564,14 +555,13 @@ public class JavaWriterTests {
@Test
public void privateInstanceMethod() throws IOException {
- var java = new JavaMethodDeclaration(
- JavaIdentifier.of("a"),
- JavaVisibility.PRIVATE,
- JavaMethodKind.INSTANCE,
- JavaTypeRef.VOID,
- List.of(),
- JavaBlock.EMPTY
- );
+ var java = JavaMethodDeclaration.arbitrary()
+ .withName(JavaIdentifier.of("a"))
+ .withVisibility(JavaVisibility.PRIVATE)
+ .withKind(JavaMethodKind.INSTANCE)
+ .withReturnType(JavaTypeRef.VOID)
+ .withBody(JavaBlock.EMPTY)
+ .build();
var string = write(writer -> writer.writeMethodDeclaration(java));
@@ -583,14 +573,13 @@ public class JavaWriterTests {
@Test
public void emptyStaticMethod() throws IOException {
- var java = new JavaMethodDeclaration(
- JavaIdentifier.of("a"),
- JavaVisibility.PUBLIC,
- JavaMethodKind.STATIC,
- JavaTypeRef.VOID,
- List.of(),
- JavaBlock.EMPTY
- );
+ var java = JavaMethodDeclaration.arbitrary()
+ .withName(JavaIdentifier.of("a"))
+ .withVisibility(JavaVisibility.PUBLIC)
+ .withKind(JavaMethodKind.STATIC)
+ .withReturnType(JavaTypeRef.VOID)
+ .withBody(JavaBlock.EMPTY)
+ .build();
var string = write(writer -> writer.writeMethodDeclaration(java));
@@ -602,17 +591,17 @@ public class JavaWriterTests {
@Test
public void methodWithParams() throws IOException {
- var java = new JavaMethodDeclaration(
- JavaIdentifier.of("a"),
- JavaVisibility.PUBLIC,
- JavaMethodKind.INSTANCE,
- JavaTypeRef.VOID,
- List.of(
+ var java = JavaMethodDeclaration.arbitrary()
+ .withName(JavaIdentifier.of("a"))
+ .withVisibility(JavaVisibility.PUBLIC)
+ .withKind(JavaMethodKind.INSTANCE)
+ .withReturnType(JavaTypeRef.VOID)
+ .withParams(List.of(
new JavaParam(JavaTypeRef.INT, JavaIdentifier.of("b")),
new JavaParam(JavaTypeRef.LONG, JavaIdentifier.of("c"))
- ),
- JavaBlock.EMPTY
- );
+ ))
+ .withBody(JavaBlock.EMPTY)
+ .build();
var string = write(writer -> writer.writeMethodDeclaration(java));
@@ -623,17 +612,38 @@ public class JavaWriterTests {
}
@Test
+ public void methodWithThrows() throws IOException {
+ var java = JavaMethodDeclaration.arbitrary()
+ .withName(JavaIdentifier.of("a"))
+ .withVisibility(JavaVisibility.PUBLIC)
+ .withKind(JavaMethodKind.INSTANCE)
+ .withReturnType(JavaTypeRef.VOID)
+ .withThrows(List.of(
+ JavaTypeRef.topLevel(JavaPackageName.of("java", "io"), JavaIdentifier.of("IOException")),
+ JavaTypeRef.topLevel(JavaPackageName.of("java", "lang"), JavaIdentifier.of("InterruptedException"))
+ ))
+ .withBody(JavaBlock.EMPTY)
+ .build();
+
+ var string = write(writer -> writer.writeMethodDeclaration(java));
+
+ assertThat(string, equalTo("""
+ public void a() throws java.io.IOException, java.lang.InterruptedException {
+ }"""
+ ));
+ }
+
+ @Test
public void methodWithBody() throws IOException {
- var java = new JavaMethodDeclaration(
- JavaIdentifier.of("a"),
- JavaVisibility.PUBLIC,
- JavaMethodKind.INSTANCE,
- JavaTypeRef.INT,
- List.of(),
- new JavaBlock(List.of(
+ var java = JavaMethodDeclaration.arbitrary()
+ .withName(JavaIdentifier.of("a"))
+ .withVisibility(JavaVisibility.PUBLIC)
+ .withKind(JavaMethodKind.INSTANCE)
+ .withReturnType(JavaTypeRef.INT)
+ .withBody(new JavaBlock(List.of(
new JavaReturn(new JavaIntegerLiteral(42))
- ))
- );
+ )))
+ .build();
var string = write(writer -> writer.writeMethodDeclaration(java));
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclarationMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclarationMatcher.java
index 4a9e202..08633d6 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclarationMatcher.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclarationMatcher.java
@@ -58,6 +58,12 @@ public class JavaMethodDeclarationMatcher implements org.zwobble.precisely.Match
return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclarationMatcher(submatchers);
}
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclarationMatcher withThrows(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef>> throws_) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("throws", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration::throws_, throws_));
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclarationMatcher(submatchers);
+ }
+
public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclarationMatcher withBody(org.zwobble.precisely.Matcher<? super java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock>> body) {
var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration>>(this.submatchers);
submatchers.add(org.zwobble.precisely.Matchers.has("body", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration::body, body));