From ad770213d3dcd6a55e132f11204d3df983b37840 Mon Sep 17 00:00:00 2001 From: ahmad Date: Mon, 1 Jul 2024 19:13:56 +0200 Subject: [PATCH] Fixed error of constructor which is not named like class name --- .../de/maishai/typedast/typedclass/TypedClass.java | 2 +- .../typedast/typedclass/TypedConstructor.java | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java index f1bccbb..a1c1dcd 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java @@ -56,7 +56,7 @@ public class TypedClass implements TypedNode { typedDeclarations.add(new TypedDeclaration(typedProgram, declaration)); } for (Constructor constructor : c.constructors()) { - typedConstructors.add(new TypedConstructor(typedProgram, constructor)); + typedConstructors.add(new TypedConstructor(typedProgram, constructor, this.className)); } for (Method method : c.methods()) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java index 6546d7c..f081087 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java @@ -32,9 +32,14 @@ public class TypedConstructor implements TypedNode { this.typedParameters = typedParameters; this.typedBlock = typedBlock; } + public TypedConstructor(TypedProgram typedProgram, Constructor unTypedConstructor, String className) { + convertToTypedConstructor(typedProgram, unTypedConstructor, className); + } - - public void convertToTypedConstructor(TypedProgram typedProgram, Constructor unTypedConstructor) { + public void convertToTypedConstructor(TypedProgram typedProgram, Constructor unTypedConstructor, String className) { + if(!unTypedConstructor.className().equals(className)) { + throw new RuntimeException("Constructor name "+ unTypedConstructor.className() +" must be the same as class name" + className); + } name = unTypedConstructor.className(); convertToTypedParameter(typedProgram, unTypedConstructor.params()); type = Type.VOID; @@ -59,9 +64,7 @@ public class TypedConstructor implements TypedNode { return type; } - public TypedConstructor(TypedProgram typedProgram, Constructor unTypedConstructor) { - convertToTypedConstructor(typedProgram, unTypedConstructor); - } + public boolean isLocalVariablePresent(String localVarName) { return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName));