From 96d19efa5d19a43b6a206bafcdef119475972195 Mon Sep 17 00:00:00 2001 From: luca9913 Date: Sun, 12 Mar 2023 21:50:53 +0100 Subject: [PATCH] Umwandlung von Konstruktoren in eigener Methode --- .../SyntaxTreeGenerator.java | 42 +++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index 0162a744..1c7fbf86 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -23,8 +23,12 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.BlockContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassBodyDeclarationContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassOrInterfaceModifierContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassorinterfacedeclContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.ConstructorDeclarationContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.ConstructordeclContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.EmptymethodContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.GenericConstructorDeclarationContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.GenericDeclarationListContext; +import de.dhbwstuttgart.parser.antlr.Java17Parser.GenericconstructorContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.GenericmethodContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.MemberclassorinterfaceContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.MemberconstructorContext; @@ -193,7 +197,6 @@ public class SyntaxTreeGenerator { for (ModifierContext mod : member.modifier()) { membermodifiers += allmodifiers.get(mod.getText()); } - String membername; switch (member.memberDeclaration()) { case MemberclassorinterfaceContext memberclsoif: { break; @@ -207,7 +210,7 @@ public class SyntaxTreeGenerator { break; } case MemberconstructorContext memberconstructor: { - // TODO: parse constructors + constructors.add(convert(membermodifiers, memberconstructor.constructor(), name, superClass, generics)); break; } default: @@ -348,13 +351,38 @@ public class SyntaxTreeGenerator { MethodblockContext methodblock = (MethodblockContext) body; block = stmtgen.convert(methodblock.block(), true); } - if (parentClass.equals(new JavaClassName(name))) { - // TODO: Konstruktoren in eigener Methode behandeln - /* fieldInitializations geloescht PL 2018-11-24 */ - return new Constructor(modifiers, name, retType, paramlist, block, gtvDeclarations, header.getStart()); + return new Method(modifiers, name, retType, paramlist, block, gtvDeclarations, header.getStart()); + } + + public Constructor convert(int modifiers, Java17Parser.ConstructorContext constructorContext, + JavaClassName parentClass, + RefType superClass, GenericsRegistry generics) { + GenericsRegistry localgenerics = generics; + GenericDeclarationListContext genericdeclarations; + GenericDeclarationList gtvDeclarations; + ConstructorDeclarationContext constructordeclaration; + String name; + if (constructorContext instanceof GenericconstructorContext) { + GenericconstructorContext genericconstructor = (GenericconstructorContext) constructorContext; + GenericConstructorDeclarationContext gcdeclaration = genericconstructor.genericConstructorDeclaration(); + name = gcdeclaration.constructorDeclaration().identifier().getText(); + genericdeclarations = gcdeclaration.genericDeclarationList(); + constructordeclaration = gcdeclaration.constructorDeclaration(); + localgenerics.putAll(createGenerics(genericdeclarations, parentClass, name, reg, generics)); + gtvDeclarations = TypeGenerator.convert(genericdeclarations, parentClass, name, reg, localgenerics); } else { - return new Method(modifiers, name, retType, paramlist, block, gtvDeclarations, header.getStart()); + ConstructordeclContext constructordeclarationcontext = (ConstructordeclContext) constructorContext; + constructordeclaration = constructordeclarationcontext.constructorDeclaration(); + name = constructordeclaration.identifier().getText(); + gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), constructordeclaration.getStart()); } + RefTypeOrTPHOrWildcardOrGeneric retType = TypeGenerator.convertTypeName(name, constructordeclaration.getStart(), + reg, localgenerics); + StatementGenerator stmtgen = new StatementGenerator(reg, localgenerics, fields, new HashMap<>()); + ParameterList paramlist = stmtgen.convert(constructordeclaration.formalParameters().formalParameterList()); + Block block = stmtgen.convert(constructordeclaration.constructorBody, true); + return new Constructor(modifiers, name, retType, paramlist, block, gtvDeclarations, + constructordeclaration.getStart()); } private List convert(Java17Parser.FieldDeclarationContext fieldDeclContext, int modifiers,