From 2c62f848edf40c84c4100d79796bc3ec0d57fd0a Mon Sep 17 00:00:00 2001 From: Jason Hertzog <86388948+JasonHertzog@users.noreply.github.com> Date: Sun, 24 Sep 2023 10:39:06 -0400 Subject: [PATCH 1/3] Adding support for custom constructors Added support for custom constructors by checking if there are any operations with the same name as the class. --- code-generator.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/code-generator.js b/code-generator.js index 0604f8f..7e7e624 100644 --- a/code-generator.js +++ b/code-generator.js @@ -401,14 +401,24 @@ class JavaCodeGenerator { writeConstructor (codeWriter, elem, options, imports, curPackage) { if (elem.name.length > 0) { var terms = [] + + // Check if there's an operation with the same name as the class (custom constructor) + var constructorOperation = elem.operations.find(op => op.name === elem.name) + // Doc - this.writeDoc(codeWriter, 'Default constructor', options, imports, curPackage) + this.writeDoc(codeWriter, constructorOperation ? 'Custom constructor' : 'Default constructor', options, imports, curPackage) // Visibility var visibility = this.getVisibility(elem) if (visibility) { terms.push(visibility) } - terms.push(elem.name + '()') + if (constructorOperation) { + // Generate constructor using parameters from the UML operation + var params = constructorOperation.params.map(param => `${param.type} ${param.name}`).join(', ') + terms.push(`${elem.name}(${params})`) + } else { + terms.push(elem.name + '()') + } codeWriter.writeLine(terms.join(' ') + ' {') codeWriter.writeLine('}') } From 004e7dd70a3b521e148bae577a7bd51894698aa9 Mon Sep 17 00:00:00 2001 From: Jason Hertzog <86388948+JasonHertzog@users.noreply.github.com> Date: Sun, 24 Sep 2023 10:56:20 -0400 Subject: [PATCH 2/3] Update code-generator.js WIP: modifying how I format the params when there is a custom class. --- code-generator.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code-generator.js b/code-generator.js index 7e7e624..877e853 100644 --- a/code-generator.js +++ b/code-generator.js @@ -414,8 +414,8 @@ class JavaCodeGenerator { } if (constructorOperation) { // Generate constructor using parameters from the UML operation - var params = constructorOperation.params.map(param => `${param.type} ${param.name}`).join(', ') - terms.push(`${elem.name}(${params})`) + var params = constructorOperation.params.map(param => param.type + " " + param.name).join(', ') + terms.push(elem.name + '(' + params + ')') } else { terms.push(elem.name + '()') } From 4ac3faa780b0139575a27a8826c6970f3e06f61f Mon Sep 17 00:00:00 2001 From: Jason Hertzog <86388948+JasonHertzog@users.noreply.github.com> Date: Sun, 24 Sep 2023 11:29:15 -0400 Subject: [PATCH 3/3] Added: Constructor will be a constructor instead of a method --- code-generator.js | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/code-generator.js b/code-generator.js index 877e853..30c6285 100644 --- a/code-generator.js +++ b/code-generator.js @@ -398,27 +398,17 @@ class JavaCodeGenerator { * @param {type.Model} elem * @param {Object} options */ - writeConstructor (codeWriter, elem, options, imports, curPackage) { + writeConstructor (codeWriter, elem, options, imports, curPackage) { if (elem.name.length > 0) { var terms = [] - - // Check if there's an operation with the same name as the class (custom constructor) - var constructorOperation = elem.operations.find(op => op.name === elem.name) - // Doc - this.writeDoc(codeWriter, constructorOperation ? 'Custom constructor' : 'Default constructor', options, imports, curPackage) + this.writeDoc(codeWriter, 'Default constructor', options, imports, curPackage) // Visibility var visibility = this.getVisibility(elem) if (visibility) { terms.push(visibility) } - if (constructorOperation) { - // Generate constructor using parameters from the UML operation - var params = constructorOperation.params.map(param => param.type + " " + param.name).join(', ') - terms.push(elem.name + '(' + params + ')') - } else { - terms.push(elem.name + '()') - } + terms.push(elem.name + '()') codeWriter.writeLine(terms.join(' ') + ' {') codeWriter.writeLine('}') } @@ -471,6 +461,7 @@ class JavaCodeGenerator { var terms = [] var params = elem.getNonReturnParameters() var returnParam = elem.getReturnParameter() + var isConstructor = (elem.name === owner.name) // doc var doc = elem.documentation.trim() @@ -500,14 +491,10 @@ class JavaCodeGenerator { } // type - if (returnParam) { + if (returnParam && !isConstructor) { terms.push(this.getType(returnParam, imports, curPackage)) - } else { - if (elem.name === owner.name) { - //constructor has no return - }else{ - terms.push('void') - } + } else if (!isConstructor) { + terms.push('void') } // name + parameters @@ -538,7 +525,7 @@ class JavaCodeGenerator { } // return statement - if (returnParam) { + if (returnParam && !isConstructor) { var returnType = this.getType(returnParam, imports, curPackage) if (returnType === 'boolean') { codeWriter.writeLine('return false;')