diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index b1b2aa2f..068f1784 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -66,7 +66,7 @@ public class JavaTXCompiler { Boolean resultmodel = true; public final Map sourceFiles = new HashMap<>(); - Boolean log = false; //gibt an ob ein Log-File nach System.getProperty("user.dir")+""/logFiles/"" geschrieben werden soll? + Boolean log = true; //gibt an ob ein Log-File nach System.getProperty("user.dir")+""/logFiles/"" geschrieben werden soll? public volatile UnifyTaskModel usedTasks = new UnifyTaskModel(); public final DirectoryClassLoader classLoader; @@ -207,7 +207,7 @@ public class JavaTXCompiler { cl.getMethods().add(new Method(m.modifier, m.name, m.getReturnType().acceptTV(new TypeExchanger(gtvs)), newParaList, m.block, // new GenericDeclarationList(newGenericsList, // ((GenericDeclarationList)m.getGenerics()).getOffset()), - (GenericDeclarationList) m.getGenerics(), m.getOffset(), true)); + (GenericDeclarationList) m.getGenerics(), m.getOffset(), true, false)); } } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java b/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java index 97b18bcd..fff2386a 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java @@ -33,6 +33,7 @@ public class Method extends SyntaxTreeNode implements IItemWithOffset, TypeScope private GenericDeclarationList generics; private final RefTypeOrTPHOrWildcardOrGeneric returnType; public final Boolean isInherited; + public final Boolean isOverridden; /* * its Constraints @@ -50,10 +51,11 @@ public class Method extends SyntaxTreeNode implements IItemWithOffset, TypeScope this.block = block; this.generics = gtvDeclarations; this.isInherited = false; + this.isOverridden = false; } public Method(int modifier, String name, RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList parameterList, Block block, - GenericDeclarationList gtvDeclarations, Token offset, Boolean isInherited) { + GenericDeclarationList gtvDeclarations, Token offset, Boolean isInherited, Boolean isOverridden) { super(offset); this.name = name; this.modifier = modifier; @@ -62,6 +64,7 @@ public class Method extends SyntaxTreeNode implements IItemWithOffset, TypeScope this.block = block; this.generics = gtvDeclarations; this.isInherited = isInherited; + this.isOverridden = isOverridden; } public ParameterList getParameterList() { @@ -107,4 +110,10 @@ public class Method extends SyntaxTreeNode implements IItemWithOffset, TypeScope public int hashCode() { return Objects.hash(name, parameterlist, returnType); } + + @Override + public String toString() { + return name; + + } } diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java index 020eb6a0..76066d41 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/ASTFactory.java @@ -105,11 +105,21 @@ public class ASTFactory { allInheritedMethods.removeAll(allDeclaredMethods); for (java.lang.reflect.Method method : allDeclaredMethods) { var signature = methodSignatures.get(new Pair<>(method.getName(), org.objectweb.asm.Type.getMethodDescriptor(method))); - methoden.add(createMethod(method, signature, jreClass, false)); + if (jreClass.getSuperclass()==null) { + methoden.add(createMethod(method, signature, jreClass, false, false)); + } + else try { + jreClass.getSuperclass().getDeclaredMethod(method.getName(), method.getParameterTypes()); + methoden.add(createMethod(method, signature, jreClass, false, true)); + } + catch (java.lang.NoSuchMethodException e) + { + methoden.add(createMethod(method, signature, jreClass, false, false)); + } } for (java.lang.reflect.Method method : allInheritedMethods) { var signature = methodSignatures.get(new Pair<>(method.getName(), org.objectweb.asm.Type.getMethodDescriptor(method))); - methoden.add(createMethod(method, signature, jreClass, true)); + methoden.add(createMethod(method, signature, jreClass, true, false)); } List felder = new ArrayList<>(); for (java.lang.reflect.Field field : jreClass.getDeclaredFields()) { @@ -192,7 +202,7 @@ public class ASTFactory { return Optional.of(new de.dhbwstuttgart.syntaxtree.Constructor(modifier, name, returnType, parameterList, block, gtvDeclarations, offset /* , new ArrayList<>() geloescht PL 2018-11-24 */)); } - public static Method createMethod(java.lang.reflect.Method jreMethod, String signature, java.lang.Class inClass, Boolean isInherited) { + public static Method createMethod(java.lang.reflect.Method jreMethod, String signature, java.lang.Class inClass, Boolean isInherited, Boolean isOverridden) { String name = jreMethod.getName(); RefTypeOrTPHOrWildcardOrGeneric returnType; Type jreRetType; @@ -218,7 +228,7 @@ public class ASTFactory { GenericDeclarationList gtvDeclarations = createGenerics(jreMethod.getTypeParameters(), inClass, jreMethod.getName(), signature); Token offset = new NullToken(); - return new Method(jreMethod.getModifiers(), name, returnType, parameterList, block, gtvDeclarations, offset, isInherited); + return new Method(jreMethod.getModifiers(), name, returnType, parameterList, block, gtvDeclarations, offset, isInherited, isOverridden); } public static GenericDeclarationList createGenerics(TypeVariable[] typeParameters, Class context, String methodName, String signature) { diff --git a/src/main/java/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java b/src/main/java/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java index e4a92c02..b03ff1dd 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/assumptions/MethodAssumption.java @@ -20,14 +20,16 @@ public class MethodAssumption extends Assumption{ private RefTypeOrTPHOrWildcardOrGeneric retType; List params; private final Boolean isInherited; + private final Boolean isOverridden; public MethodAssumption(ClassOrInterface receiver, RefTypeOrTPHOrWildcardOrGeneric retType, - List params, TypeScope scope, Boolean isInherited){ + List params, TypeScope scope, Boolean isInherited, Boolean isOverridden){ super(scope); this.receiver = receiver; this.retType = retType; this.params = params; this.isInherited = isInherited; + this.isOverridden = isOverridden; } /* @@ -74,4 +76,8 @@ public class MethodAssumption extends Assumption{ public Boolean isInherited() { return isInherited; } + + public Boolean isOverridden() { + return isOverridden; + } } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/constraints/Constraint.java b/src/main/java/de/dhbwstuttgart/typeinference/constraints/Constraint.java index 07a2e9d2..0ac2a84e 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/constraints/Constraint.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/constraints/Constraint.java @@ -8,7 +8,8 @@ import java.util.Set; public class Constraint extends HashSet { private static final long serialVersionUID = 1L; - private Boolean isInherited = false;//wird nur für die Method-Constraints benoetigt + private Boolean isInherited = false;//wird beides nur für die Method-Constraints benoetigt + private Boolean isOverridden = false; /* * wird verwendet um bei der Codegenerierung die richtige Methoden - Signatur @@ -22,12 +23,14 @@ public class Constraint extends HashSet { super(); } - public Constraint(Boolean isInherited) { + public Constraint(Boolean isInherited, Boolean isOverridden) { this.isInherited = isInherited; + this.isOverridden = isOverridden; } - public Constraint(Boolean isInherited, Constraint extendConstraint, Set methodSignatureConstraint) { + public Constraint(Boolean isInherited, Boolean isOverridden, Constraint extendConstraint, Set methodSignatureConstraint) { this.isInherited = isInherited; + this.isOverridden = isOverridden; this.extendConstraint = extendConstraint; this.methodSignatureConstraint = methodSignatureConstraint; } @@ -40,6 +43,10 @@ public class Constraint extends HashSet { return isInherited; } + public Boolean isOverridden() { + return isInherited; + } + public Constraint getExtendConstraint() { return extendConstraint; } @@ -57,7 +64,7 @@ public class Constraint extends HashSet { } public String toString() { - return super.toString() + "\nisInherited = " + isInherited + return super.toString() + "\nisInherited = " + isInherited + " isOveridden = " + isOverridden + methodSignatureConstraint //" + extendsContraint: " + (extendConstraint != null ? extendConstraint.toStringBase() : "null" ) + "\n" ; diff --git a/src/main/java/de/dhbwstuttgart/typeinference/constraints/ConstraintSet.java b/src/main/java/de/dhbwstuttgart/typeinference/constraints/ConstraintSet.java index f52001b3..f438c664 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/constraints/ConstraintSet.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/constraints/ConstraintSet.java @@ -77,6 +77,7 @@ public class ConstraintSet { .map(o) .collect(Collectors.toCollection(( () -> new Constraint (as.isInherited(), + as.isOverridden(), (as.getExtendConstraint() != null) ? as.getExtendConstraint().stream().map(o).collect(Collectors.toCollection(Constraint::new)) : null, diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index cabe5f87..722e5e2f 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -591,7 +591,7 @@ public class TYPEStmt implements StatementVisitor { for (var ctor : clazz.getConstructors()) { var params = convertParams(ctor.getParameterList(), info); if (params.size() != superCall.arglist.getArguments().size()) continue; - var assumption = new MethodAssumption(null, new Void(new NullToken()), params, createTypeScope(clazz, ctor), ctor.isInherited); + var assumption = new MethodAssumption(null, new Void(new NullToken()), params, createTypeScope(clazz, ctor), ctor.isInherited, false); GenericsResolver resolver = getResolverInstance(); Set> oneMethodConstraints = generateConstraint(superCall, assumption, info, resolver); @@ -607,7 +607,7 @@ public class TYPEStmt implements StatementVisitor { for (var ctor : info.getCurrentClass().getConstructors()) { var params = convertParams(ctor.getParameterList(), info); if (params.size() != thisCall.arglist.getArguments().size()) continue; - var assumption = new MethodAssumption(null, new Void(new NullToken()), params, createTypeScope(info.getCurrentClass(), ctor), ctor.isInherited); + var assumption = new MethodAssumption(null, new Void(new NullToken()), params, createTypeScope(info.getCurrentClass(), ctor), ctor.isInherited, false); GenericsResolver resolver = getResolverInstance(); Set> oneMethodConstraints = generateConstraint(thisCall, assumption, info, resolver); @@ -640,8 +640,8 @@ public class TYPEStmt implements StatementVisitor { protected Set> generateConstraint(MethodCall forMethod, MethodAssumption assumption, TypeInferenceBlockInformation info, GenericsResolver resolver) { Constraint methodConstraint, extendsMethodConstraint; - methodConstraint = new Constraint<>(assumption.isInherited()); - extendsMethodConstraint = new Constraint<>(assumption.isInherited());// PL 2023-01-24: Ersetzt die Dopplung in visit(MethodCall) + methodConstraint = new Constraint<>(assumption.isInherited(), assumption.isOverridden()); + extendsMethodConstraint = new Constraint<>(assumption.isInherited(), assumption.isOverridden());// PL 2023-01-24: Ersetzt die Dopplung in visit(MethodCall) ClassOrInterface receiverCl = assumption.getReceiver(); /* @@ -746,14 +746,14 @@ public class TYPEStmt implements StatementVisitor { public RefTypeOrTPHOrWildcardOrGeneric getReturnType() { throw new NotImplementedException(); } - }, false)); + }, false, false)); } for (ClassOrInterface cl : info.getAvailableClasses()) { for (Method m : cl.getMethods()) { if (m.getName().equals(name) && m.getParameterList().getFormalparalist().size() == numArgs) { RefTypeOrTPHOrWildcardOrGeneric retType = m.getReturnType();// info.checkGTV(m.getReturnType()); - ret.add(new MethodAssumption(cl, retType, convertParams(m.getParameterList(), info), createTypeScope(cl, m), m.isInherited)); + ret.add(new MethodAssumption(cl, retType, convertParams(m.getParameterList(), info), createTypeScope(cl, m), m.isInherited, m.isOverridden)); } } } @@ -785,7 +785,7 @@ public class TYPEStmt implements StatementVisitor { for (Method m : cl.getConstructors()) { if (m.getParameterList().getFormalparalist().size() == argList.getArguments().size()) { var params = convertParams(m.getParameterList(), info); - ret.add(new MethodAssumption(cl, cl.generateTypeOfThisClass(), params, createTypeScope(cl, m), m.isInherited)); + ret.add(new MethodAssumption(cl, cl.generateTypeOfThisClass(), params, createTypeScope(cl, m), m.isInherited, m.isOverridden)); } } } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java index a3d2474c..2d2fa758 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/RuleSet.java @@ -702,9 +702,10 @@ public class RuleSet implements IRuleSet{ x -> uni.apply(pair,x)).collect(Collectors.toCollection((b.getExtendConstraint() != null) ? () -> new Constraint( b.isInherited(), + b.isOverridden(), b.getExtendConstraint().stream().map(x -> uni.apply(pair,x)).collect(Collectors.toCollection(Constraint::new)), b.getmethodSignatureConstraint().stream().map(x -> uni.apply(pair,x)).collect(Collectors.toCollection(HashSet::new))) - : () -> new Constraint(b.isInherited()) + : () -> new Constraint(b.isInherited(), b.isOverridden()) )); oderConstraints.replaceAll(oc -> oc.stream().map(applyUni).collect(Collectors.toCollection(HashSet::new))); /* diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java index 90a1b31b..8371477b 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/TypeUnifyTask.java @@ -816,6 +816,7 @@ public class TypeUnifyTask extends RecursiveTask>> { writeLog("nextSetasList: " + nextSetasList.toString()); if (variance == 1) { a = oup.max(nextSetasList.iterator()); + writeLog("Max: a in " + variance + " "+ a); nextSetasList.remove(a); if (oderConstraint) { nextSetasListOderConstraints.add(((Constraint)a).getExtendConstraint()); @@ -1353,12 +1354,15 @@ public class TypeUnifyTask extends RecursiveTask>> { while(aParDefIt.hasNext()) { Set a_new = aParDefIt.next(); List> smallerSetasList = oup.smallerThan(a_new, nextSetasList); + writeLog("smallerSetasList: " + smallerSetasList); List> notInherited = smallerSetasList.stream() - .filter(x -> !((Constraint)x).isInherited()) + .filter(x -> !((Constraint)x).isInherited() && !((Constraint)x).isOverridden()) .collect(Collectors.toCollection(ArrayList::new)); + writeLog("notInherited: " + notInherited+"\n"); List> notErased = new ArrayList<>(); notInherited.stream().forEach(x -> { notErased.addAll(oup.smallerEqThan(x, smallerSetasList)); }); List> erased = new ArrayList<>(smallerSetasList); + writeLog("notErased: " + notErased+"\n"); erased.removeAll(notErased); nextSetasList.removeAll(erased); diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java index 96bf1bb3..d6514c67 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -699,7 +699,7 @@ implements IFiniteClosure { */ public int compare (UnifyType left, UnifyType right, PairOperator pairop) { - //try {logFile.write("left: "+ left + " right: " + right + " pairop: " + pairop);} catch (IOException ie) {} + try {logFile.write("left: "+ left + " right: " + right + " pairop: " + pairop +"\n");} catch (IOException ie) {} if (left.getName().equals("Matrix") || right.getName().equals("Matrix")) System.out.println(""); /* @@ -756,15 +756,15 @@ implements IFiniteClosure { HashSet hs = new HashSet<>(); hs.add(up); Set smallerRes = unifyTask.applyTypeUnificationRules(hs, this); - /* - //if (left.getName().equals("Matrix") || right.getName().equals("Matrix")) + + //if (left.getName().equals("Vector") || right.getName().equals("AbstractList")) {try { logFile.write("\nsmallerRes: " + smallerRes);//"smallerHash: " + greaterHash.toString()); logFile.flush(); } catch (IOException e) { System.err.println("no LogFile");}} - */ + //Gleichungen der Form a <./=. Theta oder Theta <./=. a oder a <./=. b sind ok. Predicate delFun = x -> !((x.getLhsType() instanceof PlaceholderType || x.getRhsType() instanceof PlaceholderType) @@ -772,6 +772,12 @@ implements IFiniteClosure { ((WildcardType)x.getLhsType()).getWildcardedType().equals(x.getRhsType())) ); long smallerLen = smallerRes.stream().filter(delFun).count(); + try { + logFile.write("\nsmallerLen: " + smallerLen +"\n"); + logFile.flush(); + } + catch (IOException e) { + System.err.println("no LogFile");} if (smallerLen == 0) return -1; else { up = new UnifyPair(right, left, pairop); @@ -779,15 +785,15 @@ implements IFiniteClosure { hs = new HashSet<>(); hs.add(up); Set greaterRes = unifyTask.applyTypeUnificationRules(hs, this); - /* - //if (left.getName().equals("Matrix") || right.getName().equals("Matrix")) + + //if (left.getName().equals("Vector") || right.getName().equals("AbstractList")) {try { logFile.write("\ngreaterRes: " + greaterRes);//"smallerHash: " + greaterHash.toString()); logFile.flush(); } catch (IOException e) { System.err.println("no LogFile");}} - */ + //Gleichungen der Form a <./=. Theta oder Theta <./=. a oder a <./=. b sind ok. long greaterLen = greaterRes.stream().filter(delFun).count(); if (greaterLen == 0) return 1;