Repository: alloy-framework/alloy
Pull request #37 (2024-09-25 07:10:42)

@alloy-js/java: Allowing specifying `throws` clause to methods
Adds `throws` property to `Method` so that you can specify errors a method may throw.

pkg-pr-new[bot]: 
[Open in Stackblitz](https://pkg.pr.new/template/92009cbd-1bfb-4166-9b98-8646138a5711)


<details><summary><b>@alloy-js/babel-plugin</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/babel-plugin@37
```

</p></details>
      
    

<details><summary><b>@alloy-js/babel-preset</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/babel-preset@37
```

</p></details>
      
    

<details><summary><b>@alloy-js/babel-plugin-jsx-dom-expressions</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/babel-plugin-jsx-dom-expressions@37
```

</p></details>
      
    

<details><summary><b>@alloy-js/core</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/core@37
```

</p></details>
      
    

<details><summary><b>@alloy-js/csharp</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/csharp@37
```

</p></details>
      
    

<details><summary><b>@alloy-js/java</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/java@37
```

</p></details>
      
    

<details><summary><b>@alloy-js/prettier-plugin-alloy</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/prettier-plugin-alloy@37
```

</p></details>
      
    

<details><summary><b>@alloy-js/typescript</b></summary><p>

```
pnpm add https://pkg.pr.new/alloy-framework/alloy/@alloy-js/typescript@37
```

</p></details>
      
    

_commit: <a href="https://github.com/alloy-framework/alloy/runs/30627672364"><code>b04cfa4</code></a>_


Patch:
diff --git a/.chronus/changes/feature-throws-2024-8-25-7-11-10.md b/.chronus/changes/feature-throws-2024-8-25-7-11-10.md
new file mode 100644
index 0000000..ebf326b
--- /dev/null
+++ b/.chronus/changes/feature-throws-2024-8-25-7-11-10.md
@@ -0,0 +1,7 @@
+---
+changeKind: feature
+packages:
+  - "@alloy-js/java"
+---
+
+@alloy-js/java: Allowing specifying `throws` clause to methods
diff --git a/packages/java/src/components/Method.tsx b/packages/java/src/components/Method.tsx
index 7c163c6..7c95b92 100644
--- a/packages/java/src/components/Method.tsx
+++ b/packages/java/src/components/Method.tsx
@@ -7,6 +7,7 @@ import { Parameters } from "./Parameters.js";
 export interface MethodProps extends ObjectModifiers, GenericTypes {
   name: string;
   return?: Children;
+  throws?: Children;
   parameters?: Record<string, Children>;
   children?: Children;
 }
@@ -15,6 +16,7 @@ export function Method(props: MethodProps) {
   const params = <Parameters parameters={props.parameters}></Parameters>;
   const name = useJavaNamePolicy().getName(props.name, "method");
   const modifiers = collectModifiers(props);
+  const throwsClause = props.throws ? code` throws ${props.throws}` : "";
   const generics = props.generics ?
     code`${collectGenerics(props.generics)}${" "}`
   : "";
@@ -26,6 +28,6 @@ export function Method(props: MethodProps) {
   `
   : ";";
   return code`
-        ${modifiers}${generics}${props.return ?? "void"} ${name}(${params})${sBody}
+        ${modifiers}${generics}${props.return ?? "void"} ${name}(${params})${throwsClause}${sBody}
     `;
 }


Test patch:
diff --git a/packages/java/test/method.test.tsx b/packages/java/test/method.test.tsx
index d0351ac..728a2e4 100644
--- a/packages/java/test/method.test.tsx
+++ b/packages/java/test/method.test.tsx
@@ -1,6 +1,6 @@
 import { code, Declaration, refkey } from "@alloy-js/core";
 import { d } from "@alloy-js/core/testing";
-import { expect, it } from "vitest";
+import { describe, expect, it } from "vitest";
 import * as jv from "../src/components/index.js";
 import { assertFileContents, testRender, toSourceText } from "./utils.js";
 
@@ -48,6 +48,67 @@ it("declares bodyless function", () => {
   `);
 });
 
+describe("can throw errors", () => {
+  it("of generic Exception", () => {
+    const res = toSourceText(
+      <jv.Declaration name="Test">
+        {code`
+        class Test {
+          ${<jv.Method public name="testMethod" throws={'Exception'} />}
+        }
+      `}
+      </jv.Declaration>,
+    );
+
+    expect(res).toBe(d`
+    package me.test.code;
+    
+    class Test {
+      public void testMethod() throws Exception;
+    }
+  `);
+  });
+
+  it("of custom Exception", () => {
+    const res = testRender(
+      <>
+        <jv.SourceFile path="Test.java">
+          <jv.Declaration name="Test">
+            {code`
+              class Test {
+                ${<jv.Method public name="testMethod" throws={refkey('CustomError')} />}
+              }
+            `}
+          </jv.Declaration>
+        </jv.SourceFile>
+        <jv.PackageDirectory package="errors">
+          <jv.SourceFile path="CustomError.java">
+            <jv.Declaration name="CustomError">
+              {code`
+                public class CustomError extends Exception {
+                }
+              `}
+            </jv.Declaration>
+          </jv.SourceFile>
+
+        </jv.PackageDirectory>
+      </>,
+    );
+
+    assertFileContents(res, {
+      "Test.java": d`
+      package me.test.code;
+      
+      import me.test.code.errors.CustomError;
+
+      class Test {
+        public void testMethod() throws CustomError;
+      }
+    `,
+    });
+  });
+});
+
 it("declares return type", () => {
   const res = toSourceText(
     <jv.Declaration name="Test">