Type implementiert

This commit is contained in:
Aldaron7 2018-03-14 13:02:10 +01:00
parent 9e14473975
commit 7e88ddc3d5
27 changed files with 560 additions and 393 deletions

View File

@ -1,32 +0,0 @@
package de.dhbwstuttgart.strucTypes;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class CheckTypeVisitor extends DefaultASTVisitor {
private boolean isTypeVariable;
private String name;
@Override
public void visit(RefType refType) {
this.isTypeVariable = true;
this.name = refType.getName().toString();
}
@Override
public void visit(TypePlaceholder typePlaceholder) {
this.isTypeVariable = false;
this.name = typePlaceholder.getName();
}
public boolean isTypeVariable() {
return isTypeVariable;
}
public String getName() {
return name;
}
}

View File

@ -0,0 +1,25 @@
package de.dhbwstuttgart.strucTypes;
import java.util.Optional;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.type.RefType;
public class ClassOrInterfaceFactory {
public static Optional<ClassOrInterface> createClass(JavaClassName name) {
try {
return Optional.of(ASTFactory.createClass(ClassLoader.getSystemClassLoader().loadClass(name.toString())));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Optional.empty();
}
public static Optional<ClassOrInterface> createClass(RefType refType) {
return ClassOrInterfaceFactory.createClass(refType.getName());
}
}

View File

@ -47,7 +47,7 @@ import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType; import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class DefaultASTVisitor implements ASTVisitor { public abstract class DefaultASTVisitor implements ASTVisitor {
@Override @Override
public void visit(ArgumentList argumentList) { public void visit(ArgumentList argumentList) {

View File

@ -1,16 +1,52 @@
package de.dhbwstuttgart.strucTypes; package de.dhbwstuttgart.strucTypes;
import java.util.HashMap;
import java.util.Map;
import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.statement.Expression;
import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class StrucTYPE { public class StrucTYPE extends DefaultASTVisitor {
private TypeAssumptions assumptions;
private SourceFile sourceFile; private SourceFile sourceFile;
private ConstraintsSet constraintsSet = new ConstraintsSet();
public StrucTYPE(TypeAssumptions assumptions, SourceFile sourceFile) { private Map<TypePlaceholder, RefTypeOrTPHOrWildcardOrGeneric> inferredTypes = new HashMap<>();
this.assumptions = assumptions;
public StrucTYPE(SourceFile sourceFile) {
this.sourceFile = sourceFile; this.sourceFile = sourceFile;
} }
public ConstraintsSet getConstraints() {
for (ClassOrInterface cls : sourceFile.getClasses()) {
TYPEExpr typeExpr = new TYPEExpr();
cls.accept(typeExpr);
this.evaluateTypeExpr(typeExpr);
cls.getMethods().forEach(m -> m.accept(this));
}
return this.constraintsSet;
}
public Map<TypePlaceholder, RefTypeOrTPHOrWildcardOrGeneric> getInferredTypes() {
return inferredTypes;
}
@Override
public void visit(Method method) {
// Es gibt nur ein Return Statement
Expression retexpr = ((Return) method.block.statements.get(0)).retexpr;
// ordnet dem Methodentyp den Returntyp zu [sigma(Mt)]
this.inferredTypes.put((TypePlaceholder) method.getReturnType(), retexpr.getType());
}
private void evaluateTypeExpr(TYPEExpr typeExpr) {
this.inferredTypes.putAll(typeExpr.getInferredTypes());
this.constraintsSet.addConstraintsSet(typeExpr.getConstraints());
}
} }

View File

@ -1,187 +0,0 @@
package de.dhbwstuttgart.strucTypes;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.syntaxtree.Constructor;
import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.statement.Assign;
import de.dhbwstuttgart.syntaxtree.statement.AssignToField;
import de.dhbwstuttgart.syntaxtree.statement.Binary;
import de.dhbwstuttgart.syntaxtree.statement.DoStmt;
import de.dhbwstuttgart.syntaxtree.statement.EmptyStmt;
import de.dhbwstuttgart.syntaxtree.statement.ForStmt;
import de.dhbwstuttgart.syntaxtree.statement.IfStmt;
import de.dhbwstuttgart.syntaxtree.statement.InstanceOf;
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
import de.dhbwstuttgart.syntaxtree.statement.NewArray;
import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid;
import de.dhbwstuttgart.syntaxtree.statement.StaticClassName;
import de.dhbwstuttgart.syntaxtree.statement.Super;
import de.dhbwstuttgart.syntaxtree.statement.SuperCall;
import de.dhbwstuttgart.syntaxtree.statement.UnaryPlus;
import de.dhbwstuttgart.syntaxtree.statement.WhileStmt;
import de.dhbwstuttgart.syntaxtree.statement.literal.Literal;
import de.dhbwstuttgart.syntaxtree.statement.literal.Null;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
public abstract class StrucTypeVisitor implements ASTVisitor {
@Override
public void visit(LambdaExpression lambdaExpression) {
throw new NotImplementedException();
}
@Override
public void visit(Assign assign) {
throw new NotImplementedException();
}
@Override
public void visit(Binary binary) {
throw new NotImplementedException();
}
@Override
public void visit(EmptyStmt emptyStmt) {
throw new NotImplementedException();
}
@Override
public void visit(ForStmt forStmt) {
throw new NotImplementedException();
}
@Override
public void visit(IfStmt ifStmt) {
throw new NotImplementedException();
}
@Override
public void visit(InstanceOf instanceOf) {
throw new NotImplementedException();
}
@Override
public void visit(LocalVarDecl localVarDecl) {
throw new NotImplementedException();
}
@Override
public void visit(NewArray newArray) {
throw new NotImplementedException();
}
@Override
public void visit(ReturnVoid aReturn) {
throw new NotImplementedException();
}
@Override
public void visit(StaticClassName staticClassName) {
throw new NotImplementedException();
}
@Override
public void visit(Super aSuper) {
throw new NotImplementedException();
}
@Override
public void visit(UnaryPlus unaryPlus) {
throw new NotImplementedException();
}
@Override
public void visit(WhileStmt whileStmt) {
throw new NotImplementedException();
}
@Override
public void visit(DoStmt whileStmt) {
throw new NotImplementedException();
}
@Override
public void visit(Null aNull) {
throw new NotImplementedException();
}
@Override
public void visit(Literal literal) {
throw new NotImplementedException();
}
@Override
public void visit(AssignToField assignLeftSide) {
throw new NotImplementedException();
}
@Override
public void visit(AssignToLocal assignLeftSide) {
throw new NotImplementedException();
}
@Override
public void visit(SuperCall superCall) {
throw new NotImplementedException();
}
@Override
public void visit(GenericTypeVar genericTypeVar) {
throw new NotImplementedException();
}
@Override
public void visit(GenericDeclarationList genericTypeVars) {
throw new NotImplementedException();
}
@Override
public void visit(Constructor field) {
throw new NotImplementedException();
}
@Override
public void visit(SuperWildcardType superWildcardType) {
throw new NotImplementedException();
}
@Override
public void visit(ExtendsWildcardType extendsWildcardType) {
throw new NotImplementedException();
}
@Override
public void visit(GenericRefType genericRefType) {
throw new NotImplementedException();
}
}

View File

@ -1,188 +1,192 @@
package de.dhbwstuttgart.strucTypes; package de.dhbwstuttgart.strucTypes;
import de.dhbwstuttgart.exceptions.NotImplementedException; import java.util.ArrayList;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import java.util.HashMap;
import de.dhbwstuttgart.syntaxtree.ASTVisitor; import java.util.List;
import java.util.Map;
import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet;
import de.dhbwstuttgart.strucTypes.constraint.FieldConstraint;
import de.dhbwstuttgart.strucTypes.constraint.MethodConstraint;
import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Constructor;
import de.dhbwstuttgart.syntaxtree.Field; import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.ParameterList;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList; import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
import de.dhbwstuttgart.syntaxtree.statement.Assign;
import de.dhbwstuttgart.syntaxtree.statement.AssignToField;
import de.dhbwstuttgart.syntaxtree.statement.Binary;
import de.dhbwstuttgart.syntaxtree.statement.Block; import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.CastExpr; import de.dhbwstuttgart.syntaxtree.statement.CastExpr;
import de.dhbwstuttgart.syntaxtree.statement.DoStmt;
import de.dhbwstuttgart.syntaxtree.statement.EmptyStmt;
import de.dhbwstuttgart.syntaxtree.statement.Expression; import de.dhbwstuttgart.syntaxtree.statement.Expression;
import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver; import de.dhbwstuttgart.syntaxtree.statement.ExpressionReceiver;
import de.dhbwstuttgart.syntaxtree.statement.FieldVar; import de.dhbwstuttgart.syntaxtree.statement.FieldVar;
import de.dhbwstuttgart.syntaxtree.statement.ForStmt;
import de.dhbwstuttgart.syntaxtree.statement.IfStmt;
import de.dhbwstuttgart.syntaxtree.statement.InstanceOf;
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
import de.dhbwstuttgart.syntaxtree.statement.LocalVar; import de.dhbwstuttgart.syntaxtree.statement.LocalVar;
import de.dhbwstuttgart.syntaxtree.statement.LocalVarDecl;
import de.dhbwstuttgart.syntaxtree.statement.MethodCall; import de.dhbwstuttgart.syntaxtree.statement.MethodCall;
import de.dhbwstuttgart.syntaxtree.statement.NewArray;
import de.dhbwstuttgart.syntaxtree.statement.NewClass; import de.dhbwstuttgart.syntaxtree.statement.NewClass;
import de.dhbwstuttgart.syntaxtree.statement.Return; import de.dhbwstuttgart.syntaxtree.statement.Return;
import de.dhbwstuttgart.syntaxtree.statement.ReturnVoid;
import de.dhbwstuttgart.syntaxtree.statement.StaticClassName;
import de.dhbwstuttgart.syntaxtree.statement.Super;
import de.dhbwstuttgart.syntaxtree.statement.SuperCall;
import de.dhbwstuttgart.syntaxtree.statement.This; import de.dhbwstuttgart.syntaxtree.statement.This;
import de.dhbwstuttgart.syntaxtree.statement.UnaryPlus;
import de.dhbwstuttgart.syntaxtree.statement.WhileStmt;
import de.dhbwstuttgart.syntaxtree.statement.literal.Literal;
import de.dhbwstuttgart.syntaxtree.statement.literal.Null;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class TYPEExpr extends StrucTypeVisitor { public class TYPEExpr extends DefaultASTVisitor {
private TypeAssumptions assumptions; private ClassOrInterface aThis;
private ConstraintsSet constraints; private ConstraintsSet constraints = new ConstraintsSet();
private Map<TypePlaceholder, RefTypeOrTPHOrWildcardOrGeneric> inferredTypes = new HashMap<>();
public TYPEExpr(TypeAssumptions assumptions) {
this.assumptions = assumptions; public TYPEExpr() {
} }
public ConstraintsSet getConstraints(){ public ConstraintsSet getConstraints() {
return this.constraints; return this.constraints;
} }
@Override public Map<TypePlaceholder, RefTypeOrTPHOrWildcardOrGeneric> getInferredTypes() {
public void visit(SourceFile sourceFile) { return inferredTypes;
// TODO Auto-generated method stub
throw new NotImplementedException();
} }
@Override @Override
public void visit(ClassOrInterface classOrInterface) { public void visit(ClassOrInterface classOrInterface) {
// TODO Auto-generated method stub this.aThis = classOrInterface;
throw new NotImplementedException(); classOrInterface.getMethods().forEach(m -> m.accept(this));
}
@Override
public void visit(Field field) {
// TODO Auto-generated method stub
throw new NotImplementedException();
}
@Override
public void visit(Method method) {
// TODO Auto-generated method stub
throw new NotImplementedException();
}
@Override
public void visit(ParameterList formalParameters) {
// TODO Auto-generated method stub
}
@Override
public void visit(FormalParameter formalParameter) {
// TODO Auto-generated method stub
}
@Override
public void visit(Block block) {
// TODO Auto-generated method stub
throw new NotImplementedException();
}
@Override
public void visit(Return aReturn) {
// TODO Auto-generated method stub
throw new NotImplementedException();
}
@Override
public void visit(ExpressionReceiver expressionReceiver) {
// TODO Auto-generated method stub
} }
@Override @Override
public void visit(LocalVar localVar) { public void visit(LocalVar localVar) {
// keine neuen Constraints // keine neuen Constraints. Typisierung bereits im SyntaxTree vorhanden.
} }
@Override @Override
public void visit(FieldVar fieldVar) { public void visit(FieldVar fieldVar) {
fieldVar.receiver.accept(this); fieldVar.receiver.accept(this);
CheckTypeVisitor fieldTypeVisitor = new CheckTypeVisitor();
fieldVar.getType().accept(fieldTypeVisitor); // Löst Typen zwischen Feld und Feldvariable auf
//TODO check ty in Ass && rty f in fields(ty) this.inferFieldVarTypes(fieldVar);
if(!fieldTypeVisitor.isTypeVariable()){} // keine neuen Constraints
else{ // Ermittelt den Typ ty von fieldVar.receiver und fields(f)
FieldConstraint fieldConstraint = new FieldConstraint(fieldVar.receiver.getType(), fieldVar.getType(), fieldVar.fieldVarName); TypeExtract fieldTypeVisitor = new TypeExtract();
fieldVar.receiver.getType().accept(fieldTypeVisitor);
if (!fieldTypeVisitor.isTypeVariable()) {
fieldTypeVisitor.getField(fieldVar.fieldVarName).ifPresent(
f -> this.inferredTypes.put((TypePlaceholder) fieldVar.receiver.getType(), (RefType) f.getType()));
} // keine neuen Constraints
else {
FieldConstraint fieldConstraint = new FieldConstraint(fieldVar.receiver.getType(), fieldVar.fieldVarName,
fieldVar.getType());
this.constraints.addConstraint(fieldConstraint); this.constraints.addConstraint(fieldConstraint);
} }
}
private void inferFieldVarTypes(FieldVar fieldVar) {
if (fieldVar.receiver instanceof This) {
this.aThis.getFieldDecl().stream().filter(f -> f.getName().equals(fieldVar.fieldVarName))
// keine statische Polymorphie zugelassen
.findFirst()
.ifPresent(f -> this.inferredTypes.put((TypePlaceholder) fieldVar.getType(), f.getType()));
}
} }
@Override @Override
public void visit(MethodCall methodCall) { public void visit(MethodCall methodCall) {
// TODO Auto-generated method stub methodCall.receiver.accept(this);
methodCall.arglist.accept(this);
// ermittelt den Typ ty0 von methodCall.receiver und mtype(m, ty0)
TypeExtract methodTypeVisitor = new TypeExtract();
methodCall.receiver.getType().accept(methodTypeVisitor);
List<Expression> arguments = methodCall.getArgumentList().getArguments();
if (!methodTypeVisitor.isTypeVariable()) {
methodTypeVisitor.getMethod(methodCall.name, arguments.size()).ifPresent(m -> {
for (int i = 0; i < arguments.size(); i++) {
this.constraints.addConstraint(new SubTypeConstraint(arguments.get(i).getType(),
m.getParameterList().getParameterAt(i).getType()));
}
this.inferredTypes.put((TypePlaceholder) methodCall.getType(), (RefType) m.getReturnType());
});
} else {
MethodConstraint methodConstraint = new MethodConstraint(methodCall.receiver.getType(), methodCall.name,
new ArrayList<SubTypeConstraint>(), methodCall.getType());
arguments.forEach(e -> methodConstraint.addArguments(e.getType()));
this.constraints.addConstraint(methodConstraint);
}
} }
@Override @Override
public void visit(ArgumentList argumentList) { public void visit(ArgumentList argumentList) {
// TODO Auto-generated method stub argumentList.getArguments().forEach(a -> a.accept(this));
} }
@Override @Override
public void visit(CastExpr castExpr) { public void visit(CastExpr castExpr) {
// TODO Auto-generated method stub castExpr.expr.accept(this);
// TODO RefType equals implementieren
if (((RefType) castExpr.getType()).getName().equals(this.aThis.getClassName())) {
// keine neuen Constraints
} else {
// TODO implement generics
}
} }
@Override @Override
public void visit(NewClass newClass) { public void visit(NewClass newClass) {
// TODO Auto-generated method stub newClass.getArgumentList().accept(this);
RefType type = (RefType) newClass.getType();
TypeExtract typeExtract = new TypeExtract();
// TODO RefType equals implementieren
if (type.getName().equals(this.aThis.getType().getName())) {
this.aThis.accept(typeExtract);
} else {
type.accept(typeExtract);
// TODO implement generics
}
this.createNewClassSubTypeConstraints(newClass, typeExtract);
} }
@Override private void createNewClassSubTypeConstraints(NewClass newClass, TypeExtract typeExtract) {
public void visit(RefType refType) { List<Expression> arguments = newClass.getArgumentList().getArguments();
// TODO Auto-generated method stub List<Field> fields = typeExtract.getFields();
int argumentsSize = arguments.size();
} int fieldsSize = fields.size();
if (argumentsSize != fieldsSize) {
@Override throw new IllegalArgumentException(String.format(
public void visit(TypePlaceholder typePlaceholder) { "The number of fields (%d) in %s doesn't match the number of arguments (%d) passed to its constructor.",
// TODO Auto-generated method stub fieldsSize, newClass.name, argumentsSize));
}
for (int i = 0; i < argumentsSize; i++) {
SubTypeConstraint subTypeConstraint = new SubTypeConstraint(arguments.get(i).getType(),
fields.get(i).getType());
this.constraints.addConstraint(subTypeConstraint);
}
} }
@Override @Override
public void visit(This aThis) { public void visit(This aThis) {
// TODO Auto-generated method stub if (this.aThis != null) {
this.inferredTypes.put((TypePlaceholder) aThis.getType(), (RefType) this.aThis.getType());
}
} }
@Override
public void visit(ExpressionReceiver expressionReceiver) {
expressionReceiver.expr.accept(this);
}
@Override
public void visit(Method method) {
method.block.accept(this);
}
@Override
public void visit(Block block) {
// Es gibt nur ein Statement: Return
block.getStatements().forEach(s -> s.accept(this));
}
@Override
public void visit(Return aReturn) {
aReturn.retexpr.accept(this);
}
} }

View File

@ -1,5 +0,0 @@
package de.dhbwstuttgart.strucTypes;
public class TypeAssumptions {
}

View File

@ -0,0 +1,98 @@
package de.dhbwstuttgart.strucTypes;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Constructor;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
/**
* Extrahiert die typisierten Felder und Methoden von ClassOrInterface bzw. von
* RefType rekursiv über alle Superklassen und implementierten Interfaces.
*
* @author mvr
*
*/
public class TypeExtract extends DefaultASTVisitor {
private List<Field> fields = new ArrayList<>();
private List<Method> methods = new ArrayList<>();
private List<Constructor> constructors = new ArrayList<>();
private boolean typeVariable = false;
private boolean initialClass = true;
public List<Field> getFields() {
return this.fields;
}
public List<Method> getMethods() {
return this.methods;
}
public List<Constructor> getConstructors() {
return constructors;
}
public Optional<Field> getField(String fieldName) {
return this.fields.stream().filter(f -> f.getName().equals(fieldName))
// keine statische Polymorphie zugelassen
.findFirst();
}
public Optional<Method> getMethod(String methodName, int parameterCount) {
return this.methods.stream()
.filter(m -> m.getName().equals(methodName) && m.getParameterList().getFormalparalist().size() == parameterCount)
// keine statische Polymorphie zugelassen
.findFirst();
}
public boolean isTypeVariable() {
return this.typeVariable;
}
@Override
public void visit(ClassOrInterface classOrInterface) {
classOrInterface.getFieldDecl().forEach(f -> f.accept(this));
classOrInterface.getMethods().forEach(m -> m.accept(this));
classOrInterface.getConstructors().forEach(c -> {if(c != null && this.initialClass) c.accept(this);});
this.initialClass = false;
// superClass(Object) -> Object => unendliche Rekursionstiefe!
if (!classOrInterface.getClassName().equals(new JavaClassName("java.lang.Object"))) {
classOrInterface.getSuperClass().accept(this);
classOrInterface.implementedInterfaces.forEach(i -> i.accept(this));
}
}
@Override
public void visit(RefType refType) {
this.typeVariable = false;
ClassOrInterfaceFactory.createClass(refType).ifPresent(c -> c.accept(this));
}
@Override
public void visit(Field field) {
this.fields.add(field);
}
@Override
public void visit(Method method) {
this.methods.add(method);
}
@Override
public void visit(TypePlaceholder typePlaceholder) {
this.typeVariable = true;
}
@Override
public void visit(Constructor constructor) {
this.constructors.add(constructor);
}
}

View File

@ -1,4 +1,4 @@
package de.dhbwstuttgart.strucTypes; package de.dhbwstuttgart.strucTypes.constraint;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -39,6 +39,12 @@ public class ConstraintsSet {
this.methodConstraints.add(constraint); this.methodConstraints.add(constraint);
} }
public void addConstraintsSet(ConstraintsSet constraintsSet){
constraintsSet.getSubTypeConstraints().forEach(this::addConstraint);
constraintsSet.getFieldConstraints().forEach(this::addConstraint);
constraintsSet.getMethodConstraints().forEach(this::addConstraint);
}
} }

View File

@ -1,18 +1,24 @@
package de.dhbwstuttgart.strucTypes; package de.dhbwstuttgart.strucTypes.constraint;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
/**
* fc(c, f, c') : Klasse/Typ c hat ein Feld f mit Typ c'
*
* @author mvr
*
*/
public class FieldConstraint { public class FieldConstraint {
private RefTypeOrTPHOrWildcardOrGeneric classType; private RefTypeOrTPHOrWildcardOrGeneric classType;
private RefTypeOrTPHOrWildcardOrGeneric fieldType;
private String fieldName; private String fieldName;
private RefTypeOrTPHOrWildcardOrGeneric fieldType;
public FieldConstraint(RefTypeOrTPHOrWildcardOrGeneric classType, RefTypeOrTPHOrWildcardOrGeneric fieldType,
String fieldName) { public FieldConstraint(RefTypeOrTPHOrWildcardOrGeneric classType, String fieldName,
RefTypeOrTPHOrWildcardOrGeneric fieldType) {
this.classType = classType; this.classType = classType;
this.fieldType = fieldType;
this.fieldName = fieldName; this.fieldName = fieldName;
this.fieldType = fieldType;
} }
public RefTypeOrTPHOrWildcardOrGeneric getClassType() { public RefTypeOrTPHOrWildcardOrGeneric getClassType() {
@ -26,8 +32,5 @@ public class FieldConstraint {
public String getFieldName() { public String getFieldName() {
return fieldName; return fieldName;
} }
} }

View File

@ -1,4 +1,4 @@
package de.dhbwstuttgart.strucTypes; package de.dhbwstuttgart.strucTypes.constraint;
import java.util.List; import java.util.List;
@ -7,15 +7,22 @@ import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
/**
* mc(c, m, C, (c', C')) : Klasse/Typ c hat Methode m mit Argumenten von Typ C
* mit Rückgabewert c' und Subtyp-Constraints C <. C' wobei C = {c1,... , cn)
*
* @author mvr
*
*/
public class MethodConstraint { public class MethodConstraint {
private RefTypeOrTPHOrWildcardOrGeneric classType; private RefTypeOrTPHOrWildcardOrGeneric classType;
private RefTypeOrTPHOrWildcardOrGeneric returnType; private RefTypeOrTPHOrWildcardOrGeneric returnType;
private String methodName; private String methodName;
private List<SubTypeConstraint> arguments; private List<SubTypeConstraint> arguments;
public MethodConstraint(RefTypeOrTPHOrWildcardOrGeneric classType, RefTypeOrTPHOrWildcardOrGeneric returnType, public MethodConstraint(RefTypeOrTPHOrWildcardOrGeneric classType, String methodName,
String methodName, List<SubTypeConstraint> arguments) { List<SubTypeConstraint> arguments, RefTypeOrTPHOrWildcardOrGeneric returnType) {
this.classType = classType; this.classType = classType;
this.returnType = returnType; this.returnType = returnType;
this.methodName = methodName; this.methodName = methodName;
@ -37,14 +44,14 @@ public class MethodConstraint {
public List<SubTypeConstraint> getArguments() { public List<SubTypeConstraint> getArguments() {
return arguments; return arguments;
} }
public void addArgument(SubTypeConstraint... subTypeConstraints){ public void addArguments(SubTypeConstraint... subTypeConstraints) {
for (SubTypeConstraint subTypeConstraint : subTypeConstraints) { for (SubTypeConstraint subTypeConstraint : subTypeConstraints) {
this.arguments.add(subTypeConstraint); this.arguments.add(subTypeConstraint);
} }
} }
public void addArgument(RefTypeOrTPHOrWildcardOrGeneric... parameters){ public void addArguments(RefTypeOrTPHOrWildcardOrGeneric... parameters) {
for (RefTypeOrTPHOrWildcardOrGeneric parameter : parameters) { for (RefTypeOrTPHOrWildcardOrGeneric parameter : parameters) {
Token offset = parameter.getOffset(); Token offset = parameter.getOffset();
TypePlaceholder tph = TypePlaceholder.fresh(offset); TypePlaceholder tph = TypePlaceholder.fresh(offset);
@ -52,6 +59,5 @@ public class MethodConstraint {
this.arguments.add(subTypeConstraint); this.arguments.add(subTypeConstraint);
} }
} }
} }

View File

@ -1,22 +1,29 @@
package de.dhbwstuttgart.strucTypes; package de.dhbwstuttgart.strucTypes.constraint;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
/**
* c <. c' : c ist subtyp von c'
*
* @author mvr
*
*/
public class SubTypeConstraint { public class SubTypeConstraint {
private RefTypeOrTPHOrWildcardOrGeneric subtype; private RefTypeOrTPHOrWildcardOrGeneric subtype;
private RefTypeOrTPHOrWildcardOrGeneric supertype; private RefTypeOrTPHOrWildcardOrGeneric supertype;
public SubTypeConstraint(RefTypeOrTPHOrWildcardOrGeneric subtype, RefTypeOrTPHOrWildcardOrGeneric supertype) { public SubTypeConstraint(RefTypeOrTPHOrWildcardOrGeneric subtype, RefTypeOrTPHOrWildcardOrGeneric supertype) {
this.subtype = subtype; this.subtype = subtype;
this.supertype = supertype; this.supertype = supertype;
} }
public RefTypeOrTPHOrWildcardOrGeneric getSubtype() { public RefTypeOrTPHOrWildcardOrGeneric getSubtype() {
return subtype; return subtype;
} }
public RefTypeOrTPHOrWildcardOrGeneric getSupertype() { public RefTypeOrTPHOrWildcardOrGeneric getSupertype() {
return supertype; return supertype;
} }
} }

View File

@ -0,0 +1,42 @@
package de.dhbwstuttgart.strucTypes.printutils;
import java.util.List;
import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet;
import de.dhbwstuttgart.strucTypes.constraint.FieldConstraint;
import de.dhbwstuttgart.strucTypes.constraint.MethodConstraint;
import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint;
public class PrintConstraints {
public static void print(ConstraintsSet constraintsSet){
printSubTypeConstraints(constraintsSet.getSubTypeConstraints());
printFieldConstraints(constraintsSet.getFieldConstraints());
printMethodConstraints(constraintsSet.getMethodConstraints());
}
public static void printSubTypeConstraints(List<SubTypeConstraint> constraints) {
System.out.println("\n SubTypeConstraints:");
constraints.forEach(c-> System.out.println(c.getSubtype() + " < " + c.getSupertype()));
}
public static void printFieldConstraints(List<FieldConstraint> constraints){
System.out.println("\n FieldConstraints:");
constraints.forEach(c -> System.out.println(String.format("F(%s,%s,%s)", c.getClassType() , c.getFieldName() , c.getFieldType())));
}
public static void printMethodConstraints(List<MethodConstraint> constraints){
System.out.println("\n MethodConstraints:");
constraints.forEach(c-> {
StringBuilder sb = new StringBuilder("M(");
sb.append(c.getClassType() + ",");
sb.append(c.getMethodName() + ",[");
c.getArguments().forEach(a -> sb.append(a.getSubtype() + ","));
sb.append("],(" + c.getReturnType() + ",[");
c.getArguments().forEach(a-> sb.append(a.getSupertype() + ","));
sb.append("]))");
System.out.println(sb.toString());
});
}
}

View File

@ -0,0 +1,14 @@
package de.dhbwstuttgart.strucTypes.printutils;
import java.util.Map;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class PrintInferredTypes {
public static void print(Map<TypePlaceholder, RefTypeOrTPHOrWildcardOrGeneric> inferredTypes){
System.out.println("\n Inferred Types:");
inferredTypes.keySet().forEach(key -> System.out.println(key + " <=> " + inferredTypes.get(key)));
}
}

View File

@ -1,4 +1,4 @@
package de.dhbwstuttgart.strucTypes; package de.dhbwstuttgart.strucTypes.printutils;
import java.util.List; import java.util.List;
@ -66,16 +66,18 @@ public class SyntaxTreePrinter implements ASTVisitor {
@Override @Override
public void visit(ClassOrInterface classOrInterface) { public void visit(ClassOrInterface classOrInterface) {
JavaClassName className = classOrInterface.getClassName(); JavaClassName className = classOrInterface.getClassName();
RefTypeOrTPHOrWildcardOrGeneric type = classOrInterface.getType();
List<Field> fields = classOrInterface.getFieldDecl(); List<Field> fields = classOrInterface.getFieldDecl();
List<Method> methods = classOrInterface.getMethods(); List<Method> methods = classOrInterface.getMethods();
RefType superClass = classOrInterface.getSuperClass(); RefType superClass = classOrInterface.getSuperClass();
List<? extends RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces = classOrInterface.implementedInterfaces; List<? extends RefTypeOrTPHOrWildcardOrGeneric> implementedInterfaces = classOrInterface.implementedInterfaces;
System.out.println("class: " + className); System.out.print("class: " + className + " : ");
type.accept(this);
fields.forEach(f->f.accept(this)); fields.forEach(f->f.accept(this));
methods.forEach(m->m.accept(this)); methods.forEach(m->m.accept(this));
System.out.print("superClass: "); System.out.print("\n superClass: ");
superClass.accept(this); superClass.accept(this);
System.out.println("implemented Interfaces: "); System.out.println("\n implemented Interfaces: ");
implementedInterfaces.forEach(i -> i.accept(this)); implementedInterfaces.forEach(i -> i.accept(this));
} }
@ -94,7 +96,7 @@ public class SyntaxTreePrinter implements ASTVisitor {
String name = method.getName(); String name = method.getName();
RefTypeOrTPHOrWildcardOrGeneric returnType = method.getReturnType(); RefTypeOrTPHOrWildcardOrGeneric returnType = method.getReturnType();
ParameterList parameterList = method.getParameterList(); ParameterList parameterList = method.getParameterList();
System.out.print("method: " + name + " : "); System.out.print("\n method: " + name + " : ");
returnType.accept(this); returnType.accept(this);
parameterList.accept(this); parameterList.accept(this);
method.block.accept(this); method.block.accept(this);
@ -199,7 +201,7 @@ public class SyntaxTreePrinter implements ASTVisitor {
public void visit(This aThis) { public void visit(This aThis) {
ArgumentList arglist = aThis.arglist; ArgumentList arglist = aThis.arglist;
RefTypeOrTPHOrWildcardOrGeneric type = aThis.getType(); RefTypeOrTPHOrWildcardOrGeneric type = aThis.getType();
System.out.print("this: " + " : "); System.out.print("this : ");
type.accept(this); type.accept(this);
if( arglist != null) arglist.accept(this); if( arglist != null) arglist.accept(this);
} }
@ -208,7 +210,10 @@ public class SyntaxTreePrinter implements ASTVisitor {
public void visit(RefType refType) { public void visit(RefType refType) {
JavaClassName name = refType.getName(); JavaClassName name = refType.getName();
System.out.println(name); System.out.println(name);
if(!refType.getParaList().isEmpty()){
System.out.println(" generics: ");
refType.getParaList().forEach(p->p.accept(this));
}
} }
@Override @Override

View File

@ -0,0 +1,49 @@
package strucType;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.strucTypes.StrucTYPE;
import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet;
import de.dhbwstuttgart.strucTypes.printutils.PrintConstraints;
import de.dhbwstuttgart.strucTypes.printutils.PrintInferredTypes;
import de.dhbwstuttgart.strucTypes.printutils.SyntaxTreePrinter;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class TestStrucType {
public static final String rootDirectory = System.getProperty("user.dir") + "/test/strucType/javFiles/";
@org.junit.Test
public void test() throws ClassNotFoundException, IOException {
ArrayList<File> files = new ArrayList<>();
// files.add(new File(rootDirectory + "testLocalVar.jav"));
// files.add(new File(rootDirectory + "testCast.jav"));
files.add(new File(rootDirectory + "testNew.jav"));
// files.add(new File(rootDirectory + "testFieldVar.jav"));
// files.add(new File(rootDirectory + "testFieldMethod.jav"));
// files.add(new File(rootDirectory + "testPaperExample.jav"));
JavaTXCompiler compiler = new JavaTXCompiler(files);
for (File f : compiler.sourceFiles.keySet()) {
String name = f.getName();
System.out.println("Filename: " + name);
SourceFile sourceFile = compiler.sourceFiles.get(f);
//Print SourceFile Infos
sourceFile.accept(new SyntaxTreePrinter());
StrucTYPE strucTYPE = new StrucTYPE(sourceFile);
ConstraintsSet constraints = strucTYPE.getConstraints();
PrintConstraints.print(constraints);
Map<TypePlaceholder, RefTypeOrTPHOrWildcardOrGeneric> inferredTypes = strucTYPE.getInferredTypes();
PrintInferredTypes.print(inferredTypes);
System.out.println("____________________________________________________________________________");
}
}
}

View File

@ -8,7 +8,7 @@ import java.util.ArrayList;
import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.strucTypes.SyntaxTreePrinter; import de.dhbwstuttgart.strucTypes.printutils.SyntaxTreePrinter;
public class TestSyntaxTreePrinter { public class TestSyntaxTreePrinter {
public static final String rootDirectory = System.getProperty("user.dir")+"/test/strucType/javFiles/"; public static final String rootDirectory = System.getProperty("user.dir")+"/test/strucType/javFiles/";

View File

@ -1,4 +1,10 @@
package strucType.input;
import strucType.typedtestclasses.A;
import strucType.typedtestclasses.A2;
import strucType.typedtestclasses.B;
class C class C
{ {
mC(x){return (Integer)x; } mA(A x){return (A2)x; }
mB(c) { return (B)c; }
} }

View File

@ -1,5 +1,10 @@
package strucType.input;
import java.lang.Integer;
class M class M
{ {
f; f;
mM(x, y) { return f.mF(x, y); } mM(x, y) { return f.mF(x, y); }
mInt(Integer x) { return x.doubleValue(); }
} }

View File

@ -1,3 +1,5 @@
package strucType.input;
class F class F
{ {
f; f;

View File

@ -1,6 +1,6 @@
import java.util.List; package strucType.input;
class C1 implements List class C
{ {
m(x) { return x; } m(x) { return x; }
} }

View File

@ -1,4 +1,10 @@
package strucType.input;
import strucType.typedtestclasses.A;
import strucType.typedtestclasses.B;
class N class N
{ {
mN(s) {return new String(s); } a;
mA(){ return new A(a); }
mB(x){ return new B(x,a); }
} }

View File

@ -1,3 +1,5 @@
package strucType.input;
class A class A
{ {
mt(x, y, z) { return x.sub(y).add(z); } mt(x, y, z) { return x.sub(y).add(z); }

View File

@ -0,0 +1,19 @@
package strucType.typedtestclasses;
public class A {
public A a;
public A(A a) {
this.a = a;
}
public A getA() {
return this.a;
}
public <E> B<E> getB(B<E> b) {
return b;
}
}

View File

@ -0,0 +1,9 @@
package strucType.typedtestclasses;
public class A2 extends A {
public A2(A a) {
super(a);
}
}

View File

@ -0,0 +1,20 @@
package strucType.typedtestclasses;
public class B<E> {
public E e;
public A a;
public B(E e, A a) {
this.e = e;
this.a = a;
}
public E getGeneric(){
return this.e;
}
public A getInput(){
return this.a;
}
}

View File

@ -0,0 +1,27 @@
package strucType.typedtestclasses;
public class C<E,F> {
public E e;
public F f;
public C(E e, F f) {
this.e = e;
this.f = f;
}
public E getGeneric1(){
return this.e;
}
public F getGeneric2(){
return this.f;
}
public C<A, B<E>> convert(A a, B<E> b){
return new C<>(a,b);
}
}