diff --git a/src/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/de/dhbwstuttgart/core/JavaTXCompiler.java index 0968fe9d..196354e1 100644 --- a/src/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -17,19 +17,19 @@ import de.dhbwstuttgart.typeinference.unify.model.UnifyPair; import java.io.File; import java.io.IOException; import java.util.*; +import java.util.stream.Collectors; public class JavaTXCompiler { protected List sourceFiles = new ArrayList<>(); - public ResultSet typeInference(){ + public List typeInference(){ List allClasses = new ArrayList<>(); for(SourceFile sf : sourceFiles){ allClasses.addAll(sf.getClasses()); } FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses); - ConstraintSet cons = new ConstraintSet<>(); - cons = new TYPE(sourceFiles).getConstraints(); + final ConstraintSet cons = new TYPE(sourceFiles).getConstraints(); ConstraintSet unifyCons = UnifyTypeFactory.convert(cons); TypeUnify unify = new TypeUnify(); @@ -45,9 +45,9 @@ public class JavaTXCompiler { System.out.println("RESULT: " + result); results.addAll(result); } - return new ResultSet(UnifyTypeFactory.convert(results.iterator().next(), generateTPHMap(cons))); - //return new ResultSet(new HashSet<>(Arrays.asList(UnifyTypeFactory.convert(results, generateTPHMap(cons)).iterator().next()))); - } + return results.stream().map((unifyPairs -> + new ResultSet(UnifyTypeFactory.convert(unifyPairs, generateTPHMap(cons))))).collect(Collectors.toList()); + } private Map generateTPHMap(ConstraintSet constraints){ HashMap ret = new HashMap<>(); diff --git a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index 2e1ce08a..b13db501 100644 --- a/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -163,12 +163,12 @@ public class UnifyTypeFactory { } public static RefTypeOrTPHOrWildcardOrGeneric convert(SuperType t, Map tphs) { - RefType innerType = new RefType(new JavaClassName(t.getSuperedType().getName()), new NullToken()); + RefTypeOrTPHOrWildcardOrGeneric innerType = convert(t.getSuperedType(), tphs); return new SuperWildcardType(innerType, new NullToken()); } public static RefTypeOrTPHOrWildcardOrGeneric convert(ExtendsType t, Map tphs) { - RefType innerType = new RefType(new JavaClassName(t.getExtendedType().getName()), new NullToken()); + RefTypeOrTPHOrWildcardOrGeneric innerType = convert(t.getExtendedType(), tphs); return new ExtendsWildcardType(innerType, new NullToken()); } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java index f283362a..1852b323 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/ExtendsWildcardType.java @@ -19,7 +19,7 @@ public class ExtendsWildcardType extends WildcardType{ * Author: Arne Lüdtke
* Standard Konstruktor für eine ExtendsWildcard */ - public ExtendsWildcardType (RefType extendsType, Token offset) + public ExtendsWildcardType (RefTypeOrTPHOrWildcardOrGeneric extendsType, Token offset) { super(extendsType, offset); } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java index d1945f80..3fc8ffb9 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/SuperWildcardType.java @@ -19,7 +19,7 @@ public class SuperWildcardType extends WildcardType{ * Author: Arne Lüdtke
* Standard Konstruktor für eine SuperWildcard */ - public SuperWildcardType( RefType innerType, Token offset) + public SuperWildcardType( RefTypeOrTPHOrWildcardOrGeneric innerType, Token offset) { super(innerType, offset); } diff --git a/src/de/dhbwstuttgart/syntaxtree/type/WildcardType.java b/src/de/dhbwstuttgart/syntaxtree/type/WildcardType.java index 0c9fb1ed..208138b6 100755 --- a/src/de/dhbwstuttgart/syntaxtree/type/WildcardType.java +++ b/src/de/dhbwstuttgart/syntaxtree/type/WildcardType.java @@ -11,19 +11,19 @@ import org.antlr.v4.runtime.Token; public abstract class WildcardType extends RefTypeOrTPHOrWildcardOrGeneric { - protected RefType innerType = null; + protected RefTypeOrTPHOrWildcardOrGeneric innerType = null; /** * Author: Arne Lüdtke
* Standard Konstruktor für eine Wildcard */ - public WildcardType(RefType innerType, Token offset) + public WildcardType(RefTypeOrTPHOrWildcardOrGeneric innerType, Token offset) { super(offset); this.innerType = innerType; } - public RefType getInnerType(){ + public RefTypeOrTPHOrWildcardOrGeneric getInnerType(){ return innerType; } diff --git a/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java b/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java index 19a5deae..737ef669 100644 --- a/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java +++ b/src/de/dhbwstuttgart/typedeployment/TypeInsertFactory.java @@ -1,6 +1,5 @@ package de.dhbwstuttgart.typedeployment; -import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.type.*; import de.dhbwstuttgart.typeinference.result.*; @@ -31,76 +30,10 @@ public class TypeInsertFactory { public static TypeInsert createInsertPoints(RefTypeOrTPHOrWildcardOrGeneric type, Token offset, ClassOrInterface cl, Method m, ResultSet resultSet) { Set ret = new HashSet<>(); - TypeInsertPoint insertPoint = null; ResolvedType resolvedType = resultSet.resolveType(type); - insertPoint = new TypeInsertPoint(offset, + TypeInsertPoint insertPoint = new TypeInsertPoint(offset, new TypeToInsertString(resolvedType.resolvedType).insert); return new TypeInsert(insertPoint, new HashSet<>(Arrays.asList(createGenericInsert(resolvedType.additionalGenerics, cl, m)))); - /* - for (Pair pair : pairs) { - RefTypeOrTPHOrWildcardOrGeneric relatedType = null; - if (pair.TA1.equals(type)) { - relatedType = pair.TA2; - } else if (pair.TA2.equals(type)) { - relatedType = pair.TA1; - } - if (relatedType != null) { - //Es wurde ein direkter Treffer gefunden: - if (pair.OperatorEqual()) { //resolved ist vom Typ RefType - RefType resolved = ((RefType) relatedType); - String insert = createInsertString(resolved, additionalInserts); - insertPoint = new TypeInsertPoint(offset, insert); - break; - } else { //Ansonsten ist es ein TPH - //additionalInserts.add((TypePlaceholder) relatedType); //Sollte nicht notwendig sein - //Dann wurde er nicht aufgelöst und es kann nur der TPH als Generic eingesetzt werden: - insertPoint = new TypeInsertPoint(offset, ((TypePlaceholder) type).getName()); - break; - } - } - } - if(insertPoint == null ){ //Dann muss es ein TPH sein, welcher nicht im Result auftaucht - if(type instanceof TypePlaceholder){ - insertPoint = new TypeInsertPoint(offset, ((TypePlaceholder) type).getName()); - additionalInserts.add(((TypePlaceholder) type)); - }else if(type instanceof GenericRefType){ - insertPoint = new TypeInsertPoint(offset, ((GenericRefType) type).getName().toString()); - }else if(type instanceof RefType){ - insertPoint = new TypeInsertPoint(offset, ((RefType) type).getName().toString()); - }else throw new NotImplementedException(); - } - - //Alle Bounds finden: - Set newGenerics = new HashSet<>(); - boolean added = true; - while(added){ - //Fügt alle TPHs an, welche mit den additionalInserts in Verbindung stehen. - added = false; - for(Pair pair : pairs){ - if (additionalInserts.contains(pair.TA1) || additionalInserts.contains(pair.TA2)) { - newGenerics.add(new GenericInsertPair(pair)); - added |= additionalInserts.add((TypePlaceholder) pair.TA1); - added |= additionalInserts.add((TypePlaceholder) pair.TA2); - } - } - } - - //Fügt noch die Additional Inserts an, welche mit nichts in Verbindung stehen: - for(TypePlaceholder additionalTPH : additionalInserts){ - boolean inside = false; - for(GenericInsertPair p :newGenerics){ - if(p.contains(additionalTPH)){ - inside = true; - break; - } - } - if(! inside)newGenerics.add(new GenericInsertPair(additionalTPH, null)); - } - if(newGenerics.size() > 0) - ret.add(createGenericInsert(newGenerics, cl, m)); - - //return ret; - */ } private static TypeInsertPoint createGenericInsert(Set toInsert, ClassOrInterface cl, Method m){ diff --git a/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java b/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java index 467172c4..8dea5748 100644 --- a/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java +++ b/src/de/dhbwstuttgart/typedeployment/TypeInsertPlacer.java @@ -29,17 +29,21 @@ class TypeInsertPlacerClass extends AbstractASTWalker{ protected final ResultSet results; protected final ClassOrInterface cl; public final Set inserts = new HashSet<>(); + private Method method; TypeInsertPlacerClass(ClassOrInterface forClass, ResultSet withResults){ this.cl = forClass; + this.method = null; this.results = withResults; forClass.accept(this); } @Override public void visit(Method method) { + this.method = method; + if(method.getReturnType() instanceof TypePlaceholder) inserts.add(TypeInsertFactory.createInsertPoints( - method.getReturnType(), method.getReturnType().getOffset(), cl, null, results)); + method.getReturnType(), method.getReturnType().getOffset(), cl, method, results)); super.visit(method); } @@ -47,7 +51,7 @@ class TypeInsertPlacerClass extends AbstractASTWalker{ public void visit(Field field) { if(field.getType() instanceof TypePlaceholder){ inserts.add(TypeInsertFactory.createInsertPoints( - field.getType(), field.getType().getOffset(), cl, null, results)); + field.getType(), field.getType().getOffset(), cl, method, results)); } super.visit(field); } @@ -55,8 +59,9 @@ class TypeInsertPlacerClass extends AbstractASTWalker{ @Override public void visit(ParameterList params) { for(FormalParameter param : params){ + if(param.getType() instanceof TypePlaceholder) inserts.add(TypeInsertFactory.createInsertPoints( - param.getType(), param.getType().getOffset(), cl, null, results)); + param.getType(), param.getType().getOffset(), cl, method, results)); } super.visit(params); } diff --git a/test/typeinference/JavaTXCompilerTest.java b/test/typeinference/JavaTXCompilerTest.java index 2f9395fd..ec070d3d 100644 --- a/test/typeinference/JavaTXCompilerTest.java +++ b/test/typeinference/JavaTXCompilerTest.java @@ -5,6 +5,7 @@ import de.dhbwstuttgart.syntaxtree.SourceFile; import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter; import de.dhbwstuttgart.typedeployment.TypeInsert; import de.dhbwstuttgart.typedeployment.TypeInsertFactory; +import de.dhbwstuttgart.typeinference.result.ResultSet; import org.junit.Test; import java.io.File; @@ -38,10 +39,12 @@ public class JavaTXCompilerTest extends JavaTXCompiler { for(File f : filesToTest){ SourceFile sf = this.parse(f); System.out.println(ASTTypePrinter.print(this.sourceFiles.get(sourceFiles.size()-1))); - Set result = TypeInsertFactory.createTypeInsertPoints(sf, this.typeInference()); - String content = readFile(f.getPath(), StandardCharsets.UTF_8); - for(TypeInsert tip : result){ - System.out.println(tip.insert(content)); + for(ResultSet resultSet : this.typeInference()){ + Set result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet); + String content = readFile(f.getPath(), StandardCharsets.UTF_8); + for(TypeInsert tip : result){ + System.out.println(tip.insert(content)); + } } }