forked from JavaTX/JavaCompilerCore
Fehler in UnifyTypeConvert beheben
This commit is contained in:
parent
a355fe7786
commit
98331af3c0
@ -17,19 +17,19 @@ import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class JavaTXCompiler {
|
public class JavaTXCompiler {
|
||||||
|
|
||||||
protected List<SourceFile> sourceFiles = new ArrayList<>();
|
protected List<SourceFile> sourceFiles = new ArrayList<>();
|
||||||
|
|
||||||
public ResultSet typeInference(){
|
public List<ResultSet> typeInference(){
|
||||||
List<ClassOrInterface> allClasses = new ArrayList<>();
|
List<ClassOrInterface> allClasses = new ArrayList<>();
|
||||||
for(SourceFile sf : sourceFiles){
|
for(SourceFile sf : sourceFiles){
|
||||||
allClasses.addAll(sf.getClasses());
|
allClasses.addAll(sf.getClasses());
|
||||||
}
|
}
|
||||||
FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses);
|
FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses);
|
||||||
ConstraintSet<Pair> cons = new ConstraintSet<>();
|
final ConstraintSet<Pair> cons = new TYPE(sourceFiles).getConstraints();
|
||||||
cons = new TYPE(sourceFiles).getConstraints();
|
|
||||||
ConstraintSet<UnifyPair> unifyCons = UnifyTypeFactory.convert(cons);
|
ConstraintSet<UnifyPair> unifyCons = UnifyTypeFactory.convert(cons);
|
||||||
|
|
||||||
TypeUnify unify = new TypeUnify();
|
TypeUnify unify = new TypeUnify();
|
||||||
@ -45,9 +45,9 @@ public class JavaTXCompiler {
|
|||||||
System.out.println("RESULT: " + result);
|
System.out.println("RESULT: " + result);
|
||||||
results.addAll(result);
|
results.addAll(result);
|
||||||
}
|
}
|
||||||
return new ResultSet(UnifyTypeFactory.convert(results.iterator().next(), generateTPHMap(cons)));
|
return results.stream().map((unifyPairs ->
|
||||||
//return new ResultSet(new HashSet<>(Arrays.asList(UnifyTypeFactory.convert(results, generateTPHMap(cons)).iterator().next())));
|
new ResultSet(UnifyTypeFactory.convert(unifyPairs, generateTPHMap(cons))))).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Map<String, TypePlaceholder> generateTPHMap(ConstraintSet<Pair> constraints){
|
private Map<String, TypePlaceholder> generateTPHMap(ConstraintSet<Pair> constraints){
|
||||||
HashMap<String, TypePlaceholder> ret = new HashMap<>();
|
HashMap<String, TypePlaceholder> ret = new HashMap<>();
|
||||||
|
@ -163,12 +163,12 @@ public class UnifyTypeFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static RefTypeOrTPHOrWildcardOrGeneric convert(SuperType t, Map<String,TypePlaceholder> tphs) {
|
public static RefTypeOrTPHOrWildcardOrGeneric convert(SuperType t, Map<String,TypePlaceholder> tphs) {
|
||||||
RefType innerType = new RefType(new JavaClassName(t.getSuperedType().getName()), new NullToken());
|
RefTypeOrTPHOrWildcardOrGeneric innerType = convert(t.getSuperedType(), tphs);
|
||||||
return new SuperWildcardType(innerType, new NullToken());
|
return new SuperWildcardType(innerType, new NullToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static RefTypeOrTPHOrWildcardOrGeneric convert(ExtendsType t, Map<String,TypePlaceholder> tphs) {
|
public static RefTypeOrTPHOrWildcardOrGeneric convert(ExtendsType t, Map<String,TypePlaceholder> tphs) {
|
||||||
RefType innerType = new RefType(new JavaClassName(t.getExtendedType().getName()), new NullToken());
|
RefTypeOrTPHOrWildcardOrGeneric innerType = convert(t.getExtendedType(), tphs);
|
||||||
return new ExtendsWildcardType(innerType, new NullToken());
|
return new ExtendsWildcardType(innerType, new NullToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ public class ExtendsWildcardType extends WildcardType{
|
|||||||
* Author: Arne Lüdtke<br/>
|
* Author: Arne Lüdtke<br/>
|
||||||
* Standard Konstruktor für eine ExtendsWildcard
|
* Standard Konstruktor für eine ExtendsWildcard
|
||||||
*/
|
*/
|
||||||
public ExtendsWildcardType (RefType extendsType, Token offset)
|
public ExtendsWildcardType (RefTypeOrTPHOrWildcardOrGeneric extendsType, Token offset)
|
||||||
{
|
{
|
||||||
super(extendsType, offset);
|
super(extendsType, offset);
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ public class SuperWildcardType extends WildcardType{
|
|||||||
* Author: Arne Lüdtke<br/>
|
* Author: Arne Lüdtke<br/>
|
||||||
* Standard Konstruktor für eine SuperWildcard
|
* Standard Konstruktor für eine SuperWildcard
|
||||||
*/
|
*/
|
||||||
public SuperWildcardType( RefType innerType, Token offset)
|
public SuperWildcardType( RefTypeOrTPHOrWildcardOrGeneric innerType, Token offset)
|
||||||
{
|
{
|
||||||
super(innerType, offset);
|
super(innerType, offset);
|
||||||
}
|
}
|
||||||
|
@ -11,19 +11,19 @@ import org.antlr.v4.runtime.Token;
|
|||||||
|
|
||||||
public abstract class WildcardType extends RefTypeOrTPHOrWildcardOrGeneric {
|
public abstract class WildcardType extends RefTypeOrTPHOrWildcardOrGeneric {
|
||||||
|
|
||||||
protected RefType innerType = null;
|
protected RefTypeOrTPHOrWildcardOrGeneric innerType = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Author: Arne Lüdtke<br/>
|
* Author: Arne Lüdtke<br/>
|
||||||
* Standard Konstruktor für eine Wildcard
|
* Standard Konstruktor für eine Wildcard
|
||||||
*/
|
*/
|
||||||
public WildcardType(RefType innerType, Token offset)
|
public WildcardType(RefTypeOrTPHOrWildcardOrGeneric innerType, Token offset)
|
||||||
{
|
{
|
||||||
super(offset);
|
super(offset);
|
||||||
this.innerType = innerType;
|
this.innerType = innerType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public RefType getInnerType(){
|
public RefTypeOrTPHOrWildcardOrGeneric getInnerType(){
|
||||||
return innerType;
|
return innerType;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package de.dhbwstuttgart.typedeployment;
|
package de.dhbwstuttgart.typedeployment;
|
||||||
|
|
||||||
import de.dhbwstuttgart.exceptions.NotImplementedException;
|
|
||||||
import de.dhbwstuttgart.syntaxtree.*;
|
import de.dhbwstuttgart.syntaxtree.*;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.*;
|
import de.dhbwstuttgart.syntaxtree.type.*;
|
||||||
import de.dhbwstuttgart.typeinference.result.*;
|
import de.dhbwstuttgart.typeinference.result.*;
|
||||||
@ -31,76 +30,10 @@ public class TypeInsertFactory {
|
|||||||
public static TypeInsert createInsertPoints(RefTypeOrTPHOrWildcardOrGeneric type, Token offset, ClassOrInterface cl, Method m,
|
public static TypeInsert createInsertPoints(RefTypeOrTPHOrWildcardOrGeneric type, Token offset, ClassOrInterface cl, Method m,
|
||||||
ResultSet resultSet) {
|
ResultSet resultSet) {
|
||||||
Set<TypeInsertPoint> ret = new HashSet<>();
|
Set<TypeInsertPoint> ret = new HashSet<>();
|
||||||
TypeInsertPoint insertPoint = null;
|
|
||||||
ResolvedType resolvedType = resultSet.resolveType(type);
|
ResolvedType resolvedType = resultSet.resolveType(type);
|
||||||
insertPoint = new TypeInsertPoint(offset,
|
TypeInsertPoint insertPoint = new TypeInsertPoint(offset,
|
||||||
new TypeToInsertString(resolvedType.resolvedType).insert);
|
new TypeToInsertString(resolvedType.resolvedType).insert);
|
||||||
return new TypeInsert(insertPoint, new HashSet<>(Arrays.asList(createGenericInsert(resolvedType.additionalGenerics, cl, m))));
|
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<GenericInsertPair> 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<GenericInsertPair> toInsert, ClassOrInterface cl, Method m){
|
private static TypeInsertPoint createGenericInsert(Set<GenericInsertPair> toInsert, ClassOrInterface cl, Method m){
|
||||||
|
@ -29,17 +29,21 @@ class TypeInsertPlacerClass extends AbstractASTWalker{
|
|||||||
protected final ResultSet results;
|
protected final ResultSet results;
|
||||||
protected final ClassOrInterface cl;
|
protected final ClassOrInterface cl;
|
||||||
public final Set<TypeInsert> inserts = new HashSet<>();
|
public final Set<TypeInsert> inserts = new HashSet<>();
|
||||||
|
private Method method;
|
||||||
|
|
||||||
TypeInsertPlacerClass(ClassOrInterface forClass, ResultSet withResults){
|
TypeInsertPlacerClass(ClassOrInterface forClass, ResultSet withResults){
|
||||||
this.cl = forClass;
|
this.cl = forClass;
|
||||||
|
this.method = null;
|
||||||
this.results = withResults;
|
this.results = withResults;
|
||||||
forClass.accept(this);
|
forClass.accept(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(Method method) {
|
public void visit(Method method) {
|
||||||
|
this.method = method;
|
||||||
|
if(method.getReturnType() instanceof TypePlaceholder)
|
||||||
inserts.add(TypeInsertFactory.createInsertPoints(
|
inserts.add(TypeInsertFactory.createInsertPoints(
|
||||||
method.getReturnType(), method.getReturnType().getOffset(), cl, null, results));
|
method.getReturnType(), method.getReturnType().getOffset(), cl, method, results));
|
||||||
super.visit(method);
|
super.visit(method);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,7 +51,7 @@ class TypeInsertPlacerClass extends AbstractASTWalker{
|
|||||||
public void visit(Field field) {
|
public void visit(Field field) {
|
||||||
if(field.getType() instanceof TypePlaceholder){
|
if(field.getType() instanceof TypePlaceholder){
|
||||||
inserts.add(TypeInsertFactory.createInsertPoints(
|
inserts.add(TypeInsertFactory.createInsertPoints(
|
||||||
field.getType(), field.getType().getOffset(), cl, null, results));
|
field.getType(), field.getType().getOffset(), cl, method, results));
|
||||||
}
|
}
|
||||||
super.visit(field);
|
super.visit(field);
|
||||||
}
|
}
|
||||||
@ -55,8 +59,9 @@ class TypeInsertPlacerClass extends AbstractASTWalker{
|
|||||||
@Override
|
@Override
|
||||||
public void visit(ParameterList params) {
|
public void visit(ParameterList params) {
|
||||||
for(FormalParameter param : params){
|
for(FormalParameter param : params){
|
||||||
|
if(param.getType() instanceof TypePlaceholder)
|
||||||
inserts.add(TypeInsertFactory.createInsertPoints(
|
inserts.add(TypeInsertFactory.createInsertPoints(
|
||||||
param.getType(), param.getType().getOffset(), cl, null, results));
|
param.getType(), param.getType().getOffset(), cl, method, results));
|
||||||
}
|
}
|
||||||
super.visit(params);
|
super.visit(params);
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import de.dhbwstuttgart.syntaxtree.SourceFile;
|
|||||||
import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter;
|
import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter;
|
||||||
import de.dhbwstuttgart.typedeployment.TypeInsert;
|
import de.dhbwstuttgart.typedeployment.TypeInsert;
|
||||||
import de.dhbwstuttgart.typedeployment.TypeInsertFactory;
|
import de.dhbwstuttgart.typedeployment.TypeInsertFactory;
|
||||||
|
import de.dhbwstuttgart.typeinference.result.ResultSet;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -38,10 +39,12 @@ public class JavaTXCompilerTest extends JavaTXCompiler {
|
|||||||
for(File f : filesToTest){
|
for(File f : filesToTest){
|
||||||
SourceFile sf = this.parse(f);
|
SourceFile sf = this.parse(f);
|
||||||
System.out.println(ASTTypePrinter.print(this.sourceFiles.get(sourceFiles.size()-1)));
|
System.out.println(ASTTypePrinter.print(this.sourceFiles.get(sourceFiles.size()-1)));
|
||||||
Set<TypeInsert> result = TypeInsertFactory.createTypeInsertPoints(sf, this.typeInference());
|
for(ResultSet resultSet : this.typeInference()){
|
||||||
String content = readFile(f.getPath(), StandardCharsets.UTF_8);
|
Set<TypeInsert> result = TypeInsertFactory.createTypeInsertPoints(sf, resultSet);
|
||||||
for(TypeInsert tip : result){
|
String content = readFile(f.getPath(), StandardCharsets.UTF_8);
|
||||||
System.out.println(tip.insert(content));
|
for(TypeInsert tip : result){
|
||||||
|
System.out.println(tip.insert(content));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user