Umwandlung von Konstruktoren in eigener Methode

This commit is contained in:
luca9913 2023-03-12 21:50:53 +01:00
parent 1919e34eed
commit 96d19efa5d

View File

@ -23,8 +23,12 @@ import de.dhbwstuttgart.parser.antlr.Java17Parser.BlockContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassBodyDeclarationContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassBodyDeclarationContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassOrInterfaceModifierContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassOrInterfaceModifierContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.ClassorinterfacedeclContext; 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.EmptymethodContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.GenericConstructorDeclarationContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.GenericDeclarationListContext; 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.GenericmethodContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.MemberclassorinterfaceContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.MemberclassorinterfaceContext;
import de.dhbwstuttgart.parser.antlr.Java17Parser.MemberconstructorContext; import de.dhbwstuttgart.parser.antlr.Java17Parser.MemberconstructorContext;
@ -193,7 +197,6 @@ public class SyntaxTreeGenerator {
for (ModifierContext mod : member.modifier()) { for (ModifierContext mod : member.modifier()) {
membermodifiers += allmodifiers.get(mod.getText()); membermodifiers += allmodifiers.get(mod.getText());
} }
String membername;
switch (member.memberDeclaration()) { switch (member.memberDeclaration()) {
case MemberclassorinterfaceContext memberclsoif: { case MemberclassorinterfaceContext memberclsoif: {
break; break;
@ -207,7 +210,7 @@ public class SyntaxTreeGenerator {
break; break;
} }
case MemberconstructorContext memberconstructor: { case MemberconstructorContext memberconstructor: {
// TODO: parse constructors constructors.add(convert(membermodifiers, memberconstructor.constructor(), name, superClass, generics));
break; break;
} }
default: default:
@ -348,13 +351,38 @@ public class SyntaxTreeGenerator {
MethodblockContext methodblock = (MethodblockContext) body; MethodblockContext methodblock = (MethodblockContext) body;
block = stmtgen.convert(methodblock.block(), true); block = stmtgen.convert(methodblock.block(), true);
} }
if (parentClass.equals(new JavaClassName(name))) { return new Method(modifiers, name, retType, paramlist, block, gtvDeclarations, header.getStart());
// TODO: Konstruktoren in eigener Methode behandeln }
/* fieldInitializations geloescht PL 2018-11-24 */
return new Constructor(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 { } 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<? extends Field> convert(Java17Parser.FieldDeclarationContext fieldDeclContext, int modifiers, private List<? extends Field> convert(Java17Parser.FieldDeclarationContext fieldDeclContext, int modifiers,