diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index b6e85c9a..dd6fe176 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -14,6 +14,7 @@ import de.dhbwstuttgart.typecheck.*; import java.io.File; import java.lang.reflect.Modifier; +import java.sql.Ref; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -182,27 +183,27 @@ public class SyntaxTreeGenerator{ return new SourceFile(this.pkgName, classes, this.imports); } - public Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass, GenericsRegistry generics) { + public Method convert(Java8Parser.MethodDeclarationContext methodDeclarationContext, JavaClassName parentClass, RefType superClass, GenericsRegistry generics) { Java8Parser.MethodHeaderContext header = methodDeclarationContext.methodHeader(); int modifiers = SyntaxTreeGenerator.convert(methodDeclarationContext.methodModifier()); GenericsRegistry localGenerics = createGenerics(methodDeclarationContext.methodHeader().typeParameters(), parentClass, header.methodDeclarator().Identifier().getText()); localGenerics.putAll(generics); - return convert(modifiers, header, methodDeclarationContext.methodBody(),parentClass, localGenerics); + return convert(modifiers, header, methodDeclarationContext.methodBody(),parentClass, superClass, localGenerics); } - public Method convert(Java8Parser.InterfaceMethodDeclarationContext ctx, JavaClassName parentClass, GenericsRegistry generics) { + public Method convert(Java8Parser.InterfaceMethodDeclarationContext ctx, JavaClassName parentClass, RefType superClass, GenericsRegistry generics) { Java8Parser.MethodHeaderContext header = ctx.methodHeader(); int modifiers = SyntaxTreeGenerator.convertInterfaceModifier(ctx.interfaceMethodModifier()); GenericsRegistry localGenerics = createGenerics(header.typeParameters(), parentClass, header.methodDeclarator().Identifier().getText()); localGenerics.putAll(generics); - return convert(modifiers, header, ctx.methodBody(),parentClass, localGenerics); + return convert(modifiers, header, ctx.methodBody(),parentClass, superClass, localGenerics); } private Method convert(int modifiers, Java8Parser.MethodHeaderContext header, Java8Parser.MethodBodyContext body, - JavaClassName parentClass, GenericsRegistry localGenerics) { + JavaClassName parentClass, RefType superClass, GenericsRegistry localGenerics) { StatementGenerator stmtGen = new StatementGenerator(reg, localGenerics, new HashMap<>()); @@ -232,9 +233,7 @@ public class SyntaxTreeGenerator{ block = stmtGen.convert(body.block()); } if(parentClass.equals(new JavaClassName(name))){ - //TODO: Constructor darf nicht Rückgabetyp void bekommen: Hier als Rückgabetyp die Klasse inklusive generische Variablen - //retType = TypeGenerator.convertTypeName(name, gtvDeclarations, header.getStart(), reg, localGenerics); - return new Constructor(name, retType, modifiers, parameterList, block, gtvDeclarations, header.getStart(), fieldInitializations); + return new Constructor(name, retType, modifiers, parameterList, block, gtvDeclarations, header.getStart(), fieldInitializations, superClass); }else{ return new Method(name, retType, modifiers, parameterList,block, gtvDeclarations, header.getStart()); } @@ -268,8 +267,14 @@ public class SyntaxTreeGenerator{ }else{ genericClassParameters = TypeGenerator.convert(ctx.typeParameters(), name, "",reg, generics); } + RefType superClass ; + if(ctx.superclass() != null){ + superClass = convert(ctx.superclass()); + }else{ + superClass = ASTFactory.createObjectClass().getType(); + } List fielddecl = convertFields(ctx.classBody(), generics); - List methods = convertMethods(ctx.classBody(), name, generics); + List methods = convertMethods(ctx.classBody(), name, superClass, generics); List konstruktoren = new ArrayList<>(); for(int i = 0; i implementedInterfaces = convert(ctx.superinterfaces(), generics); return new ClassOrInterface(modifiers, name, fielddecl, methods, konstruktoren, genericClassParameters, superClass, @@ -311,13 +310,12 @@ public class SyntaxTreeGenerator{ return ret; } - private Constructor generateStandardConstructor(String className, GenericDeclarationList classGenerics, Token offset){ + private Constructor generateStandardConstructor(String className, JavaClassName parentClass, RefType superClass, GenericDeclarationList classGenerics, Token offset){ RefType classType = ClassOrInterface.generateTypeOfClass(reg.getName(className), classGenerics, offset); int modifiers = 0; ParameterList params = new ParameterList(new ArrayList<>(), offset); - //TODO: Konstruktor muss Felder initialisieren: Block block = new Block(new ArrayList<>(), offset); - return new Constructor(className, classType, modifiers, params, block, classGenerics, offset, fieldInitializations); + return new Constructor(className, classType, modifiers, params, block, classGenerics, offset, fieldInitializations, superClass); } private RefType convert(Java8Parser.SuperclassContext superclass) { @@ -325,7 +323,7 @@ public class SyntaxTreeGenerator{ } private List convertMethods(Java8Parser.ClassBodyContext classBodyContext, - JavaClassName parentClass, GenericsRegistry generics) { + JavaClassName parentClass, RefType superClass, GenericsRegistry generics) { List ret = new ArrayList<>(); for(Java8Parser.ClassBodyDeclarationContext classMember : classBodyContext.classBodyDeclaration()){ if(classMember.classMemberDeclaration() != null){ @@ -334,7 +332,7 @@ public class SyntaxTreeGenerator{ //Do nothing! }else if(classMemberDeclarationContext.methodDeclaration()!= null){ - ret.add(this.convert(classMemberDeclarationContext.methodDeclaration(), parentClass, generics)); + ret.add(this.convert(classMemberDeclarationContext.methodDeclaration(), parentClass, superClass, generics)); } } } @@ -466,8 +464,6 @@ public class SyntaxTreeGenerator{ GenericsRegistry generics = createGenerics(ctx.typeParameters(), name, ""); - List fields = convertFields(ctx.interfaceBody()); - List methods = convertMethods(ctx.interfaceBody(), name, generics); GenericDeclarationList genericParams; if(ctx.typeParameters() != null){ genericParams = TypeGenerator.convert(ctx.typeParameters(), name, "",reg, generics); @@ -476,6 +472,9 @@ public class SyntaxTreeGenerator{ } RefType superClass = ASTFactory.createObjectClass().getType(); + List fields = convertFields(ctx.interfaceBody()); + List methods = convertMethods(ctx.interfaceBody(), name, superClass, generics); + List extendedInterfaces = convert(ctx.extendsInterfaces(), generics); return new ClassOrInterface(modifiers, name, fields, methods, new ArrayList<>(), @@ -504,11 +503,11 @@ public class SyntaxTreeGenerator{ } private List convertMethods(Java8Parser.InterfaceBodyContext interfaceBodyContext, - JavaClassName parentClass, GenericsRegistry generics) { + JavaClassName parentClass, RefType superClass, GenericsRegistry generics) { List ret = new ArrayList<>(); for(Java8Parser.InterfaceMemberDeclarationContext member : interfaceBodyContext.interfaceMemberDeclaration()){ if(member.interfaceMethodDeclaration() != null){ - ret.add(this.convert(member.interfaceMethodDeclaration(), parentClass, generics)); + ret.add(this.convert(member.interfaceMethodDeclaration(), parentClass, superClass, generics)); //new Method(name, type, modifier, params, null, genericDecls, member.interfaceMethodDeclaration().getStart()); }else{ throw new NotImplementedException(); diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java index b5f72532..eeaca00c 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/TypeGenerator.java @@ -54,7 +54,8 @@ public class TypeGenerator { return TypeGenerator.convert(unannTypeContext.unannReferenceType().unannClassOrInterfaceType(), reg, genericsRegistry); } - public static GenericDeclarationList convert(Java8Parser.TypeParametersContext typeParametersContext, JavaClassName parentClass, String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) { + public static GenericDeclarationList convert(Java8Parser.TypeParametersContext typeParametersContext, + JavaClassName parentClass, String parentMethod, JavaClassRegistry reg, GenericsRegistry generics) { Token endOffset = typeParametersContext.getStop(); List typeVars = new ArrayList<>(); for(Java8Parser.TypeParameterContext typeParameter : typeParametersContext.typeParameterList().typeParameter()){ diff --git a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java index 20e895b4..cd3027f5 100644 --- a/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java +++ b/src/de/dhbwstuttgart/syntaxtree/AbstractASTWalker.java @@ -263,4 +263,9 @@ public abstract class AbstractASTWalker implements ASTVisitor{ public void visit(AssignToLocal assignLeftSide) { assignLeftSide.localVar.accept(this); } + + @Override + public void visit(SuperCall superCall) { + this.visit((MethodCall)superCall); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/Constructor.java b/src/de/dhbwstuttgart/syntaxtree/Constructor.java index 9f085051..3423e377 100644 --- a/src/de/dhbwstuttgart/syntaxtree/Constructor.java +++ b/src/de/dhbwstuttgart/syntaxtree/Constructor.java @@ -1,31 +1,34 @@ package de.dhbwstuttgart.syntaxtree; import de.dhbwstuttgart.syntaxtree.statement.Statement; +import de.dhbwstuttgart.syntaxtree.statement.SuperCall; +import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceBlockInformation; -import de.dhbwstuttgart.typeinference.assumptions.TypeInferenceInformation; -import de.dhbwstuttgart.typeinference.constraints.ConstraintSet; -import de.dhbwstuttgart.typeinference.typeAlgo.TYPE; -import de.dhbwstuttgart.typeinference.typeAlgo.TYPEStmt; import org.antlr.v4.runtime.Token; import de.dhbwstuttgart.syntaxtree.statement.Block; -import java.util.ArrayList; import java.util.List; public class Constructor extends Method { + //TODO: Constructor braucht ein super-Statement + public Constructor(String name, RefTypeOrTPHOrWildcardOrGeneric returnType, int modifiers, ParameterList parameterList, Block codeInsideConstructor, + GenericDeclarationList gtvDeclarations, Token offset, List fieldInitializations, RefType superClass) { + super(name, returnType, modifiers, parameterList, prepareBlock(codeInsideConstructor,fieldInitializations, superClass), gtvDeclarations, offset); + + } + /** - * Das sind die Statements, welche die Felder der zugehörigen Klasse dieses Konstruktor initialisieren + * @param fieldInitializations - Das sind die Statements, + * welche die Felder der zugehörigen Klasse dieses + * Konstruktor initialisieren */ - public final List fieldInitializations; - - public Constructor(String name, RefTypeOrTPHOrWildcardOrGeneric returnType, int modifiers, ParameterList parameterList, Block codeInsideConstructor, GenericDeclarationList gtvDeclarations, Token offset, List fieldInitializations) { - super(name, returnType, modifiers, parameterList, codeInsideConstructor, gtvDeclarations, offset); - - this.fieldInitializations = fieldInitializations; + protected static Block prepareBlock(Block constructorBlock, List fieldInitializations, RefType superClass){ + List statements = constructorBlock.getStatements(); + statements.add(0, new SuperCall(constructorBlock.getOffset())); + return new Block(statements, constructorBlock.getOffset()); } @Override diff --git a/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java b/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java index b912fab6..b63e0438 100644 --- a/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java +++ b/src/de/dhbwstuttgart/syntaxtree/StatementVisitor.java @@ -65,4 +65,6 @@ public interface StatementVisitor { void visit(AssignToField assignLeftSide); void visit(AssignToLocal assignLeftSide); + + void visit(SuperCall superCall); } diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index 60531289..5dba40a1 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -79,10 +79,19 @@ public class ASTFactory { Token offset = new NullToken(); int modifier = constructor.getModifiers(); - return new de.dhbwstuttgart.syntaxtree.Constructor(name,returnType, modifier, parameterList, block, gtvDeclarations, offset, new ArrayList<>()); + if(inClass.equals(java.lang.Object.class)){ + return null; + } + + return new de.dhbwstuttgart.syntaxtree.Constructor(name,returnType, modifier, parameterList, block, gtvDeclarations, offset, new ArrayList<>(), + createType(inClass.getSuperclass())); } - public static Method createMethod(java.lang.reflect.Method jreMethod, java.lang.Class inClass){ + private static RefType createType(Class classType) { + return createClass(classType).getType(); + } + + public static Method createMethod(java.lang.reflect.Method jreMethod, java.lang.Class inClass){ String name = jreMethod.getName(); RefTypeOrTPHOrWildcardOrGeneric returnType; returnType = createType(jreMethod.getReturnType(),new JavaClassName(inClass.getName()), name); diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/SuperCall.java b/src/de/dhbwstuttgart/syntaxtree/statement/SuperCall.java index ea9b491d..44cf213b 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/SuperCall.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/SuperCall.java @@ -1,5 +1,9 @@ package de.dhbwstuttgart.syntaxtree.statement; +import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.type.RefType; +import de.dhbwstuttgart.syntaxtree.type.Void; +import org.antlr.v4.runtime.Token; import org.apache.bcel.Constants; import org.apache.bcel.generic.InstructionFactory; import org.apache.bcel.generic.InstructionHandle; @@ -7,18 +11,22 @@ import org.apache.bcel.generic.InstructionList; import de.dhbwstuttgart.syntaxtree.SyntaxTreeNode; +import java.util.ArrayList; -public class SuperCall extends ThisCall +public class SuperCall extends MethodCall { - public SuperCall(int offset,int variableLength) - { - super(null,null,variableLength); + public SuperCall(Token offset){ + this(new ArgumentList(new ArrayList(), offset),offset); } - public SuperCall(SyntaxTreeNode parent){ - this(0,0); + public SuperCall(ArgumentList argumentList, Token offset){ + super(new Void(offset), new Receiver(new This(offset)), "", argumentList, offset); } - + + @Override + public void accept(StatementVisitor visitor) { + visitor.visit(this); + } } diff --git a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index e38c0230..0befcb2b 100644 --- a/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -342,4 +342,11 @@ public class OutputGenerator implements ASTVisitor { public void visit(AssignToLocal assignLeftSide) { assignLeftSide.localVar.accept(this); } + + @Override + public void visit(SuperCall superCall) { + out.append("super("); + superCall.arglist.accept(this); + out.append(")"); + } } \ No newline at end of file diff --git a/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java b/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java index 7d0f557d..a8fc9de6 100644 --- a/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java +++ b/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java @@ -3,6 +3,7 @@ package de.dhbwstuttgart.typedeployment; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.statement.AssignLeftSide; import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression; +import de.dhbwstuttgart.syntaxtree.statement.SuperCall; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.ResultSet; import de.dhbwstuttgart.typeinference.constraints.Pair; diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java index 2a9ef6d0..960f1ab6 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPE.java @@ -69,7 +69,7 @@ public class TYPE { private ConstraintSet getConstraintsConstructor(Constructor m, TypeInferenceInformation info, ClassOrInterface currentClass) { TypeInferenceBlockInformation blockInfo = new TypeInferenceBlockInformation(info.getAvailableClasses(), currentClass, null); TYPEStmt methodScope = new TYPEStmt(blockInfo); - for(Statement stmt : m.fieldInitializations)stmt.accept(methodScope); + //for(Statement stmt : m.fieldInitializations)stmt.accept(methodScope); ConstraintSet ret = this.getConstraintsMethod(m, info, currentClass); ret.addAll(methodScope.getConstraints()); return ret; diff --git a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 8f51a634..9b149620 100644 --- a/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -239,6 +239,11 @@ public class TYPEStmt implements StatementVisitor{ //Hier ist kein Code nötig. Es werden keine extra Constraints generiert } + @Override + public void visit(SuperCall superCall) { + //TODO: Für einen super-Call werden keine Constraints erzeugt bisher + } + /* METHOD CALL Section: */