Super-Statement an Constructor angefügt

This commit is contained in:
JanUlrich 2017-08-30 17:06:42 +02:00
parent b251a646ca
commit 7c3181c3f0
11 changed files with 89 additions and 49 deletions

View File

@ -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<Field> fielddecl = convertFields(ctx.classBody(), generics);
List<Method> methods = convertMethods(ctx.classBody(), name, generics);
List<Method> methods = convertMethods(ctx.classBody(), name, superClass, generics);
List<Constructor> konstruktoren = new ArrayList<>();
for(int i = 0; i<methods.size();i++){
Method m = methods.get(i);
@ -281,17 +286,11 @@ public class SyntaxTreeGenerator{
if(konstruktoren.size()<1){//Standardkonstruktor anfügen:
konstruktoren.add(
generateStandardConstructor(
ctx.Identifier().getText(),
ctx.Identifier().getText(), name, superClass,
genericClassParameters, offset)
);
}
RefType superClass ;
if(ctx.superclass() != null){
superClass = convert(ctx.superclass());
}else{
superClass = ASTFactory.createObjectClass().getType();
}
Boolean isInterface = false;
List<RefTypeOrTPHOrWildcardOrGeneric> 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<Method> convertMethods(Java8Parser.ClassBodyContext classBodyContext,
JavaClassName parentClass, GenericsRegistry generics) {
JavaClassName parentClass, RefType superClass, GenericsRegistry generics) {
List<Method> 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<Field> fields = convertFields(ctx.interfaceBody());
List<Method> 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<Field> fields = convertFields(ctx.interfaceBody());
List<Method> methods = convertMethods(ctx.interfaceBody(), name, superClass, generics);
List<RefTypeOrTPHOrWildcardOrGeneric> extendedInterfaces = convert(ctx.extendsInterfaces(), generics);
return new ClassOrInterface(modifiers, name, fields, methods, new ArrayList<>(),
@ -504,11 +503,11 @@ public class SyntaxTreeGenerator{
}
private List<Method> convertMethods(Java8Parser.InterfaceBodyContext interfaceBodyContext,
JavaClassName parentClass, GenericsRegistry generics) {
JavaClassName parentClass, RefType superClass, GenericsRegistry generics) {
List<Method> 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();

View File

@ -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<GenericTypeVar> typeVars = new ArrayList<>();
for(Java8Parser.TypeParameterContext typeParameter : typeParametersContext.typeParameterList().typeParameter()){

View File

@ -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);
}
}

View File

@ -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<Statement> 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<Statement> fieldInitializations;
public Constructor(String name, RefTypeOrTPHOrWildcardOrGeneric returnType, int modifiers, ParameterList parameterList, Block codeInsideConstructor, GenericDeclarationList gtvDeclarations, Token offset, List<Statement> fieldInitializations) {
super(name, returnType, modifiers, parameterList, codeInsideConstructor, gtvDeclarations, offset);
this.fieldInitializations = fieldInitializations;
protected static Block prepareBlock(Block constructorBlock, List<Statement> fieldInitializations, RefType superClass){
List<Statement> statements = constructorBlock.getStatements();
statements.add(0, new SuperCall(constructorBlock.getOffset()));
return new Block(statements, constructorBlock.getOffset());
}
@Override

View File

@ -65,4 +65,6 @@ public interface StatementVisitor {
void visit(AssignToField assignLeftSide);
void visit(AssignToLocal assignLeftSide);
void visit(SuperCall superCall);
}

View File

@ -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);

View File

@ -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<Expression>(), offset),offset);
}
public SuperCall(SyntaxTreeNode parent){
this(0,0);
public SuperCall(ArgumentList argumentList, Token offset){
super(new Void(offset), new Receiver(new This(offset)), "<init>", argumentList, offset);
}
@Override
public void accept(StatementVisitor visitor) {
visitor.visit(this);
}
}

View File

@ -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(")");
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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:
*/