From ce2b4e03037d3bf6134ddb72b2e5879f7ab9fcbf Mon Sep 17 00:00:00 2001 From: Aldaron7 Date: Fri, 23 Mar 2018 21:41:31 +0100 Subject: [PATCH] Anpassung bei typisierungen in TYPEExpr, Anpassung Ausgabe --- .../dhbwstuttgart/strucTypes/Construct.java | 28 ++--- .../strucTypes/InferredTypes.java | 18 ++++ .../dhbwstuttgart/strucTypes/StrucTYPE.java | 6 +- src/de/dhbwstuttgart/strucTypes/TYPEExpr.java | 36 ++++--- .../dhbwstuttgart/strucTypes/TypeExtract.java | 2 - .../constraint/FieldConstraint.java | 5 + .../constraint/MethodConstraint.java | 36 +++++++ .../constraint/SubTypeConstraint.java | 5 + .../IllegalInterfaceTypeException.java | 32 ++++++ .../printutils/PrintConstraints.java | 102 ++++++++---------- .../printutils/SyntaxTreePrinter.java | 57 ++++++---- test/strucType/TestConstruct.java | 40 +++---- test/strucType/TestStrucType.java | 12 +-- test/strucType/TestSyntaxTreePrinter.java | 12 +-- test/strucType/javFiles/testCast.jav | 11 +- test/strucType/javFiles/testMethod.jav | 6 +- test/strucType/javFiles/testNew.jav | 3 +- 17 files changed, 255 insertions(+), 156 deletions(-) create mode 100644 src/de/dhbwstuttgart/strucTypes/exception/IllegalInterfaceTypeException.java diff --git a/src/de/dhbwstuttgart/strucTypes/Construct.java b/src/de/dhbwstuttgart/strucTypes/Construct.java index 0e02be7e..ac5aa85a 100644 --- a/src/de/dhbwstuttgart/strucTypes/Construct.java +++ b/src/de/dhbwstuttgart/strucTypes/Construct.java @@ -7,10 +7,10 @@ import java.util.List; import org.antlr.v4.runtime.Token; import de.dhbwstuttgart.parser.NullToken; -import de.dhbwstuttgart.parser.SyntaxTreeGenerator.GenericContext; import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet; import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint; +import de.dhbwstuttgart.strucTypes.exception.IllegalInterfaceTypeException; import de.dhbwstuttgart.strucTypes.exception.ImpossibleSubTypeException; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.Constructor; @@ -42,15 +42,15 @@ public class Construct extends DefaultASTVisitor { } } - public InferredTypes getInferredTypes() { - return inferredTypes; - } - public List getConstructedInterfaces() { this.newInterf.forEach(i -> i.accept(this)); return constructedInterfaces; } + public InferredTypes getInferredTypes() { + return inferredTypes; + } + public List getSubTypeConstraints() { return subTypeConstraints; } @@ -70,12 +70,10 @@ public class Construct extends DefaultASTVisitor { this.constructedInterfaces.add(this.constructInterface(typePlaceholder, name)); } - // TODO check nur TPH in newInterf + //check nur TPH in newInterf @Override public void visit(RefType refType) { - // JavaClassName name = refType.getName(); - // this.constructedInterfaces.add(this.constructInterface(refType, - // name)); + throw new IllegalInterfaceTypeException(String.format("%s is not a valid type to generate an interface for.", refType)); } private ClassOrInterface constructInterface(TypePlaceholder i, JavaClassName name) { @@ -85,24 +83,20 @@ public class Construct extends DefaultASTVisitor { List parameterInhTyterm = new ArrayList<>(); final Token offset = new NullToken(); - // For über alle FieldConstraints mit ClassType i + // Schleife über alle FieldConstraints mit ClassType i this.constraintsSet.getFieldConstraints().stream().filter(fc -> fc.getClassType().equals(i)).forEach(fc -> { TypePlaceholder type = TypePlaceholder.fresh(offset); parameterInhTyterm.add(fc.getFieldType()); - // TODO generics.add(new GenericTypeVar(s, bounds, offset, - // endOffset)); mit type generics.add(new GenericTypeVar( type.getName(), new ArrayList<>(), offset, offset)); Field field = new Field(fc.getFieldName(), type, Modifier.PUBLIC, offset); fielddecl.add(field); }); - // For über alle MethodConstraints mit ClassType i + // Schleife über alle MethodConstraints mit ClassType i this.constraintsSet.getMethodConstraints().stream().filter(mc -> mc.getClassType().equals(i)).forEach(mc -> { TypePlaceholder returnType = TypePlaceholder.fresh(offset); parameterInhTyterm.add(mc.getReturnType()); - // TODO generics.add(new GenericTypeVar(s, bounds, offset, - // endOffset)); mit retrunType generics.add(new GenericTypeVar(returnType.getName(), new ArrayList<>(), offset, offset)); Block block = new Block(new ArrayList<>(), offset); GenericDeclarationList gtvDeclarations = new GenericDeclarationList(new ArrayList<>(), offset); @@ -111,8 +105,6 @@ public class Construct extends DefaultASTVisitor { TypePlaceholder tph = TypePlaceholder.fresh(offset); params.add(new FormalParameter(tph.getName(), tph, offset)); parameterInhTyterm.add(supertype); - // TODO generics.add(new GenericTypeVar(s, bounds, offset, - // endOffset)); mit tph generics.add(new GenericTypeVar(tph.getName(), new ArrayList<>(), offset, offset)); }); ParameterList parameterList = new ParameterList(params, offset); @@ -134,8 +126,6 @@ public class Construct extends DefaultASTVisitor { final List implementedInterfaces = new ArrayList<>(); GenericDeclarationList genericClassParameters = new GenericDeclarationList(generics, i.getOffset()); ClassOrInterface constructedInterface = new ClassOrInterface(modifiers, name, fielddecl, methods, constructors, genericClassParameters, superClass, isInterface, implementedInterfaces, offset); -// new ClassOrInterface(modifiers, name, fielddecl, methods, constructors, genericClassParameters, -// superClass, isInterface, implementedInterfaces, offset); return constructedInterface; } diff --git a/src/de/dhbwstuttgart/strucTypes/InferredTypes.java b/src/de/dhbwstuttgart/strucTypes/InferredTypes.java index 7fb49e12..3523e459 100644 --- a/src/de/dhbwstuttgart/strucTypes/InferredTypes.java +++ b/src/de/dhbwstuttgart/strucTypes/InferredTypes.java @@ -2,6 +2,7 @@ package de.dhbwstuttgart.strucTypes; import java.util.Collection; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import java.util.Set; @@ -12,6 +13,8 @@ public class InferredTypes implements Map inferredTypes = new HashMap<>(); + public static final InferredTypes EMPTY = new InferredTypes(); + public void resolveTransitiveTypes() { Set keySet = this.inferredTypes.keySet(); for (TypePlaceholder key : keySet) { @@ -82,4 +85,19 @@ public class InferredTypes implements Map iterator = this.inferredTypes.keySet().iterator(); + while (iterator.hasNext()) { + TypePlaceholder tph = iterator.next(); + s += tph + " -> " + inferredTypes.get(tph); + if (iterator.hasNext()) { + s += ", "; + } + } + s += "]"; + return s; + } + } diff --git a/src/de/dhbwstuttgart/strucTypes/StrucTYPE.java b/src/de/dhbwstuttgart/strucTypes/StrucTYPE.java index dbca1113..06de9fad 100644 --- a/src/de/dhbwstuttgart/strucTypes/StrucTYPE.java +++ b/src/de/dhbwstuttgart/strucTypes/StrucTYPE.java @@ -21,7 +21,7 @@ public class StrucTYPE extends DefaultASTVisitor { } public ConstraintsSet getConstraints() { - for (ClassOrInterface cls : sourceFile.getClasses()) { + for (ClassOrInterface cls : this.sourceFile.getClasses()) { TYPEExpr typeExpr = new TYPEExpr(); cls.accept(typeExpr); cls.getMethods().forEach(m -> m.accept(this)); @@ -54,9 +54,9 @@ public class StrucTYPE extends DefaultASTVisitor { private void evaluateTypeExpr(TYPEExpr typeExpr) { this.inferredTypes.putAll(typeExpr.getInferredTypes()); this.inferredTypes.resolveTransitiveTypes(); + this.constraintsSet.addConstraintsSet(typeExpr.getConstraints()); - // TODO infer types in constraints - this.constraintsSet.inferTypes(inferredTypes); + this.constraintsSet.inferTypes(this.inferredTypes); } } diff --git a/src/de/dhbwstuttgart/strucTypes/TYPEExpr.java b/src/de/dhbwstuttgart/strucTypes/TYPEExpr.java index 0da45426..d87a2486 100644 --- a/src/de/dhbwstuttgart/strucTypes/TYPEExpr.java +++ b/src/de/dhbwstuttgart/strucTypes/TYPEExpr.java @@ -68,8 +68,8 @@ public class TYPEExpr extends DefaultASTVisitor { } if (!fieldTypeVisitor.isTypeVariable() && fieldTypeVisitor.getField(fieldVar.fieldVarName).isPresent()) { - fieldTypeVisitor.getField(fieldVar.fieldVarName).ifPresent( - f -> this.inferredTypes.put((TypePlaceholder) fieldVar.getType(), (RefType) f.getType())); + fieldTypeVisitor.getField(fieldVar.fieldVarName) + .ifPresent(f -> this.inferredTypes.put((TypePlaceholder) fieldVar.getType(), f.getType())); } // keine neuen Constraints else { FieldConstraint fieldConstraint = new FieldConstraint(fieldVar.receiver.getType(), fieldVar.fieldVarName, @@ -78,6 +78,13 @@ public class TYPEExpr extends DefaultASTVisitor { } } + /** + * Löst die Typenzugehörigkeit zwischen fieldVar mit receiver this und dem + * korrespondierenden Feld in der Instanz auf. + * + * @param fieldVar + * @return true, wenn ein passendes Feld in der Instanz gefungen wird. + */ private boolean inferFieldVarTypes(FieldVar fieldVar) { if (fieldVar.receiver instanceof This) { this.aThis.getFieldDecl().stream().filter(f -> f.getName().equals(fieldVar.fieldVarName)) @@ -111,7 +118,7 @@ public class TYPEExpr extends DefaultASTVisitor { this.constraints.addConstraint(new SubTypeConstraint(arguments.get(i).getType(), m.getParameterList().getParameterAt(i).getType())); } - this.inferredTypes.put((TypePlaceholder) methodCall.getType(), (RefType) m.getReturnType()); + this.inferredTypes.put((TypePlaceholder) methodCall.getType(), m.getReturnType()); }); } else { MethodConstraint methodConstraint = new MethodConstraint(methodCall.receiver.getType(), methodCall.name, @@ -129,11 +136,10 @@ public class TYPEExpr extends DefaultASTVisitor { @Override public void visit(CastExpr castExpr) { castExpr.expr.accept(this); - // TODO RefType equals implementieren if (((RefType) castExpr.getType()).getName().equals(this.aThis.getClassName())) { // keine neuen Constraints } else { - // TODO implement generics + // implement generics nicht nötig } } @@ -142,21 +148,20 @@ public class TYPEExpr extends DefaultASTVisitor { newClass.getArgumentList().accept(this); RefType type = (RefType) newClass.getType(); TypeExtract typeExtract = new TypeExtract(); - // TODO RefType equals implementieren if (type.getName().equals(this.aThis.getClassName())) { this.aThis.accept(typeExtract); } else { type.accept(typeExtract); - // TODO implement generics + // implement generics nicht nötig } this.createNewClassSubTypeConstraints(newClass, typeExtract); } private void createNewClassSubTypeConstraints(NewClass newClass, TypeExtract typeExtract) { - List arguments = newClass.getArgumentList().getArguments(); - List fields = typeExtract.getFields(); - int argumentsSize = arguments.size(); - int fieldsSize = fields.size(); + final List arguments = newClass.getArgumentList().getArguments(); + final List fields = typeExtract.getFields(); + final int argumentsSize = arguments.size(); + final int fieldsSize = fields.size(); if (argumentsSize != fieldsSize) { throw new IllegalArgumentException(String.format( "The number of fields (%d) in %s doesn't match the number of arguments (%d) passed to its constructor.", @@ -171,10 +176,11 @@ public class TYPEExpr extends DefaultASTVisitor { @Override public void visit(This aThis) { - //TODO TPH von This mit Klasse in inferredTypes verknüpfen -// if (this.aThis != null) { -// this.inferredTypes.put((TypePlaceholder) aThis.getType(), (RefType) this.aThis.getType()); -// } + // TPH von This mit Klasse in inferredTypes verknüpfen + if (this.aThis != null) { + this.inferredTypes.put((TypePlaceholder) aThis.getType(), ClassOrInterface + .generateTypeOfClass(this.aThis.getClassName(), this.aThis.getGenerics(), this.aThis.getOffset())); + } } diff --git a/src/de/dhbwstuttgart/strucTypes/TypeExtract.java b/src/de/dhbwstuttgart/strucTypes/TypeExtract.java index 22f5f34e..e3d1e048 100644 --- a/src/de/dhbwstuttgart/strucTypes/TypeExtract.java +++ b/src/de/dhbwstuttgart/strucTypes/TypeExtract.java @@ -81,13 +81,11 @@ public class TypeExtract extends DefaultASTVisitor { @Override public void visit(Field field) { -// if (field.getType() instanceof RefType) this.fields.add(field); } @Override public void visit(Method method) { - if (method.getReturnType() instanceof RefType) this.methods.add(method); } diff --git a/src/de/dhbwstuttgart/strucTypes/constraint/FieldConstraint.java b/src/de/dhbwstuttgart/strucTypes/constraint/FieldConstraint.java index abe2b119..c9e3c17e 100644 --- a/src/de/dhbwstuttgart/strucTypes/constraint/FieldConstraint.java +++ b/src/de/dhbwstuttgart/strucTypes/constraint/FieldConstraint.java @@ -42,5 +42,10 @@ public class FieldConstraint { this.fieldType = inferredTypes.get(fieldType); } } + + @Override + public String toString() { + return String.format("F(%s, %s, %s)", this.classType, this.fieldName, this.fieldType); + } } diff --git a/src/de/dhbwstuttgart/strucTypes/constraint/MethodConstraint.java b/src/de/dhbwstuttgart/strucTypes/constraint/MethodConstraint.java index 285698ef..b6ed99a1 100644 --- a/src/de/dhbwstuttgart/strucTypes/constraint/MethodConstraint.java +++ b/src/de/dhbwstuttgart/strucTypes/constraint/MethodConstraint.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.strucTypes.constraint; +import java.util.Iterator; import java.util.List; import org.antlr.v4.runtime.Token; @@ -71,4 +72,39 @@ public class MethodConstraint { arguments.forEach(c -> c.inferTypes(inferredTypes)); } + @Override + public String toString() { + StringBuilder sb = new StringBuilder("M("); + sb.append(this.classType); + sb.append(", "); + sb.append(this.methodName); + sb.append(", ["); + List arguments = this.arguments; + if (!arguments.isEmpty()) { + Iterator iterator = arguments.iterator(); + while (iterator.hasNext()) { + RefTypeOrTPHOrWildcardOrGeneric subtype = iterator.next().getSubtype(); + sb.append(subtype.toString()); + if (iterator.hasNext()) { + sb.append(", "); + } + } + } + sb.append("],("); + sb.append(this.returnType); + sb.append(", ["); + if (!arguments.isEmpty()) { + Iterator iterator = arguments.iterator(); + while (iterator.hasNext()) { + RefTypeOrTPHOrWildcardOrGeneric supertype = iterator.next().getSupertype(); + sb.append(supertype.toString()); + if (iterator.hasNext()) { + sb.append(", "); + } + } + } + sb.append("]))"); + return sb.toString(); + } + } diff --git a/src/de/dhbwstuttgart/strucTypes/constraint/SubTypeConstraint.java b/src/de/dhbwstuttgart/strucTypes/constraint/SubTypeConstraint.java index 1e4248e4..3eb2f483 100644 --- a/src/de/dhbwstuttgart/strucTypes/constraint/SubTypeConstraint.java +++ b/src/de/dhbwstuttgart/strucTypes/constraint/SubTypeConstraint.java @@ -62,4 +62,9 @@ public class SubTypeConstraint { return null; } + @Override + public String toString() { + return String.format("%s <* %s", this.subtype, this.supertype); + } + } diff --git a/src/de/dhbwstuttgart/strucTypes/exception/IllegalInterfaceTypeException.java b/src/de/dhbwstuttgart/strucTypes/exception/IllegalInterfaceTypeException.java new file mode 100644 index 00000000..d144925d --- /dev/null +++ b/src/de/dhbwstuttgart/strucTypes/exception/IllegalInterfaceTypeException.java @@ -0,0 +1,32 @@ +package de.dhbwstuttgart.strucTypes.exception; + +public class IllegalInterfaceTypeException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public IllegalInterfaceTypeException() { + // TODO Auto-generated constructor stub + } + + public IllegalInterfaceTypeException(String message) { + super(message); + // TODO Auto-generated constructor stub + } + + public IllegalInterfaceTypeException(Throwable cause) { + super(cause); + // TODO Auto-generated constructor stub + } + + public IllegalInterfaceTypeException(String message, Throwable cause) { + super(message, cause); + // TODO Auto-generated constructor stub + } + + public IllegalInterfaceTypeException(String message, Throwable cause, boolean enableSuppression, + boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + // TODO Auto-generated constructor stub + } + +} diff --git a/src/de/dhbwstuttgart/strucTypes/printutils/PrintConstraints.java b/src/de/dhbwstuttgart/strucTypes/printutils/PrintConstraints.java index ae9399b7..cbd086d6 100644 --- a/src/de/dhbwstuttgart/strucTypes/printutils/PrintConstraints.java +++ b/src/de/dhbwstuttgart/strucTypes/printutils/PrintConstraints.java @@ -2,16 +2,12 @@ package de.dhbwstuttgart.strucTypes.printutils; import java.util.List; -import de.dhbwstuttgart.strucTypes.DefaultASTVisitor; 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.type.RefType; -import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; -import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; -public class PrintConstraints extends DefaultASTVisitor { +public class PrintConstraints { public void print(ConstraintsSet constraintsSet) { printSubTypeConstraints(constraintsSet.getSubTypeConstraints()); @@ -21,67 +17,59 @@ public class PrintConstraints extends DefaultASTVisitor { public void printSubTypeConstraints(List constraints) { System.out.println("\n SubTypeConstraints:"); - constraints.forEach(c -> { - c.getSubtype().accept(this); - System.out.print(" <* "); - c.getSupertype().accept(this); - System.out.println(); - }); + constraints.forEach(System.out::println); + // constraints.forEach(c -> System.out + // .println(String.format("%s <* %s", c.getSubtype().toString(), + // c.getSupertype().toString()))); } public void printFieldConstraints(List constraints) { System.out.println("\n FieldConstraints:"); - constraints.forEach(c -> { - System.out.print("F("); - c.getClassType().accept(this); - System.out.print(", "); - System.out.print(c.getFieldName()); - System.out.print(", "); - c.getFieldType().accept(this); - System.out.println(")"); - }); + constraints.forEach(System.out::println); + // constraints.forEach(c -> System.out.println(String.format("F(%s, %s, + // %s)", c.getClassType().toString(), + // c.getFieldName(), c.getFieldType().toString()))); } public void printMethodConstraints(List constraints) { System.out.println("\n MethodConstraints:"); - constraints.forEach(c -> { - System.out.print("M("); - c.getClassType().accept(this); - System.out.print(", "); - System.out.print(c.getMethodName() + ", ["); - c.getArguments().forEach(a -> { - a.getSubtype().accept(this); - System.out.print(", "); - }); - System.out.print("],("); - c.getReturnType().accept(this); - System.out.print(", ["); - c.getArguments().forEach(a -> { - a.getSupertype().accept(this); - System.out.print(", "); - }); - System.out.print("]))"); - System.out.println(); - }); + constraints.forEach(System.out::println); + // constraints.forEach(c -> { + // StringBuilder sb = new StringBuilder("M("); + // sb.append(c.getClassType().toString()); + // sb.append(", "); + // sb.append(c.getMethodName()); + // sb.append(", ["); + // List arguments = c.getArguments(); + // if (!arguments.isEmpty()) { + // Iterator iterator = arguments.iterator(); + // while (iterator.hasNext()) { + // RefTypeOrTPHOrWildcardOrGeneric subtype = + // iterator.next().getSubtype(); + // sb.append(subtype.toString()); + // if (iterator.hasNext()) { + // sb.append(", "); + // } + // } + // } + // sb.append("],("); + // sb.append(c.getReturnType().toString()); + // sb.append(", ["); + // if (!arguments.isEmpty()) { + // Iterator iterator = arguments.iterator(); + // while (iterator.hasNext()) { + // RefTypeOrTPHOrWildcardOrGeneric supertype = + // iterator.next().getSupertype(); + // sb.append(supertype.toString()); + // if (iterator.hasNext()) { + // sb.append(", "); + // } + // } + // } + // sb.append("]))"); + // System.out.println(sb.toString()); + // }); } - @Override - public void visit(RefType refType) { - List paraList = refType.getParaList(); - System.out.print(refType.getName()); - if (!paraList.isEmpty()) { - System.out.print(" <"); - paraList.forEach(p -> { - p.accept(this); - System.out.print(", "); - }); - System.out.print(">"); - } - } - - @Override - public void visit(TypePlaceholder typePlaceholder) { - System.out.print(typePlaceholder); - } } diff --git a/src/de/dhbwstuttgart/strucTypes/printutils/SyntaxTreePrinter.java b/src/de/dhbwstuttgart/strucTypes/printutils/SyntaxTreePrinter.java index a85c9826..4f7774e1 100644 --- a/src/de/dhbwstuttgart/strucTypes/printutils/SyntaxTreePrinter.java +++ b/src/de/dhbwstuttgart/strucTypes/printutils/SyntaxTreePrinter.java @@ -1,11 +1,13 @@ package de.dhbwstuttgart.strucTypes.printutils; import java.util.Collection; +import java.util.Iterator; import java.util.List; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal; import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.strucTypes.InferredTypes; import de.dhbwstuttgart.syntaxtree.ASTVisitor; import de.dhbwstuttgart.syntaxtree.ClassOrInterface; import de.dhbwstuttgart.syntaxtree.Constructor; @@ -55,6 +57,16 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; public class SyntaxTreePrinter implements ASTVisitor { + private InferredTypes inferredTypes; + + public SyntaxTreePrinter() { + this(InferredTypes.EMPTY); + } + + public SyntaxTreePrinter(InferredTypes inferredTypes) { + this.inferredTypes = inferredTypes; + } + @Override public void visit(SourceFile sourceFile) { sourceFile.KlassenVektor.forEach(cl -> cl.accept(this)); @@ -69,13 +81,17 @@ public class SyntaxTreePrinter implements ASTVisitor { Collection implementedInterfaces = classOrInterface.getSuperInterfaces(); GenericDeclarationList generics = classOrInterface.getGenerics(); - System.out.print("class: " + className); - if (generics.iterator().hasNext()) { + System.out.print("\nclass: " + className); + Iterator iterator = generics.iterator(); + if (iterator.hasNext()) { System.out.print(" <"); - generics.forEach(g -> { + while (iterator.hasNext()) { + GenericTypeVar g = iterator.next(); g.accept(this); - System.out.print(", "); - }); + if (iterator.hasNext()) { + System.out.print(", "); + } + } System.out.print(">"); } System.out.println(); @@ -165,7 +181,9 @@ public class SyntaxTreePrinter implements ASTVisitor { @Override public void visit(Return aReturn) { RefTypeOrTPHOrWildcardOrGeneric type = aReturn.getType(); - System.out.println("returnType: " + type); + System.out.print("returnType: "); + type.accept(this); + System.out.println(); aReturn.retexpr.accept(this); } @@ -232,22 +250,16 @@ public class SyntaxTreePrinter implements ASTVisitor { @Override public void visit(RefType refType) { - List paraList = refType.getParaList(); - System.out.print(refType.getName()); - if (!paraList.isEmpty()) { - System.out.print(" <"); - paraList.forEach(p -> { - p.accept(this); - System.out.print(", "); - }); - System.out.print(">"); - } + System.out.print(refType); } @Override public void visit(TypePlaceholder typePlaceholder) { - String name = typePlaceholder.getName(); - System.out.print("TPH " + name); + if (inferredTypes.get(typePlaceholder) != null) { + inferredTypes.get(typePlaceholder).accept(this); + } else { + System.out.print(typePlaceholder); + } } @@ -271,7 +283,6 @@ public class SyntaxTreePrinter implements ASTVisitor { throw new NotImplementedException(); } - @Override public void visit(LambdaExpression lambdaExpression) { throw new NotImplementedException(); @@ -314,7 +325,6 @@ public class SyntaxTreePrinter implements ASTVisitor { System.out.println("empty statement"); } - @Override public void visit(Literal literal) { RefTypeOrTPHOrWildcardOrGeneric type = literal.getType(); @@ -345,9 +355,10 @@ public class SyntaxTreePrinter implements ASTVisitor { @Override public void visit(ExpressionReceiver expressionReceiver) { Expression expr = expressionReceiver.expr; - RefTypeOrTPHOrWildcardOrGeneric type = expressionReceiver.getType(); - System.out.print("expressionReceiverType: " + type); - System.out.print(" expressionReceiver: "); + // RefTypeOrTPHOrWildcardOrGeneric type = expressionReceiver.getType(); + // System.out.print("expressionReceiverType: "); + // type.accept(this); + // System.out.print(" expressionReceiver: "); expr.accept(this); } diff --git a/test/strucType/TestConstruct.java b/test/strucType/TestConstruct.java index f5222c71..bdd6c5aa 100644 --- a/test/strucType/TestConstruct.java +++ b/test/strucType/TestConstruct.java @@ -37,29 +37,33 @@ public class TestConstruct { String name = f.getName(); System.out.println("Filename: " + name); SourceFile sourceFile = compiler.sourceFiles.get(f); - //Print SourceFile Infos + // Print SourceFile Infos sourceFile.accept(new SyntaxTreePrinter()); - + StrucTYPE strucTYPE = new StrucTYPE(sourceFile); - - ConstraintsSet constraints = strucTYPE.getConstraints(); + + final ConstraintsSet constraints = strucTYPE.getConstraints(); + final InferredTypes inferredTypesType = strucTYPE.getInferredTypes(); + + System.out.println("\n--StrucTYPE--"); printConstraints.print(constraints); - - InferredTypes inferredTypes = strucTYPE.getInferredTypes(); - PrintInferredTypes.print(inferredTypes); - - Construct construct = new Construct(constraints, inferredTypes); - - List constructedInterfaces = construct.getConstructedInterfaces(); + PrintInferredTypes.print(inferredTypesType); + + Construct construct = new Construct(constraints, inferredTypesType); + + final List constructedInterfaces = construct.getConstructedInterfaces(); + final List subTypeConstraints = construct.getSubTypeConstraints(); + final InferredTypes inferredTypesConstruct = construct.getInferredTypes(); + final SyntaxTreePrinter syntaxTreePrinterInferred = new SyntaxTreePrinter(inferredTypesConstruct); + + System.out.println("\n--Construct--"); System.out.println("\nConstructed Interfaces:"); - constructedInterfaces.forEach(i-> i.accept(new SyntaxTreePrinter())); - - List subTypeConstraints = construct.getSubTypeConstraints(); + constructedInterfaces.forEach(i -> i.accept(syntaxTreePrinterInferred)); printConstraints.printSubTypeConstraints(subTypeConstraints); - - inferredTypes = construct.getInferredTypes(); - PrintInferredTypes.print(inferredTypes); - + PrintInferredTypes.print(inferredTypesConstruct); + System.out.println("\n--Inferred SysntaxTree--"); + sourceFile.accept(syntaxTreePrinterInferred); + System.out.println("____________________________________________________________________________"); } } diff --git a/test/strucType/TestStrucType.java b/test/strucType/TestStrucType.java index 8d7424c7..ae41b796 100644 --- a/test/strucType/TestStrucType.java +++ b/test/strucType/TestStrucType.java @@ -20,13 +20,13 @@ public class TestStrucType { @org.junit.Test public void test() throws ClassNotFoundException, IOException { ArrayList 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 + "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 + "testMethod.jav")); -// files.add(new File(rootDirectory + "testPaperExample.jav")); + files.add(new File(rootDirectory + "testFieldMethod.jav")); + files.add(new File(rootDirectory + "testMethod.jav")); + files.add(new File(rootDirectory + "testPaperExample.jav")); JavaTXCompiler compiler = new JavaTXCompiler(files); for (File f : compiler.sourceFiles.keySet()) { String name = f.getName(); diff --git a/test/strucType/TestSyntaxTreePrinter.java b/test/strucType/TestSyntaxTreePrinter.java index c1b5d04f..a967642f 100644 --- a/test/strucType/TestSyntaxTreePrinter.java +++ b/test/strucType/TestSyntaxTreePrinter.java @@ -15,18 +15,18 @@ public class TestSyntaxTreePrinter { @org.junit.Test public void test() throws ClassNotFoundException, IOException { ArrayList files = new ArrayList<>(); -// files.add(new File(rootDirectory + "testLocalVar.jav")); -// files.add(new File(rootDirectory + "testCast.jav")); + 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")); + 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); compiler.sourceFiles.keySet().forEach(f->{ String name = f.getName(); System.out.println("Filename: " + name); compiler.sourceFiles.get(f).accept(new SyntaxTreePrinter()); - System.out.println(); + System.out.println("________________________________________"); }); // this.printSyntaxTree(compiler); System.out.println("test end"); diff --git a/test/strucType/javFiles/testCast.jav b/test/strucType/javFiles/testCast.jav index 30ed4bc1..dd67bc23 100644 --- a/test/strucType/javFiles/testCast.jav +++ b/test/strucType/javFiles/testCast.jav @@ -1,10 +1,13 @@ package strucType.input; import strucType.typedtestclasses.A; import strucType.typedtestclasses.A2; -import strucType.typedtestclasses.B; +import strucType.typedtestclasses.C; -class C +class Cast { - mA(A x){return (A2)x; } - mB(c) { return (B)c; } + mA(A x){return (A2) x; } + + mC(a) { return (C) mC2(a); } + + mC2(c) { return (C) c; } } \ No newline at end of file diff --git a/test/strucType/javFiles/testMethod.jav b/test/strucType/javFiles/testMethod.jav index 8acb24bd..dd790868 100644 --- a/test/strucType/javFiles/testMethod.jav +++ b/test/strucType/javFiles/testMethod.jav @@ -6,9 +6,11 @@ class M { mM(x) { return mA(x); } - A mA(x) { return x.getA(); } + mA(x) { return x.getA(); } - A2 m(A a) { return a; } + m(A a) { return a; } + + A m2(a) { return a; } } \ No newline at end of file diff --git a/test/strucType/javFiles/testNew.jav b/test/strucType/javFiles/testNew.jav index 7ee62428..15218b98 100644 --- a/test/strucType/javFiles/testNew.jav +++ b/test/strucType/javFiles/testNew.jav @@ -1,10 +1,11 @@ package strucType.input; import strucType.typedtestclasses.A; import strucType.typedtestclasses.B; -class Neu +class N { a; mA(){ return new A(a); } mB(x){ return new B(x,a); } + mN(a) {return new N(a); } } \ No newline at end of file