From be9ee49878a0c713bedb3dab54d8f7b85029d762 Mon Sep 17 00:00:00 2001 From: "pl@gohorb.ba-horb.de" Date: Tue, 13 Apr 2021 10:03:48 +0200 Subject: [PATCH] modified: src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java modified: src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java modified: src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java modified: src/main/java/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java modified: src/main/java/de/dhbwstuttgart/syntaxtree/statement/Statement.java modified: src/main/java/de/dhbwstuttgart/typeinference/constraints/Constraint.java modified: src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java modified: src/test/java/insertGenerics/TestLocalVarLambda.java --- .../dhbwstuttgart/bytecode/TPHExtractor.java | 6 ++-- .../FamilyOfGeneratedGenerics.java | 20 +++++++++---- .../StatementGenerator.java | 30 +++++++++++++------ .../syntaxtree/statement/MethodCall.java | 5 +--- .../syntaxtree/statement/Statement.java | 11 ++++++- .../typeinference/constraints/Constraint.java | 8 ++++- .../typeinference/typeAlgo/TYPEStmt.java | 8 +++-- .../insertGenerics/TestLocalVarLambda.java | 10 ++++++- 8 files changed, 72 insertions(+), 26 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java b/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java index e4f6315e..6f443a47 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java @@ -209,10 +209,12 @@ public class TPHExtractor extends AbstractASTWalker { csS.getOderConstraints() .forEach(oConSSet -> { Set> setCons = new HashSet<>(); - oConSSet.forEach(OConS -> { setCons.add( + oConSSet.forEach(OConS -> { Constraint newConsPair = new Constraint(); + newConsPair.isStatement = OConS.isStatement; + setCons.add( OConS.stream() .filter(filterUndConstraints) - .collect(Collectors.toCollection(Constraint::new)) ); + .collect(Collectors.toCollection(() -> newConsPair)) ); } ); ret.addOderConstraint(setCons);} ); return ret; diff --git a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java index f99dddad..22e60fa0 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java @@ -8,6 +8,7 @@ import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH; import java.util.*; import java.util.function.Predicate; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.typeinference.constraints.Constraint; import de.dhbwstuttgart.typeinference.constraints.Pair; @@ -590,8 +591,9 @@ public class FamilyOfGeneratedGenerics { Iterator itExtends2 = extendsSet.iterator(); while(itExtends2.hasNext()) { Pair pairExtends2 = itExtends2.next(); + Set containedVars = new HashSet<>(methodAndTPH.getTphs()); + String pairExtends2RHSName = ((TypePlaceholder)((resSet.resolveType((TypePlaceholder)pairExtends2.TA2)).resolvedType)).getName(); Iterator itEqual2 = equalSet.iterator(); - while (itEqual2.hasNext()) { boolean transClo = false; Pair pairEqual2 = itEqual2.next(); @@ -635,23 +637,29 @@ public class FamilyOfGeneratedGenerics { tempSet2.add(pairExtends2); } } + else { + containedVars.remove(((TypePlaceholder)newPair2.TA2).getName()); + } + } - Set containedVars = new HashSet<>(); - String key = ((TypePlaceholder)((resSet.resolveType((TypePlaceholder)pairExtends2.TA2)).resolvedType)).getName(); + + //String key = ((TypePlaceholder)((resSet.resolveType((TypePlaceholder)pairExtends2.TA2)).resolvedType)).getName(); //TODO: containedVars stimmt noch nicht. Ueberpruefen, ob ggf. mit den containedVars möglicherweise auch die anderen Faelle // rEqExRtilde isPairInTExTapostrophe abgedeckt sind => ggf. integrieren + /* posOfTphs.forEach((x,y) -> { if (y.contains(new PairTphMethod<>(PositionFinder.Position.METHOD, methodAndTPH.getId()))) { containedVars.add(x); }; } ); - if (containedVars.stream().filter(v -> tcOfCs.contains(new TPHConstraint(key, v, Relation.EXTENDS))) + */ + if (containedVars.stream().filter(v -> tcOfCs.contains(new TPHConstraint(pairExtends2RHSName, v, Relation.EXTENDS))) .count() > 0) { tempSet2.add(pairExtends2); } - if (posOfTphs.containsKey(key)) { - if (posOfTphs.get(key).contains(new PairTphMethod<>(PositionFinder.Position.METHOD, methodAndTPH.getId()))) { + if (posOfTphs.containsKey(pairExtends2RHSName)) { + if (posOfTphs.get(pairExtends2RHSName).contains(new PairTphMethod<>(PositionFinder.Position.METHOD, methodAndTPH.getId()))) { tempSet2.add(pairExtends2); } } diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 1936b5d2..90ba659c 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -213,10 +213,10 @@ public class StatementGenerator { ArrayList argTypes = argumentList.getArguments().stream() .map(x -> TypePlaceholder.fresh(methodInvocationContext.getStart())) .collect(Collectors.toCollection(ArrayList::new)); - MethodCall ret = new MethodCall(//new Void(new NullToken()), - TypePlaceholder.fresh(methodInvocationContext.getStart()), + MethodCall ret = new MethodCall(TypePlaceholder.fresh(methodInvocationContext.getStart()), getReceiver(receiver), name, argumentList, TypePlaceholder.fresh(methodInvocationContext.getStart()), argTypes, methodInvocationContext.getStart()); + ret.setStatement(); return ret; } @@ -303,33 +303,45 @@ public class StatementGenerator { ArrayList argTypes = args.getArguments().stream() .map(x -> TypePlaceholder.fresh(newExpression.getStart())) .collect(Collectors.toCollection(ArrayList::new)); - return new NewClass(newClass, args, null, argTypes, newExpression.getStart()); + Statement ret = new NewClass(newClass, args, null, argTypes, newExpression.getStart()); + ret.setStatement(); + return ret; } private Statement convert(Java8Parser.PreIncrementExpressionContext stmt) { Expression argument = convert(stmt.unaryExpression()); Token offset = stmt.getStart(); - return new UnaryExpr(UnaryExpr.Operation.PREINCREMENT, argument, TypePlaceholder.fresh(offset), offset); - } + Statement ret = new UnaryExpr(UnaryExpr.Operation.PREINCREMENT, argument, TypePlaceholder.fresh(offset), offset); + ret.setStatement(); + return ret; + } private Statement convert(Java8Parser.PreDecrementExpressionContext stmt) { - return new UnaryExpr(UnaryExpr.Operation.PREDECREMENT, convert(stmt.unaryExpression()), + Statement ret = new UnaryExpr(UnaryExpr.Operation.PREDECREMENT, convert(stmt.unaryExpression()), TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); + ret.setStatement(); + return ret; } private Statement convert(Java8Parser.PostIncrementExpressionContext stmt) { - return new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, convert(stmt.postfixExpression()), + Statement ret = new UnaryExpr(UnaryExpr.Operation.POSTINCREMENT, convert(stmt.postfixExpression()), TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); + ret.setStatement(); + return ret; } private Statement convert(Java8Parser.PostDecrementExpressionContext stmt) { - return new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, convert(stmt.postfixExpression()), + Statement ret = new UnaryExpr(UnaryExpr.Operation.POSTDECREMENT, convert(stmt.postfixExpression()), TypePlaceholder.fresh(stmt.getStart()), stmt.getStart()); + ret.setStatement(); + return ret; } private Statement convert(Java8Parser.AssignmentContext stmt) { AssignLeftSide leftHandSide = convert(stmt.leftHandSide()); - return new Assign(leftHandSide, convert(stmt.expression()), stmt.getStart()); + Statement ret = new Assign(leftHandSide, convert(stmt.expression()), stmt.getStart()); + ret.setStatement(); + return ret; } private AssignLeftSide convert(Java8Parser.LeftHandSideContext leftHandSide) { diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java index 75267619..4ef09d86 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/MethodCall.java @@ -24,13 +24,9 @@ public class MethodCall extends Statement public final ArgumentList arglist; - /* - * noetig fuer Bytecodegenerierung - */ public RefTypeOrTPHOrWildcardOrGeneric receiverType; public final ArrayList argTypes; - public MethodCall(RefTypeOrTPHOrWildcardOrGeneric retType, Receiver receiver, String methodName, ArgumentList argumentList, RefTypeOrTPHOrWildcardOrGeneric receiverType, ArrayList argTypes, Token offset){ super(retType,offset); @@ -40,6 +36,7 @@ public class MethodCall extends Statement this.receiverType = receiverType; this.argTypes = argTypes; } + @Override public void accept(StatementVisitor visitor) { diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Statement.java b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Statement.java index fc13cda1..aad50736 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Statement.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/statement/Statement.java @@ -6,11 +6,20 @@ import org.antlr.v4.runtime.Token; public abstract class Statement extends Expression { - + /* zeigt an, dass eine StatementExpression als Statement benutzt wird + */ + private boolean isStatement = false; public Statement(RefTypeOrTPHOrWildcardOrGeneric type, Token offset) { super(type, offset); } + public void setStatement() { + isStatement=true; + } + + public boolean getStatement() { + return isStatement; + } } \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/typeinference/constraints/Constraint.java b/src/main/java/de/dhbwstuttgart/typeinference/constraints/Constraint.java index 8715fe55..6ebb9404 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/constraints/Constraint.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/constraints/Constraint.java @@ -9,6 +9,12 @@ 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 + + /* es darf kein Constraint für den Return-Type erstellt werden, sonst gibt + * es Probleme beim Generated Generics + */ + public boolean isStatement = false; + private Constraint extendConstraint = null; public Constraint() { @@ -41,7 +47,7 @@ public class Constraint extends HashSet { } public String toString() { - return super.toString() + " isInherited = " + isInherited + return super.toString() + "\nisInherited = " + isInherited + "\nisStatement = " + isStatement //" + extendsContraint: " + (extendConstraint != null ? extendConstraint.toStringBase() : "null" ) + "\n" ; } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index b3ba00de..2860e71b 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -11,6 +11,7 @@ import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; import de.dhbwstuttgart.syntaxtree.factory.NameGenerator; import de.dhbwstuttgart.syntaxtree.statement.*; import de.dhbwstuttgart.syntaxtree.type.*; +import de.dhbwstuttgart.syntaxtree.type.Void; import de.dhbwstuttgart.typeinference.assumptions.FieldAssumption; import de.dhbwstuttgart.typeinference.assumptions.FunNClass; import de.dhbwstuttgart.typeinference.assumptions.MethodAssumption; @@ -180,6 +181,7 @@ public class TYPEStmt implements StatementVisitor{ ? new Pair(x.TA1, new ExtendsWildcardType(x.TA2, x.TA2.getOffset()), PairOperator.EQUALSDOT) : x) .collect(Collectors.toCollection(() -> new Constraint(oneMethodConstraint.isInherited()))); + extendsOneMethodConstraint.isStatement = oneMethodConstraint.isStatement; oneMethodConstraint.setExtendConstraint(extendsOneMethodConstraint); extendsOneMethodConstraint.setExtendConstraint(oneMethodConstraint); methodConstraints.add(extendsOneMethodConstraint); @@ -594,9 +596,11 @@ public class TYPEStmt implements StatementVisitor{ //methodConstraint.add(new Pair(forMethod.receiverType, retType, // PairOperator.EQUALSDOT)); //Fuer Bytecodegenerierung PL 2020-03-09 wird derzeit nicht benutzt ENDE - + + + methodConstraint.isStatement = forMethod.getStatement(); methodConstraint.add(new Pair(assumption.getReturnType(resolver), forMethod.getType(), - PairOperator.EQUALSDOT)); + PairOperator.EQUALSDOT)); methodConstraint.addAll(generateParameterConstraints(forMethod, assumption, info, resolver)); return methodConstraint; } diff --git a/src/test/java/insertGenerics/TestLocalVarLambda.java b/src/test/java/insertGenerics/TestLocalVarLambda.java index 73a316a4..a3592a24 100644 --- a/src/test/java/insertGenerics/TestLocalVarLambda.java +++ b/src/test/java/insertGenerics/TestLocalVarLambda.java @@ -76,7 +76,15 @@ public class TestLocalVarLambda { lmc.add(new MethodConstraint("SY", "java/lang/Object", Relation.EXTENDS)); methodConstraintsWithPositionTest.put("TPH DIUm(TPH O)", lmc); } - else { + else if (fogg.allConstraints.contains((new MethodConstraint("O", "DIV", Relation.EXTENDS)))) { + lmc.add(new MethodConstraint("O", "DIV", Relation.EXTENDS)); + lmc.add(new MethodConstraint("N", "java/lang/Object", Relation.EXTENDS)); + lmc.add(new MethodConstraint("DIV", "N", Relation.EXTENDS)); + lmc.add(new MethodConstraint("SY", "java/lang/Object", Relation.EXTENDS)); + methodConstraintsWithPositionTest.put("TPH Nm(TPH O)", lmc); + } + else + { lmc.add(new MethodConstraint("O", "DIU", Relation.EXTENDS)); lmc.add(new MethodConstraint("N", "java/lang/Object", Relation.EXTENDS)); lmc.add(new MethodConstraint("DIU", "N", Relation.EXTENDS));