Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions libs/java-ast/src/lib/ast/Class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,83 @@ describe("Class", () => {
expect(output).toContain("this.id = id;");
});

it("should write constructor documentation if the constructor has it", () => {
const cls = new Class({
name: "ClassWithConstructor",
packageName: "com.example",
access: Access.Public,
});

cls.addConstructor({
access: Access.Public,
javadoc: "Some constructor",
parameters: [
new Parameter({
name: "id",
docs: "The id to set",
type: Type.long(),
}),
],
body: new CodeBlock({ code: "this.id = id;" }),
});

cls.write(writer);
const output = writer.toString();

expect(output).toContain(" * Some constructor");
expect(output).toContain(" * @param id The id to set");
});

it("should write constructor documentation if the constructor doesn't, but the parameters do", () => {
const cls = new Class({
name: "ClassWithConstructor",
packageName: "com.example",
access: Access.Public,
});

cls.addConstructor({
access: Access.Public,
parameters: [
new Parameter({
name: "id",
docs: "The id to set",
type: Type.long(),
}),
],
body: new CodeBlock({ code: "this.id = id;" }),
});

cls.write(writer);
const output = writer.toString();

expect(output).toContain(" * @param id The id to set");
});

it("should write constructor documentation if the constructor does but the parameters don't", () => {
const cls = new Class({
name: "ClassWithConstructor",
packageName: "com.example",
javadoc: "Some constructor",
access: Access.Public,
});

cls.addConstructor({
access: Access.Public,
parameters: [
new Parameter({
name: "id",
type: Type.long(),
}),
],
body: new CodeBlock({ code: "this.id = id;" }),
});

cls.write(writer);
const output = writer.toString();

expect(output).toContain(" * Some constructor");
});

it("should write a class with inheritance", () => {
const cls = new Class({
name: "Child",
Expand Down
21 changes: 21 additions & 0 deletions libs/java-ast/src/lib/ast/Class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export declare namespace Class {
parameters: Parameter[];
/** The access level of the constructor */
access: Access;
/** Documentation/JavaDoc for the constructor */
javadoc?: string;
/** Constructor annotations */
annotations?: Annotation[];
/** Whether this constructor calls another constructor with super() */
Expand Down Expand Up @@ -316,6 +318,25 @@ export class Class extends AstNode {
writer: Writer,
constructor: Class.Constructor,
): void {
// Write JavaDoc if provided
if (
constructor.javadoc ||
constructor.parameters.some((param) => param.docs)
) {
writer.writeLine("/**");
if (constructor.javadoc) {
constructor.javadoc.split("\n").forEach((line) => {
writer.writeLine(` * ${line}`);
});
}
constructor.parameters.forEach((param) => {
if (param.docs) {
writer.writeLine(` * @param ${param.name} ${param.docs}`);
}
});
writer.writeLine(" */");
}

// Write annotations
if (constructor.annotations) {
constructor.annotations.forEach((annotation) => {
Expand Down