forked from JavaTX/JavaCompilerCore
TYPEExpr fixed for newClass.
This commit is contained in:
parent
e972282620
commit
f44f08b895
@ -86,6 +86,11 @@ public class Construct extends DefaultASTVisitor {
|
|||||||
String.format("%s is not a valid type to generate an interface for.", refType));
|
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) {
|
private ClassOrInterface constructInterface(TypePlaceholder i, JavaClassName name) {
|
||||||
List<Field> fielddecl = new ArrayList<>();
|
List<Field> fielddecl = new ArrayList<>();
|
||||||
List<Method> methods = new ArrayList<>();
|
List<Method> methods = new ArrayList<>();
|
||||||
|
@ -9,15 +9,15 @@ import java.util.Set;
|
|||||||
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
|
||||||
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
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 static final InferredTypes EMPTY = new InferredTypes();
|
||||||
|
|
||||||
public void resolveTransitiveTypes() {
|
public void resolveTransitiveTypes() {
|
||||||
Set<TypePlaceholder> keySet = this.inferredTypes.keySet();
|
Set<RefTypeOrTPHOrWildcardOrGeneric> keySet = this.inferredTypes.keySet();
|
||||||
for (TypePlaceholder key : keySet) {
|
for (RefTypeOrTPHOrWildcardOrGeneric key : keySet) {
|
||||||
RefTypeOrTPHOrWildcardOrGeneric value = this.inferredTypes.get(key);
|
RefTypeOrTPHOrWildcardOrGeneric value = this.inferredTypes.get(key);
|
||||||
if (value instanceof TypePlaceholder && keySet.contains(value)) {
|
if (value instanceof TypePlaceholder && keySet.contains(value)) {
|
||||||
this.inferredTypes.put(key, this.inferredTypes.get(value));
|
this.inferredTypes.put(key, this.inferredTypes.get(value));
|
||||||
@ -51,7 +51,7 @@ public class InferredTypes implements Map<TypePlaceholder, RefTypeOrTPHOrWildcar
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RefTypeOrTPHOrWildcardOrGeneric put(TypePlaceholder key, RefTypeOrTPHOrWildcardOrGeneric value) {
|
public RefTypeOrTPHOrWildcardOrGeneric put(RefTypeOrTPHOrWildcardOrGeneric key, RefTypeOrTPHOrWildcardOrGeneric value) {
|
||||||
return inferredTypes.put(key, value);
|
return inferredTypes.put(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,7 +61,7 @@ public class InferredTypes implements Map<TypePlaceholder, RefTypeOrTPHOrWildcar
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void putAll(Map<? extends TypePlaceholder, ? extends RefTypeOrTPHOrWildcardOrGeneric> m) {
|
public void putAll(Map<? extends RefTypeOrTPHOrWildcardOrGeneric, ? extends RefTypeOrTPHOrWildcardOrGeneric> m) {
|
||||||
inferredTypes.putAll(m);
|
inferredTypes.putAll(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ public class InferredTypes implements Map<TypePlaceholder, RefTypeOrTPHOrWildcar
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<TypePlaceholder> keySet() {
|
public Set<RefTypeOrTPHOrWildcardOrGeneric> keySet() {
|
||||||
return inferredTypes.keySet();
|
return inferredTypes.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,7 +81,7 @@ public class InferredTypes implements Map<TypePlaceholder, RefTypeOrTPHOrWildcar
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<java.util.Map.Entry<TypePlaceholder, RefTypeOrTPHOrWildcardOrGeneric>> entrySet() {
|
public Set<java.util.Map.Entry<RefTypeOrTPHOrWildcardOrGeneric, RefTypeOrTPHOrWildcardOrGeneric>> entrySet() {
|
||||||
return inferredTypes.entrySet();
|
return inferredTypes.entrySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,10 +92,10 @@ public class InferredTypes implements Map<TypePlaceholder, RefTypeOrTPHOrWildcar
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String s = "[";
|
String s = "[";
|
||||||
Iterator<TypePlaceholder> iterator = this.inferredTypes.keySet().iterator();
|
Iterator<RefTypeOrTPHOrWildcardOrGeneric> iterator = this.inferredTypes.keySet().iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
TypePlaceholder tph = iterator.next();
|
RefTypeOrTPHOrWildcardOrGeneric type = iterator.next();
|
||||||
s += tph + " -> " + inferredTypes.get(tph);
|
s += type + " -> " + inferredTypes.get(type);
|
||||||
if (iterator.hasNext()) {
|
if (iterator.hasNext()) {
|
||||||
s += ", ";
|
s += ", ";
|
||||||
}
|
}
|
||||||
|
@ -56,8 +56,9 @@ public class StrucTYPE extends DefaultASTVisitor {
|
|||||||
this.inferredTypes.putAll(typeExpr.getInferredTypes());
|
this.inferredTypes.putAll(typeExpr.getInferredTypes());
|
||||||
this.inferredTypes.resolveTransitiveTypes();
|
this.inferredTypes.resolveTransitiveTypes();
|
||||||
|
|
||||||
this.constraintsSet.addConstraintsSet(typeExpr.getConstraints());
|
ConstraintsSet constraints = typeExpr.getConstraints();
|
||||||
this.constraintsSet.inferTypes(this.inferredTypes);
|
constraints.inferTypes(this.inferredTypes);
|
||||||
|
this.constraintsSet.addConstraintsSet(constraints);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package de.dhbwstuttgart.strucTypes;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet;
|
import de.dhbwstuttgart.strucTypes.constraint.ConstraintsSet;
|
||||||
import de.dhbwstuttgart.strucTypes.constraint.FieldConstraint;
|
import de.dhbwstuttgart.strucTypes.constraint.FieldConstraint;
|
||||||
@ -14,6 +15,7 @@ import de.dhbwstuttgart.syntaxtree.Field;
|
|||||||
import de.dhbwstuttgart.syntaxtree.FormalParameter;
|
import de.dhbwstuttgart.syntaxtree.FormalParameter;
|
||||||
import de.dhbwstuttgart.syntaxtree.Method;
|
import de.dhbwstuttgart.syntaxtree.Method;
|
||||||
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
|
import de.dhbwstuttgart.syntaxtree.statement.ArgumentList;
|
||||||
|
import de.dhbwstuttgart.syntaxtree.statement.BinaryExpr;
|
||||||
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.Expression;
|
import de.dhbwstuttgart.syntaxtree.statement.Expression;
|
||||||
@ -118,21 +120,25 @@ public class TYPEExpr extends DefaultASTVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
List<Expression> arguments = methodCall.getArgumentList().getArguments();
|
List<Expression> arguments = methodCall.getArgumentList().getArguments();
|
||||||
if (!methodTypeVisitor.isTypeVariable()
|
Optional<Method> method = methodTypeVisitor.getMethod(methodCall.name, arguments.size());
|
||||||
&& methodTypeVisitor.getMethod(methodCall.name, arguments.size()).isPresent()) {
|
if (!methodTypeVisitor.isTypeVariable() && method.isPresent()) {
|
||||||
methodTypeVisitor.getMethod(methodCall.name, arguments.size()).ifPresent(m -> {
|
Method m = method.get();
|
||||||
for (int i = 0; i < arguments.size(); i++) {
|
for (int i = 0; i < arguments.size(); i++) {
|
||||||
this.constraints.addConstraint(new SubTypeConstraint(arguments.get(i).getType(),
|
this.constraints.addConstraint(new SubTypeConstraint(arguments.get(i).getType(),
|
||||||
m.getParameterList().getParameterAt(i).getType()));
|
m.getParameterList().getParameterAt(i).getType()));
|
||||||
}
|
}
|
||||||
this.inferredTypes.put((TypePlaceholder) methodCall.getType(), m.getReturnType());
|
this.inferredTypes.put((TypePlaceholder) methodCall.getType(), m.getReturnType());
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
MethodConstraint methodConstraint = new MethodConstraint(methodCall.receiver.getType(), methodCall.name,
|
MethodConstraint methodConstraint = new MethodConstraint(methodCall.receiver.getType(), methodCall.name,
|
||||||
new ArrayList<SubTypeConstraint>(), methodCall.getType());
|
new ArrayList<SubTypeConstraint>(), methodCall.getType());
|
||||||
arguments.forEach(e -> methodConstraint.addArguments(e.getType()));
|
arguments.forEach(e -> methodConstraint.addArguments(e.getType()));
|
||||||
this.constraints.addConstraint(methodConstraint);
|
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
|
@Override
|
||||||
@ -160,6 +166,10 @@ public class TYPEExpr extends DefaultASTVisitor {
|
|||||||
} else {
|
} else {
|
||||||
type.accept(typeExtract);
|
type.accept(typeExtract);
|
||||||
// implement generics nicht nötig
|
// implement generics nicht nötig
|
||||||
|
// ClassOrInterface orDefault =
|
||||||
|
// typedClasses.getOrDefault(type.getName(),
|
||||||
|
// ClassOrInterfaceFactory.createClass(type).get());
|
||||||
|
// orDefault.accept(typeExtract);
|
||||||
}
|
}
|
||||||
this.createNewClassSubTypeConstraints(newClass, typeExtract);
|
this.createNewClassSubTypeConstraints(newClass, typeExtract);
|
||||||
}
|
}
|
||||||
@ -176,11 +186,14 @@ public class TYPEExpr extends DefaultASTVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// List of Generics of newClass
|
// List of Generics of newClass
|
||||||
List<RefTypeOrTPHOrWildcardOrGeneric> paraList = ((RefType)newClass.getType()).getParaList();
|
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++) {
|
for (int i = 0; i < argumentsSize; i++) {
|
||||||
// Bei Fields wird kein GenericRefType sondern Object als Typ geparst.
|
// Bei Fields wird kein GenericRefType sondern Object als Typ
|
||||||
// SubTypeConstraint subTypeConstraint = new SubTypeConstraint(arguments.get(i).getType(),
|
// geparst.
|
||||||
|
// SubTypeConstraint subTypeConstraint = new
|
||||||
|
// SubTypeConstraint(arguments.get(i).getType(),
|
||||||
// fields.get(i).getType());
|
// fields.get(i).getType());
|
||||||
|
|
||||||
// Es darf nur einen Konstruktor mit allen Feldern geben.
|
// 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);
|
SubTypeConstraint subTypeConstraint = new SubTypeConstraint(arguments.get(i).getType(), supertype);
|
||||||
this.constraints.addConstraint(subTypeConstraint);
|
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
|
@Override
|
||||||
@ -223,7 +241,7 @@ public class TYPEExpr extends DefaultASTVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(GenericRefType genericRefType){
|
public void visit(GenericRefType genericRefType) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -231,4 +249,8 @@ public class TYPEExpr extends DefaultASTVisitor {
|
|||||||
public void visit(Literal literal) {
|
public void visit(Literal literal) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(BinaryExpr binary) {
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -76,6 +76,39 @@ public class SubTypeConstraint {
|
|||||||
return null;
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("%s <* %s", this.subtype, this.supertype);
|
return String.format("%s <* %s", this.subtype, this.supertype);
|
||||||
|
@ -312,7 +312,10 @@ public class SyntaxTreePrinter implements ASTVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(BinaryExpr binary) {
|
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
|
@Override
|
||||||
|
@ -206,8 +206,9 @@ public class InferTypes implements ASTReturnVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public GenericRefType visit(GenericRefType genericRefType) {
|
public RefTypeOrTPHOrWildcardOrGeneric visit(GenericRefType genericRefType) {
|
||||||
return genericRefType;
|
RefTypeOrTPHOrWildcardOrGeneric inferredType = inferredTypes.infer(genericRefType);
|
||||||
|
return inferredType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -229,8 +230,7 @@ public class InferTypes implements ASTReturnVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BinaryExpr visit(BinaryExpr binary) {
|
public BinaryExpr visit(BinaryExpr binary) {
|
||||||
throw new NotImplementedException();
|
return binary;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,18 +1,23 @@
|
|||||||
package de.dhbwstuttgart.strucTypes.visitor;
|
package de.dhbwstuttgart.strucTypes.visitor;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
import de.dhbwstuttgart.parser.scope.JavaClassName;
|
||||||
|
import de.dhbwstuttgart.strucTypes.InferredTypes;
|
||||||
import de.dhbwstuttgart.strucTypes.classorinterface.ClassOrInterfaceFactory;
|
import de.dhbwstuttgart.strucTypes.classorinterface.ClassOrInterfaceFactory;
|
||||||
|
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.Constructor;
|
||||||
import de.dhbwstuttgart.syntaxtree.Field;
|
import de.dhbwstuttgart.syntaxtree.Field;
|
||||||
import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
|
import de.dhbwstuttgart.syntaxtree.GenericDeclarationList;
|
||||||
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
|
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
|
||||||
import de.dhbwstuttgart.syntaxtree.Method;
|
import de.dhbwstuttgart.syntaxtree.Method;
|
||||||
|
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.TypePlaceholder;
|
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -28,6 +33,8 @@ public class TypeExtract extends DefaultASTVisitor {
|
|||||||
private List<Method> methods = new ArrayList<>();
|
private List<Method> methods = new ArrayList<>();
|
||||||
private List<Constructor> constructors = new ArrayList<>();
|
private List<Constructor> constructors = new ArrayList<>();
|
||||||
private List<GenericTypeVar> generics = 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 typeVariable = false;
|
||||||
private boolean initialClass = true;
|
private boolean initialClass = true;
|
||||||
|
|
||||||
@ -47,6 +54,14 @@ public class TypeExtract extends DefaultASTVisitor {
|
|||||||
return generics;
|
return generics;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public InferredTypes getGrtToTphMap() {
|
||||||
|
return grtToTphMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<SubTypeConstraint> getSubTypeConstraints() {
|
||||||
|
return subTypeConstraints;
|
||||||
|
}
|
||||||
|
|
||||||
public Optional<Field> getField(String fieldName) {
|
public Optional<Field> getField(String fieldName) {
|
||||||
return this.fields.stream().filter(f -> f.getName().equals(fieldName))
|
return this.fields.stream().filter(f -> f.getName().equals(fieldName))
|
||||||
// keine statische Polymorphie zugelassen
|
// keine statische Polymorphie zugelassen
|
||||||
@ -85,7 +100,26 @@ public class TypeExtract extends DefaultASTVisitor {
|
|||||||
@Override
|
@Override
|
||||||
public void visit(RefType refType) {
|
public void visit(RefType refType) {
|
||||||
this.typeVariable = false;
|
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
|
@Override
|
||||||
@ -117,6 +151,17 @@ public class TypeExtract extends DefaultASTVisitor {
|
|||||||
public void visit(GenericTypeVar genericTypeVar) {
|
public void visit(GenericTypeVar genericTypeVar) {
|
||||||
if (!this.generics.contains(genericTypeVar)) {
|
if (!this.generics.contains(genericTypeVar)) {
|
||||||
this.generics.add(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) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,5 +39,30 @@ public class GenericRefType extends RefType
|
|||||||
public RefTypeOrTPHOrWildcardOrGeneric accept(ASTReturnVisitor visitor) {
|
public RefTypeOrTPHOrWildcardOrGeneric accept(ASTReturnVisitor visitor) {
|
||||||
return visitor.visit(this);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,10 +46,11 @@ public class RefType extends RefTypeOrTPHOrWildcardOrGeneric
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
int hash = 0;
|
final int prime = 31;
|
||||||
hash += super.hashCode();
|
int result = 1;
|
||||||
hash += this.name.hashCode();//Nur den Name hashen. Sorgt für langsame, aber funktionierende HashMaps
|
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||||
return hash;
|
result = prime * result + ((parameter == null) ? 0 : parameter.hashCode());
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public RefType(JavaClassName fullyQualifiedName, List<RefTypeOrTPHOrWildcardOrGeneric> parameter, Token offset)
|
public RefType(JavaClassName fullyQualifiedName, List<RefTypeOrTPHOrWildcardOrGeneric> parameter, Token offset)
|
||||||
@ -69,42 +70,26 @@ public class RefType extends RefTypeOrTPHOrWildcardOrGeneric
|
|||||||
return this.parameter;
|
return this.parameter;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Author: Jrg Buerle<br/>
|
public boolean equals(Object obj) {
|
||||||
* @return
|
if (this == obj)
|
||||||
*/
|
return true;
|
||||||
public boolean equals(Object obj)
|
if (obj == null)
|
||||||
{
|
|
||||||
if(obj instanceof RefType){
|
|
||||||
boolean ret = true;
|
|
||||||
|
|
||||||
if(!super.equals(obj))
|
|
||||||
return false;
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
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{
|
|
||||||
return false;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,12 +29,14 @@ public class TestPaperExample {
|
|||||||
public final PrintConstraints printConstraints = new PrintConstraints();
|
public final PrintConstraints printConstraints = new PrintConstraints();
|
||||||
|
|
||||||
@org.junit.Test
|
@org.junit.Test
|
||||||
public void test() throws ClassNotFoundException, IOException, ImpossibleSubTypeException, InconsistentConstraintsException {
|
public void test()
|
||||||
|
throws ClassNotFoundException, IOException, ImpossibleSubTypeException, InconsistentConstraintsException {
|
||||||
ArrayList<File> files = new ArrayList<>();
|
ArrayList<File> files = new ArrayList<>();
|
||||||
// files.add(new File(rootDirectory + "testPaperExample.jav"));
|
files.add(new File(rootDirectory + "javFiles/testPaperExample.jav"));
|
||||||
// trans(files);
|
trans(files);
|
||||||
// files.clear();
|
files.clear();
|
||||||
files.add(new File(rootDirectory + "constructed/A.java"));
|
// files.add(new File(rootDirectory + "constructed/A.java"));
|
||||||
|
// files.add(new File(rootDirectory + "typedtestclasses/MyInteger.java"));
|
||||||
files.add(new File(rootDirectory + "javFiles/testMain.jav"));
|
files.add(new File(rootDirectory + "javFiles/testMain.jav"));
|
||||||
trans(files);
|
trans(files);
|
||||||
}
|
}
|
||||||
@ -49,23 +51,25 @@ public class TestPaperExample {
|
|||||||
// Print SourceFile Infos
|
// Print SourceFile Infos
|
||||||
SyntaxTreePrinter syntaxtreeprinter = new SyntaxTreePrinter();
|
SyntaxTreePrinter syntaxtreeprinter = new SyntaxTreePrinter();
|
||||||
sourceFile.accept(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);
|
StrucTYPE strucTYPE = new StrucTYPE(sourceFile);
|
||||||
|
|
||||||
final ConstraintsSet constraints = strucTYPE.getConstraints();
|
final ConstraintsSet constraints = strucTYPE.getConstraints();
|
||||||
final InferredTypes inferredTypesType = strucTYPE.getInferredTypes();
|
final InferredTypes inferredTypesType = strucTYPE.getInferredTypes();
|
||||||
|
|
||||||
System.out.println("\n--StrucTYPE--");
|
|
||||||
printConstraints.print(constraints);
|
printConstraints.print(constraints);
|
||||||
PrintInferredTypes.print(inferredTypesType);
|
PrintInferredTypes.print(inferredTypesType);
|
||||||
|
|
||||||
|
System.out.println("\n--Construct--");
|
||||||
Construct construct = new Construct(constraints, inferredTypesType);
|
Construct construct = new Construct(constraints, inferredTypesType);
|
||||||
|
|
||||||
final List<ClassOrInterface> constructedInterfaces = construct.getConstructedInterfaces();
|
final List<ClassOrInterface> constructedInterfaces = construct.getConstructedInterfaces();
|
||||||
final Set<SubTypeConstraint> subTypeConstraints = construct.getSubTypeConstraints();
|
final Set<SubTypeConstraint> subTypeConstraints = construct.getSubTypeConstraints();
|
||||||
final InferredTypes inferredTypesConstruct = construct.getInferredTypes();
|
final InferredTypes inferredTypesConstruct = construct.getInferredTypes();
|
||||||
|
|
||||||
System.out.println("\n--Construct--");
|
|
||||||
System.out.println("\nConstructed Interfaces:");
|
System.out.println("\nConstructed Interfaces:");
|
||||||
constructedInterfaces.forEach(i -> i.accept(syntaxtreeprinter));
|
constructedInterfaces.forEach(i -> i.accept(syntaxtreeprinter));
|
||||||
printConstraints.printSubTypeConstraints(subTypeConstraints);
|
printConstraints.printSubTypeConstraints(subTypeConstraints);
|
||||||
@ -77,7 +81,8 @@ public class TestPaperExample {
|
|||||||
FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(availableClasses);
|
FiniteClosure finiteClosure = UnifyTypeFactory.generateFC(availableClasses);
|
||||||
System.out.println("\nFinite Closure:");
|
System.out.println("\nFinite Closure:");
|
||||||
System.out.println(finiteClosure);
|
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();
|
ClassOrInterfaceWithConstraints solvedClass = solve.getSolvedClass();
|
||||||
System.out.println("\nSolved Class:");
|
System.out.println("\nSolved Class:");
|
||||||
solvedClass.accept(syntaxtreeprinter);
|
solvedClass.accept(syntaxtreeprinter);
|
||||||
|
@ -47,15 +47,16 @@ public class TestSolve {
|
|||||||
SyntaxTreePrinter syntaxtreeprinter = new SyntaxTreePrinter();
|
SyntaxTreePrinter syntaxtreeprinter = new SyntaxTreePrinter();
|
||||||
sourceFile.accept(syntaxtreeprinter);
|
sourceFile.accept(syntaxtreeprinter);
|
||||||
|
|
||||||
|
System.out.println("\n--StrucTYPE--");
|
||||||
StrucTYPE strucTYPE = new StrucTYPE(sourceFile);
|
StrucTYPE strucTYPE = new StrucTYPE(sourceFile);
|
||||||
|
|
||||||
final ConstraintsSet constraints = strucTYPE.getConstraints();
|
final ConstraintsSet constraints = strucTYPE.getConstraints();
|
||||||
final InferredTypes inferredTypesType = strucTYPE.getInferredTypes();
|
final InferredTypes inferredTypesType = strucTYPE.getInferredTypes();
|
||||||
|
|
||||||
System.out.println("\n--StrucTYPE--");
|
|
||||||
printConstraints.print(constraints);
|
printConstraints.print(constraints);
|
||||||
PrintInferredTypes.print(inferredTypesType);
|
PrintInferredTypes.print(inferredTypesType);
|
||||||
|
|
||||||
|
System.out.println("\n--Construct--");
|
||||||
Construct construct = new Construct(constraints, inferredTypesType);
|
Construct construct = new Construct(constraints, inferredTypesType);
|
||||||
|
|
||||||
final List<ClassOrInterface> constructedInterfaces = construct.getConstructedInterfaces();
|
final List<ClassOrInterface> constructedInterfaces = construct.getConstructedInterfaces();
|
||||||
@ -63,7 +64,6 @@ public class TestSolve {
|
|||||||
// Set<UnifyPair> subTypeConstraints = construct.getSubTypeConstraintsAsUnifyPairs();
|
// Set<UnifyPair> subTypeConstraints = construct.getSubTypeConstraintsAsUnifyPairs();
|
||||||
final InferredTypes inferredTypesConstruct = construct.getInferredTypes();
|
final InferredTypes inferredTypesConstruct = construct.getInferredTypes();
|
||||||
|
|
||||||
System.out.println("\n--Construct--");
|
|
||||||
System.out.println("\nConstructed Interfaces:");
|
System.out.println("\nConstructed Interfaces:");
|
||||||
constructedInterfaces.forEach(i -> i.accept(syntaxtreeprinter));
|
constructedInterfaces.forEach(i -> i.accept(syntaxtreeprinter));
|
||||||
printConstraints.printSubTypeConstraints(subTypeConstraints);
|
printConstraints.printSubTypeConstraints(subTypeConstraints);
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
package strucType.constructed;
|
package strucType.constructed;
|
||||||
|
|
||||||
import strucType.constructed.L;
|
import strucType.constructed.L;
|
||||||
import strucType.constructed.O;
|
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() {
|
public A() {
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package strucType.typedtestclasses;
|
|||||||
|
|
||||||
import strucType.constructed.L;
|
import strucType.constructed.L;
|
||||||
import strucType.constructed.O;
|
import strucType.constructed.O;
|
||||||
|
import java.lang.Integer;
|
||||||
|
|
||||||
public class MyInteger implements L<MyInteger, MyInteger>, O<MyInteger, MyInteger> {
|
public class MyInteger implements L<MyInteger, MyInteger>, O<MyInteger, MyInteger> {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user