TYPEExpr fixed for newClass.

This commit is contained in:
Aldaron7 2018-05-30 23:50:32 +02:00
parent e972282620
commit f44f08b895
14 changed files with 218 additions and 92 deletions

View File

@ -86,6 +86,11 @@ public class Construct extends DefaultASTVisitor {
String.format("%s is not a valid type to generate an interface for.", refType));
}
@Override
public void visit(GenericRefType genericRefType) {
System.out.println(String.format("%s is not a valid type to generate an interface for.", genericRefType));
}
private ClassOrInterface constructInterface(TypePlaceholder i, JavaClassName name) {
List<Field> fielddecl = new ArrayList<>();
List<Method> methods = new ArrayList<>();

View File

@ -9,15 +9,15 @@ import java.util.Set;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
public class InferredTypes implements Map<TypePlaceholder, RefTypeOrTPHOrWildcardOrGeneric> {
public class InferredTypes implements Map<RefTypeOrTPHOrWildcardOrGeneric, RefTypeOrTPHOrWildcardOrGeneric> {
private Map<TypePlaceholder, RefTypeOrTPHOrWildcardOrGeneric> inferredTypes = new HashMap<>();
private Map<RefTypeOrTPHOrWildcardOrGeneric, RefTypeOrTPHOrWildcardOrGeneric> inferredTypes = new HashMap<>();
public static final InferredTypes EMPTY = new InferredTypes();
public void resolveTransitiveTypes() {
Set<TypePlaceholder> keySet = this.inferredTypes.keySet();
for (TypePlaceholder key : keySet) {
Set<RefTypeOrTPHOrWildcardOrGeneric> keySet = this.inferredTypes.keySet();
for (RefTypeOrTPHOrWildcardOrGeneric key : keySet) {
RefTypeOrTPHOrWildcardOrGeneric value = this.inferredTypes.get(key);
if (value instanceof TypePlaceholder && keySet.contains(value)) {
this.inferredTypes.put(key, this.inferredTypes.get(value));
@ -51,7 +51,7 @@ public class InferredTypes implements Map<TypePlaceholder, RefTypeOrTPHOrWildcar
}
@Override
public RefTypeOrTPHOrWildcardOrGeneric put(TypePlaceholder key, RefTypeOrTPHOrWildcardOrGeneric value) {
public RefTypeOrTPHOrWildcardOrGeneric put(RefTypeOrTPHOrWildcardOrGeneric key, RefTypeOrTPHOrWildcardOrGeneric value) {
return inferredTypes.put(key, value);
}
@ -61,7 +61,7 @@ public class InferredTypes implements Map<TypePlaceholder, RefTypeOrTPHOrWildcar
}
@Override
public void putAll(Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> m) {
public void putAll(Map<? extends RefTypeOrTPHOrWildcardOrGeneric, ? extends RefTypeOrTPHOrWildcardOrGeneric> m) {
inferredTypes.putAll(m);
}
@ -71,7 +71,7 @@ public class InferredTypes implements Map<TypePlaceholder, RefTypeOrTPHOrWildcar
}
@Override
public Set<TypePlaceholder> keySet() {
public Set<RefTypeOrTPHOrWildcardOrGeneric> keySet() {
return inferredTypes.keySet();
}
@ -81,7 +81,7 @@ public class InferredTypes implements Map<TypePlaceholder, RefTypeOrTPHOrWildcar
}
@Override
public Set<java.util.Map.Entry<TypePlaceholder, RefTypeOrTPHOrWildcardOrGeneric>> entrySet() {
public Set<java.util.Map.Entry<RefTypeOrTPHOrWildcardOrGeneric, RefTypeOrTPHOrWildcardOrGeneric>> entrySet() {
return inferredTypes.entrySet();
}
@ -92,10 +92,10 @@ public class InferredTypes implements Map<TypePlaceholder, RefTypeOrTPHOrWildcar
@Override
public String toString() {
String s = "[";
Iterator<TypePlaceholder> iterator = this.inferredTypes.keySet().iterator();
Iterator<RefTypeOrTPHOrWildcardOrGeneric> iterator = this.inferredTypes.keySet().iterator();
while (iterator.hasNext()) {
TypePlaceholder tph = iterator.next();
s += tph + " -> " + inferredTypes.get(tph);
RefTypeOrTPHOrWildcardOrGeneric type = iterator.next();
s += type + " -> " + inferredTypes.get(type);
if (iterator.hasNext()) {
s += ", ";
}

View File

@ -56,8 +56,9 @@ public class StrucTYPE extends DefaultASTVisitor {
this.inferredTypes.putAll(typeExpr.getInferredTypes());
this.inferredTypes.resolveTransitiveTypes();
this.constraintsSet.addConstraintsSet(typeExpr.getConstraints());
this.constraintsSet.inferTypes(this.inferredTypes);
ConstraintsSet constraints = typeExpr.getConstraints();
constraints.inferTypes(this.inferredTypes);
this.constraintsSet.addConstraintsSet(constraints);
}
}

View File

@ -2,6 +2,7 @@ package de.dhbwstuttgart.strucTypes;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet;
import de.dhbwstuttgart.strucTypes.constraint.FieldConstraint;
@ -14,6 +15,7 @@ import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
import de.dhbwstuttgart.syntaxtree.statement.BinaryExpr;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.statement.CastExpr;
import de.dhbwstuttgart.syntaxtree.statement.Expression;
@ -118,21 +120,25 @@ public class TYPEExpr extends DefaultASTVisitor {
}
List<Expression> arguments = methodCall.getArgumentList().getArguments();
if (!methodTypeVisitor.isTypeVariable()
&& methodTypeVisitor.getMethod(methodCall.name, arguments.size()).isPresent()) {
methodTypeVisitor.getMethod(methodCall.name, arguments.size()).ifPresent(m -> {
Optional<Method> method = methodTypeVisitor.getMethod(methodCall.name, arguments.size());
if (!methodTypeVisitor.isTypeVariable() && method.isPresent()) {
Method m = method.get();
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(), 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);
}
// System.out.println("\ninferred grt:");
// System.out.println("map: " + methodTypeVisitor.getGrtToTphMap());
// System.out.println("generics: " + methodTypeVisitor.getGenerics());
this.constraints.inferTypes(methodTypeVisitor.getGrtToTphMap());
}
@Override
@ -160,6 +166,10 @@ public class TYPEExpr extends DefaultASTVisitor {
} else {
type.accept(typeExtract);
// implement generics nicht nötig
// ClassOrInterface orDefault =
// typedClasses.getOrDefault(type.getName(),
// ClassOrInterfaceFactory.createClass(type).get());
// orDefault.accept(typeExtract);
}
this.createNewClassSubTypeConstraints(newClass, typeExtract);
}
@ -177,10 +187,13 @@ public class TYPEExpr extends DefaultASTVisitor {
// List of Generics of newClass
List<RefTypeOrTPHOrWildcardOrGeneric> paraList = ((RefType) newClass.getType()).getParaList();
List<FormalParameter> formalparalist = typeExtract.getConstructors().get(0).getParameterList().getFormalparalist();
List<FormalParameter> formalparalist = typeExtract.getConstructors().get(0).getParameterList()
.getFormalparalist();
for (int i = 0; i < argumentsSize; i++) {
// Bei Fields wird kein GenericRefType sondern Object als Typ geparst.
// SubTypeConstraint subTypeConstraint = new SubTypeConstraint(arguments.get(i).getType(),
// Bei Fields wird kein GenericRefType sondern Object als Typ
// geparst.
// SubTypeConstraint subTypeConstraint = new
// SubTypeConstraint(arguments.get(i).getType(),
// fields.get(i).getType());
// Es darf nur einen Konstruktor mit allen Feldern geben.
@ -189,6 +202,11 @@ public class TYPEExpr extends DefaultASTVisitor {
SubTypeConstraint subTypeConstraint = new SubTypeConstraint(arguments.get(i).getType(), supertype);
this.constraints.addConstraint(subTypeConstraint);
}
typeExtract.getSubTypeConstraints().forEach(this.constraints::addConstraint);
// System.out.println("\ninferred grt:");
// System.out.println("map: " + typeExtract.getGrtToTphMap());
// System.out.println("generics: " + typeExtract.getGenerics());
this.constraints.inferTypes(typeExtract.getGrtToTphMap());
}
@Override
@ -231,4 +249,8 @@ public class TYPEExpr extends DefaultASTVisitor {
public void visit(Literal literal) {
}
@Override
public void visit(BinaryExpr binary) {
}
}

View File

@ -76,6 +76,39 @@ public class SubTypeConstraint {
return null;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((subtype == null) ? 0 : subtype.hashCode());
result = prime * result + ((supertype == null) ? 0 : supertype.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SubTypeConstraint other = (SubTypeConstraint) obj;
if (subtype == null) {
if (other.subtype != null)
return false;
} else if (!subtype.equals(other.subtype))
return false;
if (supertype == null) {
if (other.supertype != null)
return false;
} else if (!supertype.equals(other.supertype))
return false;
return true;
}
@Override
public String toString() {
return String.format("%s <* %s", this.subtype, this.supertype);

View File

@ -312,7 +312,10 @@ public class SyntaxTreePrinter implements ASTVisitor {
@Override
public void visit(BinaryExpr binary) {
throw new NotImplementedException();
System.out.println("binary");
// binary.lexpr.accept(this);
// System.out.println(binary.operation);
// binary.rexpr.accept(this);
}
@Override

View File

@ -206,8 +206,9 @@ public class InferTypes implements ASTReturnVisitor {
}
@Override
public GenericRefType visit(GenericRefType genericRefType) {
return genericRefType;
public RefTypeOrTPHOrWildcardOrGeneric visit(GenericRefType genericRefType) {
RefTypeOrTPHOrWildcardOrGeneric inferredType = inferredTypes.infer(genericRefType);
return inferredType;
}
@Override
@ -229,8 +230,7 @@ public class InferTypes implements ASTReturnVisitor {
@Override
public BinaryExpr visit(BinaryExpr binary) {
throw new NotImplementedException();
return binary;
}
@Override

View File

@ -1,18 +1,23 @@
package de.dhbwstuttgart.strucTypes.visitor;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.strucTypes.InferredTypes;
import de.dhbwstuttgart.strucTypes.classorinterface.ClassOrInterfaceFactory;
import de.dhbwstuttgart.strucTypes.constraint.SubTypeConstraint;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Constructor;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.Method;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
/**
@ -28,6 +33,8 @@ public class TypeExtract extends DefaultASTVisitor {
private List<Method> methods = new ArrayList<>();
private List<Constructor> constructors = new ArrayList<>();
private List<GenericTypeVar> generics = new ArrayList<>();
private InferredTypes grtToTphMap = InferredTypes.EMPTY;
private List<SubTypeConstraint> subTypeConstraints = new ArrayList<>();
private boolean typeVariable = false;
private boolean initialClass = true;
@ -47,6 +54,14 @@ public class TypeExtract extends DefaultASTVisitor {
return generics;
}
public InferredTypes getGrtToTphMap() {
return grtToTphMap;
}
public List<SubTypeConstraint> getSubTypeConstraints() {
return subTypeConstraints;
}
public Optional<Field> getField(String fieldName) {
return this.fields.stream().filter(f -> f.getName().equals(fieldName))
// keine statische Polymorphie zugelassen
@ -85,7 +100,26 @@ public class TypeExtract extends DefaultASTVisitor {
@Override
public void visit(RefType refType) {
this.typeVariable = false;
ClassOrInterfaceFactory.createClass(refType).ifPresent(c -> c.accept(this));
Optional<ClassOrInterface> createClass = ClassOrInterfaceFactory.createClass(refType);
// refType und createClass haben evtl. unterschiedliche
// Klassenparameter (refType -> tph| createClass -> gtv). Diese werden
// in grtToTphMap gespeichert.
if (createClass.isPresent()) {
ClassOrInterface cls = createClass.get();
cls.getGenerics().accept(this);
Iterator<GenericTypeVar> iteratorGtv = cls.getGenerics().iterator();
Iterator<RefTypeOrTPHOrWildcardOrGeneric> iteratorTPH = refType.getParaList().iterator();
while (iteratorGtv.hasNext()) {
GenericTypeVar nextGtv = iteratorGtv.next();
GenericRefType grt = new GenericRefType(nextGtv.getName(), nextGtv.getOffset());
RefTypeOrTPHOrWildcardOrGeneric nextTPH = iteratorTPH.next();
if (nextTPH instanceof TypePlaceholder) {
TypePlaceholder tph = (TypePlaceholder) nextTPH;
this.grtToTphMap.put(grt, tph);
}
}
cls.accept(this);
}
}
@Override
@ -117,6 +151,17 @@ public class TypeExtract extends DefaultASTVisitor {
public void visit(GenericTypeVar genericTypeVar) {
if (!this.generics.contains(genericTypeVar)) {
this.generics.add(genericTypeVar);
// Aus den bounds können die Subtypconstraints abgeleitet werden.
RefTypeOrTPHOrWildcardOrGeneric bound = genericTypeVar.getBounds().get(0);
if (!((bound instanceof RefType)
&& ((RefType) bound).getName().equals(new JavaClassName("java.lang.Object")))) {
this.subTypeConstraints.add(new SubTypeConstraint(
new GenericRefType(genericTypeVar.getName(), genericTypeVar.getOffset()), bound));
}
}
}
@Override
public void visit(GenericRefType genericRefType) {
}
}

View File

@ -39,5 +39,30 @@ public class GenericRefType extends RefType
public RefTypeOrTPHOrWildcardOrGeneric accept(ASTReturnVisitor visitor) {
return visitor.visit(this);
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
GenericRefType other = (GenericRefType) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}

View File

@ -46,10 +46,11 @@ public class RefType extends RefTypeOrTPHOrWildcardOrGeneric
@Override
public int hashCode() {
int hash = 0;
hash += super.hashCode();
hash += this.name.hashCode();//Nur den Name hashen. Sorgt ¼r langsame, aber funktionierende HashMaps
return hash;
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((parameter == null) ? 0 : parameter.hashCode());
return result;
}
public RefType(JavaClassName fullyQualifiedName, List<RefTypeOrTPHOrWildcardOrGeneric> parameter, Token offset)
@ -69,42 +70,26 @@ public class RefType extends RefTypeOrTPHOrWildcardOrGeneric
return this.parameter;
}
/**
* Author: Jrg Buerle<br/>
* @return
*/
public boolean equals(Object obj)
{
if(obj instanceof RefType){
boolean ret = true;
if(!super.equals(obj))
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if(parameter==null || parameter.size()==0){
ret &= (((RefType)obj).getParaList()==null || ((RefType)obj).getParaList().size()==0);
}
else{
if(((RefType)obj).getParaList()==null){
ret = false;
}
else if(parameter.size() != ((RefType)obj).getParaList().size())
{
ret = false;
}
else
{
for(int i = 0; i<parameter.size(); i++)
{
ret &= parameter.get(i).equals(((RefType)obj).getParaList().get(i));
}
}
}
return ret;
}
else{
if (getClass() != obj.getClass())
return false;
}
RefType other = (RefType) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (parameter == null) {
if (other.parameter != null)
return false;
} else if (!parameter.equals(other.parameter))
return false;
return true;
}

View File

@ -29,12 +29,14 @@ public class TestPaperExample {
public final PrintConstraints printConstraints = new PrintConstraints();
@org.junit.Test
public void test() throws ClassNotFoundException, IOException, ImpossibleSubTypeException, InconsistentConstraintsException {
public void test()
throws ClassNotFoundException, IOException, ImpossibleSubTypeException, InconsistentConstraintsException {
ArrayList<File> files = new ArrayList<>();
// files.add(new File(rootDirectory + "testPaperExample.jav"));
// trans(files);
// files.clear();
files.add(new File(rootDirectory + "constructed/A.java"));
files.add(new File(rootDirectory + "javFiles/testPaperExample.jav"));
trans(files);
files.clear();
// files.add(new File(rootDirectory + "constructed/A.java"));
// files.add(new File(rootDirectory + "typedtestclasses/MyInteger.java"));
files.add(new File(rootDirectory + "javFiles/testMain.jav"));
trans(files);
}
@ -49,23 +51,25 @@ public class TestPaperExample {
// Print SourceFile Infos
SyntaxTreePrinter syntaxtreeprinter = new SyntaxTreePrinter();
sourceFile.accept(syntaxtreeprinter);
// List<ClassOrInterface> typedClasses =
// compiler.sourceFiles.values().stream().flatMap(sf->sf.getClasses().stream()).collect(Collectors.toList());
System.out.println("\n--StrucTYPE--");
StrucTYPE strucTYPE = new StrucTYPE(sourceFile);
final ConstraintsSet constraints = strucTYPE.getConstraints();
final InferredTypes inferredTypesType = strucTYPE.getInferredTypes();
System.out.println("\n--StrucTYPE--");
printConstraints.print(constraints);
PrintInferredTypes.print(inferredTypesType);
System.out.println("\n--Construct--");
Construct construct = new Construct(constraints, inferredTypesType);
final List<ClassOrInterface> constructedInterfaces = construct.getConstructedInterfaces();
final Set<SubTypeConstraint> subTypeConstraints = construct.getSubTypeConstraints();
final InferredTypes inferredTypesConstruct = construct.getInferredTypes();
System.out.println("\n--Construct--");
System.out.println("\nConstructed Interfaces:");
constructedInterfaces.forEach(i -> i.accept(syntaxtreeprinter));
printConstraints.printSubTypeConstraints(subTypeConstraints);
@ -77,7 +81,8 @@ public class TestPaperExample {
FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(availableClasses);
System.out.println("\nFinite Closure:");
System.out.println(finiteClosure);
Solve solve = new Solve(subTypeConstraints, sourceFile.getClasses().get(0), finiteClosure, inferredTypesConstruct);
Solve solve = new Solve(subTypeConstraints, sourceFile.getClasses().get(0), finiteClosure,
inferredTypesConstruct);
ClassOrInterfaceWithConstraints solvedClass = solve.getSolvedClass();
System.out.println("\nSolved Class:");
solvedClass.accept(syntaxtreeprinter);

View File

@ -47,15 +47,16 @@ public class TestSolve {
SyntaxTreePrinter syntaxtreeprinter = new SyntaxTreePrinter();
sourceFile.accept(syntaxtreeprinter);
System.out.println("\n--StrucTYPE--");
StrucTYPE strucTYPE = new StrucTYPE(sourceFile);
final ConstraintsSet constraints = strucTYPE.getConstraints();
final InferredTypes inferredTypesType = strucTYPE.getInferredTypes();
System.out.println("\n--StrucTYPE--");
printConstraints.print(constraints);
PrintInferredTypes.print(inferredTypesType);
System.out.println("\n--Construct--");
Construct construct = new Construct(constraints, inferredTypesType);
final List<ClassOrInterface> constructedInterfaces = construct.getConstructedInterfaces();
@ -63,7 +64,6 @@ public class TestSolve {
// Set<UnifyPair> subTypeConstraints = construct.getSubTypeConstraintsAsUnifyPairs();
final InferredTypes inferredTypesConstruct = construct.getInferredTypes();
System.out.println("\n--Construct--");
System.out.println("\nConstructed Interfaces:");
constructedInterfaces.forEach(i -> i.accept(syntaxtreeprinter));
printConstraints.printSubTypeConstraints(subTypeConstraints);

View File

@ -1,8 +1,9 @@
package strucType.constructed;
import strucType.constructed.L;
import strucType.constructed.O;
public class A<Z extends L<AD, U>, M extends U, P, N extends V, V, AD extends O<P, V>, U> {
public class A<Z extends L<AD, U>, AD extends O<P, V>, M extends U, N extends V, U, P, V> {
public A() {
}

View File

@ -2,6 +2,7 @@ package strucType.typedtestclasses;
import strucType.constructed.L;
import strucType.constructed.O;
import java.lang.Integer;
public class MyInteger implements L<MyInteger, MyInteger>, O<MyInteger, MyInteger> {