diff --git a/pom.xml b/pom.xml
index f6a1fc87..98ea3933 100644
--- a/pom.xml
+++ b/pom.xml
@@ -14,7 +14,7 @@ http://maven.apache.org/maven-v4_0_0.xsd">
junit
junit
- 4.0
+ 4.11
test
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java
index 1c289835..cce7262c 100644
--- a/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java
+++ b/src/main/java/de/dhbwstuttgart/bytecode/BytecodeGen.java
@@ -25,10 +25,14 @@ import de.dhbwstuttgart.bytecode.signature.Signature;
import de.dhbwstuttgart.bytecode.signature.TypeToSignature;
import de.dhbwstuttgart.bytecode.signature.TypeToString;
import de.dhbwstuttgart.bytecode.simplifyRes.SimplifyResult;
-import de.dhbwstuttgart.bytecode.simplifyRes.SimplifyResultSourceFile;
+import de.dhbwstuttgart.bytecode.simplifyRes.GenericGenratorResultForSourceFile;
+import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult;
+import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResultForClass;
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
+import de.dhbwstuttgart.bytecode.utilities.MethodUtility;
import de.dhbwstuttgart.bytecode.utilities.NormalConstructor;
import de.dhbwstuttgart.bytecode.utilities.NormalMethod;
+import de.dhbwstuttgart.bytecode.utilities.Resolver;
import de.dhbwstuttgart.bytecode.utilities.Simplify;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
@@ -119,10 +123,12 @@ public class BytecodeGen implements ASTVisitor {
private final ArrayList fieldNameSignature = new ArrayList<>();
- private List simplifyResultsForAllSourceFiles;
- private SimplifyResult simplifyResult;
+ private List simplifyResultsForAllSourceFiles;
+ private GenericsGeneratorResultForClass generatedGenerics;
- public BytecodeGen(HashMap classFiles, Collection listOfResultSets, List simplifyResultsForAllSourceFiles, SourceFile sf,
+ private Resolver resolver;
+
+ public BytecodeGen(HashMap classFiles, Collection listOfResultSets, List simplifyResultsForAllSourceFiles, SourceFile sf,
String path) {
this.classFiles = classFiles;
this.listOfResultSets = listOfResultSets;
@@ -173,20 +179,19 @@ public class BytecodeGen implements ASTVisitor {
fieldInitializations = classOrInterface.getfieldInitializations();
// resultSet = listOfResultSets.get(0);
- boolean isConsWithNoParamsVisited = false;
boolean isVisited = false;
List listOfResultSetsList = new ArrayList<>(listOfResultSets);
- simplifyResult = simplifyResultsForAllSourceFiles.stream().map(sr->sr.getSimplifyResultsByName(pkgName, className)).findFirst().get();
+ generatedGenerics = simplifyResultsForAllSourceFiles.stream().map(sr->sr.getSimplifyResultsByName(pkgName, className)).findFirst().get();
for (int i = 0; i < listOfResultSetsList.size(); i++) {
//for (ResultSet rs : listOfResultSets) {
superClass = classOrInterface.getSuperClass().acceptTV(new TypeToDescriptor());
resultSet = listOfResultSetsList.get(i);
+ resolver = new Resolver(resultSet);
// tphExtractor.setResultSet(resultSet);
// Nur einmal ausführen!!
if (!isVisited) {
- tphsClass = simplifyResult.getTphsClass();
String sig = null;
/*
@@ -194,10 +199,10 @@ public class BytecodeGen implements ASTVisitor {
* Superclass
*/
if (classOrInterface.getGenerics().iterator().hasNext() || classOrInterface.getSuperClass().acceptTV(new TypeToSignature()).contains("<")
- || !tphsClass.isEmpty()) {
-
- List consClass = simplifyResult.getClassConstraints();
+ || !generatedGenerics.getClassConstraints().isEmpty()) {
+ List consClass = generatedGenerics.getClassConstraints();
+//
Signature signature = new Signature(classOrInterface, genericsAndBounds, consClass);
sig = signature.toString();
System.out.println("Signature: => " + sig);
@@ -243,20 +248,13 @@ public class BytecodeGen implements ASTVisitor {
field.getParameterList().accept(this);
- String methParamTypes = field.name + "%%";
+ String id = MethodUtility.createID(resolver, field);
- Iterator itr = field.getParameterList().iterator();
- while (itr.hasNext()) {
- FormalParameter fp = itr.next();
- methParamTypes += resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor()) + ";";
- }
-
- if (methodNameAndParamsT.contains(methParamTypes)) {
- System.out.println("ignore - Method: " + field.name + " , paramsType: " + methParamTypes);
+ if (methodNameAndParamsT.contains(id)) {
return;
}
- methodNameAndParamsT.add(methParamTypes);
- System.out.println("Method: " + field.name + " , paramsType: " + methParamTypes);
+ methodNameAndParamsT.add(id);
+ System.out.println("Constructor: " + field.name + " , paramsType: " + id);
String desc = null;
boolean hasGen = false;
@@ -271,9 +269,16 @@ public class BytecodeGen implements ASTVisitor {
}
String sig = null;
if (hasGen) {
- Map> constraints = simplifyResult.getMethodConstraintsByID(methParamTypes);
- Signature signature = new Signature(field, genericsAndBounds, methodParamsAndTypes, resultSet, constraints);
- sig = signature.toString();
+ List constraints = generatedGenerics.getClassConstraints();
+// Map> constraints = generatedGenerics.getMethodConstraintsByID(methParamTypes);
+//// ArrayList pairs = simplifyPairs(method.name,tphExtractor.allPairs,tphExtractor.allCons);
+ Signature signature = new Signature(genericsAndBounds,
+ methodParamsAndTypes, resultSet, constraints);
+
+ sig = signature.createSignatureForConstructor(field);
+// Map> constraints = generatedGenerics.getMethodConstraintsByID(methParamTypes);
+// Signature signature = new Signature(field, genericsAndBounds, methodParamsAndTypes, resultSet, constraints);
+// sig = signature.toString();
}
if (field.getParameterList().iterator().hasNext())
System.out.println(field.getParameterList().iterator().next().getType().acceptTV(new TypeToDescriptor()));
@@ -304,23 +309,19 @@ public class BytecodeGen implements ASTVisitor {
// stored in pos 0
// else it will be stored in pos 1 and this will be stored in pos 0
String retType = resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToDescriptor());
-// String retType = resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToSignature());
- String methParamTypes = retType + method.name + "%%";
- method.getParameterList().accept(this);
+
+ String id = MethodUtility.createID(resolver, method);
- Iterator itr = method.getParameterList().iterator();
- while (itr.hasNext()) {
- FormalParameter fp = itr.next();
- methParamTypes += resultSet.resolveType(fp.getType()).resolvedType.acceptTV(new TypeToDescriptor()) + ";";
- }
-
- if (methodNameAndParamsT.contains(methParamTypes)) {
+ if (methodNameAndParamsT.contains(id)) {
return;
}
- methodNameAndParamsT.add(methParamTypes);
- System.out.println("Method: " + method.name + " , paramsType: " + methParamTypes);
+ methodNameAndParamsT.add(id);
+ System.out.println("Method: " + method.name + " , paramsType: " + id);
// stores generics and their bounds of method
HashMap genericsAndBoundsMethod = new HashMap<>();
+
+ method.getParameterList().accept(this);
+
String methDesc = null;
// Method getModifiers() ?
@@ -360,11 +361,13 @@ public class BytecodeGen implements ASTVisitor {
if (hasGen || resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToString())
.equals("TPH")) {
- Map> constraints = simplifyResult.getMethodConstraintsByID(methParamTypes);
-// ArrayList pairs = simplifyPairs(method.name,tphExtractor.allPairs,tphExtractor.allCons);
- Signature signature = new Signature(method, genericsAndBoundsMethod, genericsAndBounds,
- methodParamsAndTypes, resultSet, constraints);
- sig = signature.toString();
+ List constraints = generatedGenerics.getMethodConstraintsByID(id);
+ List classConstraints = generatedGenerics.getClassConstraints();
+// Map> constraints = generatedGenerics.getMethodConstraintsByID(methParamTypes);
+//// ArrayList pairs = simplifyPairs(method.name,tphExtractor.allPairs,tphExtractor.allCons);
+ Signature signature = new Signature(genericsAndBoundsMethod, genericsAndBounds,
+ methodParamsAndTypes, resultSet, constraints,classConstraints);
+ sig = signature.createSignatureForMethod(method);
}
System.out.println(method.getName() + " ==> " + sig);
NormalMethod meth = new NormalMethod(method, genericsAndBounds, genericsAndBoundsMethod, hasGen);
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java b/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java
index 9850d902..aded8fd0 100644
--- a/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java
+++ b/src/main/java/de/dhbwstuttgart/bytecode/TPHExtractor.java
@@ -18,6 +18,7 @@ import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
import de.dhbwstuttgart.bytecode.utilities.MethodUtility;
import de.dhbwstuttgart.bytecode.utilities.Resolver;
import de.dhbwstuttgart.syntaxtree.AbstractASTWalker;
+import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Constructor;
import de.dhbwstuttgart.syntaxtree.Field;
import de.dhbwstuttgart.syntaxtree.FormalParameter;
@@ -41,10 +42,11 @@ public class TPHExtractor extends AbstractASTWalker {
// Alle TPHs der Felder werden iKopf der Klasse definiert
// alle TPHs der Klasse: (TPH, is in Method?)
public final HashMap allTPHS = new HashMap<>();
+ public final List tphsClass = new ArrayList<>();
MethodAndTPH methodAndTph;
Boolean inMethod = false;
- boolean inLocalOrParam = false;
+ boolean inLocalOrParamOrReturn = false;
public final ArrayList ListOfMethodsAndTph = new ArrayList<>();
final ArrayList> allPairs = new ArrayList<>();
@@ -64,15 +66,24 @@ public class TPHExtractor extends AbstractASTWalker {
public Resolver getResolver() {
return resolver;
}
-
+
+// @Override
+// public void visit(ClassOrInterface classOrInterface) {
+// inMethod = false;
+// classOrInterface.getfieldInitializations().ifPresent(c->c.block.accept(this));
+// super.visit(classOrInterface);
+// inMethod = true;
+// }
@Override
public void visit(TypePlaceholder tph) {
if (resultSet.resolveType(tph).resolvedType instanceof TypePlaceholder) {
TypePlaceholder resolvedTPH = (TypePlaceholder) resultSet.resolveType(tph).resolvedType;
if (inMethod) {
methodAndTph.getTphs().add(resolvedTPH.getName());
- if (inLocalOrParam)
+ if (inLocalOrParamOrReturn)
methodAndTph.getLocalTphs().add(resolvedTPH.getName());
+ }else {
+ tphsClass.add(resolvedTPH.getName());
}
allTPHS.put(resolvedTPH.getName(), inMethod);
@@ -142,9 +153,11 @@ public class TPHExtractor extends AbstractASTWalker {
public void visit(GenericRefType genericRefType) {
String name = genericRefType.getParsedName();
if (inMethod) {
- methodAndTph.getLocalTphs().add(name);
- if (inLocalOrParam)
+ methodAndTph.getTphs().add(name);
+ if (inLocalOrParamOrReturn)
methodAndTph.getLocalTphs().add(name);
+ }else {
+ tphsClass.add(name);
}
allTPHS.put(name, inMethod);
}
@@ -163,7 +176,15 @@ public class TPHExtractor extends AbstractASTWalker {
inMethod = true;
String id = MethodUtility.createID(resolver,method);
methodAndTph = new MethodAndTPH(id);
- super.visit(method);
+
+ inLocalOrParamOrReturn = true;
+ method.getReturnType().accept(this);
+ method.getParameterList().accept(this);
+ inLocalOrParamOrReturn = false;
+
+ if(method.block != null)
+ method.block.accept(this);
+
inMethod = false;
ListOfMethodsAndTph.add(methodAndTph);
}
@@ -177,23 +198,23 @@ public class TPHExtractor extends AbstractASTWalker {
@Override
public void visit(LocalVarDecl localVarDecl) {
- inLocalOrParam = true;
+// inLocalOrParamOrReturn = inMethod;
super.visit(localVarDecl);
- inLocalOrParam = false;
+// inLocalOrParamOrReturn = false;
}
@Override
public void visit(LocalVar localVar) {
- inLocalOrParam = true;
+// inLocalOrParamOrReturn = inMethod;
super.visit(localVar);
- inLocalOrParam = false;
+// inLocalOrParamOrReturn = false;
}
@Override
public void visit(ParameterList formalParameters) {
- inLocalOrParam = true;
+ inLocalOrParamOrReturn = inMethod;
super.visit(formalParameters);
- inLocalOrParam = false;
+ inLocalOrParamOrReturn = false;
}
}
\ No newline at end of file
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsSimplifier.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsSimplifier.java
index 9f85a483..525bab3c 100644
--- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsSimplifier.java
+++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsSimplifier.java
@@ -3,8 +3,7 @@
*/
package de.dhbwstuttgart.bytecode.genericsGenerator;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.*;
import de.dhbwstuttgart.bytecode.TPHExtractor;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
@@ -16,44 +15,112 @@ import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult;
*
*/
public class ConstraintsSimplifier {
- public static ConstraintsSimplierResult simplifyConstraints(TPHExtractor tphExtractor, List genericsGeneratorResults, List tphsClass) {
+ public static ConstraintsSimplierResult simplifyConstraints(final TPHExtractor tphExtractor, final List genericsGeneratorResults, final List tphsClass) {
List allCons = tphExtractor.allCons;
List equalCons = new ArrayList<>();
List simpleCycles = findSimpleCycles(allCons);
- if(!simpleCycles.isEmpty()) {
- handleSimpleCycles(allCons, simpleCycles);
- equalCons.addAll(GenericsGeneratorUtility.getEqualConstraints(allCons));
- }
-
- /* Step 2 */
+ /* Step 1 */
List foundCons = GenericsGeneratorUtility.findConstraintsWithLeftSameLeftSide(allCons);
if(!foundCons.isEmpty()) {
- List equalCons2 = GenericsGeneratorUtility.simplifyConstraintsWithSameLeftSide(foundCons,tphExtractor,tphsClass);
+ List equalCons2 = new ArrayList<>();
+ GenericsGeneratorUtility.simplifyConstraintsWithSameLeftSide(foundCons,tphExtractor,tphsClass,equalCons2);
equalCons.addAll(equalCons2);
}
+// if(!simpleCycles.isEmpty()) {
+// handleSimpleCycles(allCons, simpleCycles,tphExtractor);
+// equalCons.addAll(GenericsGeneratorUtility.getEqualConstraints(allCons));
+// }
+//
List longCycles = findLongCycles(allCons);
if(!longCycles.isEmpty()) {
- handleLongCycle(genericsGeneratorResults, allCons, longCycles);
+ handleLongCycle(genericsGeneratorResults, allCons, longCycles,tphExtractor);
}
/* The right hand side of equal constraint is the new name in name replacement result */
List nameReplacementResults = GenericsGeneratorUtility.createNameReplacementResultsFromEqualConstraints(equalCons);
+ getEqualsFromNameReplacementResults(genericsGeneratorResults,nameReplacementResults);
ConstraintsSimplierResult result = new ConstraintsSimplierResult(genericsGeneratorResults, nameReplacementResults);
return result;
}
+ private static void getEqualsFromNameReplacementResults(List genericsGeneratorResults,
+ List nameReplacementResults) {
+ genericsGeneratorResults.forEach(g->{
+ String tph = g.getConstraint().getLeft();
+ if (!nameReplacementResults.isEmpty()) {
+ collectAllEquals(nameReplacementResults, g, tph);
+ }
+
+ if (!nameReplacementResults.isEmpty()) {
+ collectAllEquals(nameReplacementResults, g);
+ }
+ });
+
+ }
+
+ private static void collectAllEquals(List nameReplacementResults, GenericsGeneratorResult g) {
+ Set equalsTPHs = g.getEqualsTPHs();
+ Set tphsToAdd = getAllEqualTphs(equalsTPHs, nameReplacementResults);
+ while (!tphsToAdd.isEmpty()){
+ equalsTPHs.addAll(tphsToAdd);
+ tphsToAdd = getAllEqualTphs(equalsTPHs, nameReplacementResults);
+ }
+ }
+
+ private static Set getAllEqualTphs(Set equalsTPHs, List nameReplacementResults) {
+ Set tphsToAdd = new HashSet<>();
+ equalsTPHs.forEach(e->{
+ collectAllEquals(nameReplacementResults, tphsToAdd,e);
+ });
+ return tphsToAdd;
+ }
+
+ public static void collectAllEquals(List nameReplacementResults, Set tphsToAdd,
+ String tph) {
+ List toRemoveList = new ArrayList<>();
+ Optional nameReplacementResult = nameReplacementResults.stream().filter(n->n.getName().equals(tph)).findFirst();
+ nameReplacementResult.ifPresent(toRemove->{
+ tphsToAdd.addAll(toRemove.getOldNames());
+ toRemoveList.add(toRemove);
+ });
+ if(!toRemoveList.isEmpty()){
+ nameReplacementResults.remove(toRemoveList.get(0));
+ toRemoveList.clear();
+ }
+ }
+
+ /**
+ * @param nameReplacementResults
+ * @param g
+ * @param tph
+ */
+ public static void collectAllEquals(List nameReplacementResults, GenericsGeneratorResult g,
+ String tph) {
+ List toRemoveList = new ArrayList<>();
+ Optional nameReplacementResult = nameReplacementResults.stream().filter(n->n.getName().equals(tph)).findFirst();
+ nameReplacementResult.ifPresent(toRemove->{
+ g.getEqualsTPHs().addAll(toRemove.getOldNames());
+ toRemoveList.add(toRemove);
+ });
+ if(!toRemoveList.isEmpty()){
+ nameReplacementResults.remove(toRemoveList.get(0));
+ toRemoveList.clear();
+ }
+ }
+
/**
* @param genericsGeneratorResults
* @param allCons
* @param longCycles
+ * @param tphExtractor
*/
public static void handleLongCycle(List genericsGeneratorResults,
- List allCons, List longCycles) {
+ List allCons, List longCycles, TPHExtractor tphExtractor) {
GenericsGeneratorUtility.modifyRelationForConstraintsinLongCycle(longCycles);
- List nameReplacementResults = GenericsGeneratorUtility.substituteTPHSFormLongCycle(allCons, longCycles);
+ List nameReplacementResults = GenericsGeneratorUtility.substituteTPHSFormLongCycle(allCons, longCycles,tphExtractor);
GenericsGeneratorUtility.createResults(genericsGeneratorResults,nameReplacementResults);
GenericsGeneratorUtility.removeEqualConstraints(allCons);
}
@@ -72,11 +139,13 @@ public class ConstraintsSimplifier {
/**
* @param allCons
* @param simpleCycles
+ * @param tphExtractor
*/
- public static void handleSimpleCycles(List allCons, List simpleCycles) {
+ public static void handleSimpleCycles(List allCons, List simpleCycles, TPHExtractor tphExtractor) {
GenericsGeneratorUtility.modifyRelation(simpleCycles);
GenericsGeneratorUtility.removeAllReverseConstraints(allCons,simpleCycles);
GenericsGeneratorUtility.substituteTPH(allCons, simpleCycles);
+ GenericsGeneratorUtility.replaceOldNames(simpleCycles,tphExtractor);
}
/**
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsWithSameLeftSide.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsWithSameLeftSide.java
index 6d3cbac3..93636cb7 100644
--- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsWithSameLeftSide.java
+++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/ConstraintsWithSameLeftSide.java
@@ -27,5 +27,9 @@ public class ConstraintsWithSameLeftSide {
public void setConstraints(List constraints) {
this.constraints = constraints;
}
-
+
+ @Override
+ public String toString() {
+ return "[" + constraints.toString() + "]";
+ }
}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/CyclesFinder.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/CyclesFinder.java
index f2212843..cf8c0885 100644
--- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/CyclesFinder.java
+++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/CyclesFinder.java
@@ -50,6 +50,8 @@ public class CyclesFinder {
List vistedConstraints = new ArrayList<>(allCons.size());
List longCycles = new ArrayList<>();
for (TPHConstraint constraint : allCons) {
+ if(constraint.getRel()==Relation.EQUAL)
+ continue;
if (!vistedConstraints.contains(constraint)) {
vistedConstraints.add(constraint);
checkConstraint(constraint, vistedConstraints, longCycles);
@@ -70,16 +72,16 @@ public class CyclesFinder {
if(containsInLongCycle(nextConstraint.get(), longCycles)) {
break;
}
- nextConstraint = getConstraintInRelation(tphsInRelation, maybeCycle);
if (isCycle(tphsInRelation)) {
addAllToVisitedConstraints(vistedConstraints, maybeCycle);
LongCycle cycle = new LongCycle(maybeCycle);
longCycles.add(cycle);
return;
}
+ nextConstraint = getConstraintInRelation(tphsInRelation, maybeCycle);
}
- addAllToVisitedConstraints(vistedConstraints, maybeCycle);
+// addAllToVisitedConstraints(vistedConstraints, maybeCycle);
}
private boolean containsInLongCycle(TPHConstraint c, List longCycles) {
@@ -99,7 +101,7 @@ public class CyclesFinder {
private Optional getConstraintInRelation(List tphsInRelation,
List maybeCycle) {
- TPHConstraint constraint = getConstraintByLeftSide(tphsInRelation.get(tphsInRelation.size()-1));
+ TPHConstraint constraint = getConstraintByLeftSide(tphsInRelation.get(tphsInRelation.size()-1),maybeCycle);
Optional nextConstraint = Optional.ofNullable(constraint);
if(nextConstraint.isPresent()) {
maybeCycle.add(constraint);
@@ -108,8 +110,12 @@ public class CyclesFinder {
return nextConstraint;
}
- private TPHConstraint getConstraintByLeftSide(String left) {
+ private TPHConstraint getConstraintByLeftSide(String left, List maybeCycle) {
for(TPHConstraint constraint : allCons) {
+ if(constraint.getRel()==Relation.EQUAL)
+ continue;
+ if(maybeCycle.contains(constraint))
+ continue;
if(constraint.getLeft().equals(left))
return constraint;
}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java
index 25a27a02..1e8f9f6e 100644
--- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java
+++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGenerator.java
@@ -4,13 +4,24 @@
package de.dhbwstuttgart.bytecode.genericsGenerator;
import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.LinkedList;
import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import org.objectweb.asm.Type;
import de.dhbwstuttgart.bytecode.TPHExtractor;
+import de.dhbwstuttgart.bytecode.constraint.ExtendsConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
+import de.dhbwstuttgart.bytecode.simplifyRes.GenericGeneratorResultsForAllMethods;
import de.dhbwstuttgart.bytecode.simplifyRes.ConstraintsSimplierResult;
import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult;
-import de.dhbwstuttgart.bytecode.utilities.MethodUtility;
+import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResultForClass;
+import de.dhbwstuttgart.bytecode.simplifyRes.MethodAndConstraints;
+import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
import de.dhbwstuttgart.syntaxtree.Method;
/**
@@ -19,17 +30,185 @@ import de.dhbwstuttgart.syntaxtree.Method;
*/
public class GenericsGenerator {
- public static List generateConstraintsForMethod(Method method, TPHExtractor tphExtractor,
- List tphsClass) {
- List genericsGeneratorResults = new ArrayList<>();
+ public static GenericsGeneratorResultForClass generateConstraints(final String className, final TPHExtractor tphExtractor,
+ final List tphsClass, final ConstraintsSimplierResult simplifiedConstraints) {
+
+ List classConstraints = generateConstraintsForClass(tphExtractor,
+ simplifiedConstraints, tphsClass);
+// GenericGeneratorResultsForAllMethods methodsAndConstraints = generateConstraintsForAllMethods(tphExtractor,
+// simplifiedConstraints, tphsClass);
+ GenericGeneratorResultsForAllMethods methodsAndConstraints = new GenericGeneratorResultsForAllMethods(new ArrayList<>());
- ConstraintsSimplierResult simplifiedConstraints = ConstraintsSimplifier.simplifyConstraints(tphExtractor, genericsGeneratorResults,tphsClass);
-
- List allCons = tphExtractor.allCons;
- String methodID = MethodUtility.createID(tphExtractor.getResolver(), method);
- List methodTPHS = GenericsGeneratorUtility.getMethodTPHS(methodID ,tphExtractor.ListOfMethodsAndTph);
-
- return null;
+ return new GenericsGeneratorResultForClass(className, classConstraints, methodsAndConstraints);
+ }
+
+
+ /**
+ * @param tphExtractor
+ * @param tphsClass
+ * @return
+ */
+ public static ConstraintsSimplierResult simplifyConstraints(final TPHExtractor tphExtractor,
+ final List tphsClass) {
+ final List genericsGeneratorResults = new ArrayList<>();
+
+ ConstraintsSimplierResult simplifiedConstraints = ConstraintsSimplifier.simplifyConstraints(tphExtractor,
+ genericsGeneratorResults, tphsClass);
+ return simplifiedConstraints;
}
+
+ private static List generateConstraintsForClass(final TPHExtractor tphExtractor,
+ final ConstraintsSimplierResult simplifiedConstraints, final List tphsClass) {
+ final List constraints = new ArrayList<>();
+
+ if(tphsClass.isEmpty())
+ return constraints;
+
+ final List allCons = tphExtractor.allCons;
+
+ final Set visitedTPHs = new HashSet<>();
+
+ final List methodTPHs = tphExtractor.ListOfMethodsAndTph.stream().map(m -> m.getLocalTphs())
+ .flatMap(l -> l.stream()).collect(Collectors.toList());
+ for (String tph : tphsClass) {
+
+ if (visitedTPHs.contains(tph))
+ continue;
+
+ final LinkedList tphsInRel = GenericsGeneratorUtility
+ .createLinkedListForTPHsInRelationClass(allCons, tphsClass, methodTPHs, visitedTPHs, tph);
+ if(!tphsInRel.isEmpty()) {
+ GenericsGeneratorResult constraint = generateGGResultForClass(tphsInRel, simplifiedConstraints, tphsClass);
+ constraints.add(constraint);
+ }
+ }
+
+ GenericsGeneratorUtility.addTPHsToClassTPHs(constraints, tphsClass);
+ GenericsGeneratorUtility.removeClassTPHsFromMethodTPHs(tphsClass, tphExtractor);
+
+ generateGGConstraintsForTPH(tphsClass, constraints, simplifiedConstraints);
+
+ return constraints;
+ }
+
+ private static GenericsGeneratorResult generateGGResultForClass(LinkedList tphsInRel,
+ ConstraintsSimplierResult simplifiedConstraints, List tphsClass) {
+ String subType = tphsInRel.getFirst();
+
+ String superType = GenericsGeneratorUtility.getNextClassTph(tphsClass, tphsInRel);
+
+ TPHConstraint constraint = new ExtendsConstraint(subType, superType);
+
+ Set equalSet = GenericsGeneratorUtility
+ .createEqualSet(simplifiedConstraints.getNameReplacementResults(), subType);
+
+ return new GenericsGeneratorResult(constraint, equalSet);
+ }
+
+ private static GenericGeneratorResultsForAllMethods generateConstraintsForAllMethods(
+ final TPHExtractor tphExtractor, ConstraintsSimplierResult simplifiedConstraints, List tphsClass) {
+
+ final List methodsAndConstraints = new ArrayList<>();
+ final GenericGeneratorResultsForAllMethods results = new GenericGeneratorResultsForAllMethods(
+ methodsAndConstraints);
+ tphExtractor.ListOfMethodsAndTph.forEach(m -> {
+ MethodAndConstraints methAndCons = generateConstraintsForMethod(tphExtractor.allCons, m,
+ simplifiedConstraints, tphsClass);
+ methodsAndConstraints.add(methAndCons);
+ });
+
+ return results;
+ }
+
+ public static MethodAndConstraints generateConstraintsForMethod(final List allCons,
+ final MethodAndTPH m, final ConstraintsSimplierResult simplifiedConstraints, List tphsClass) {
+ final List localTphs = m.getLocalTphs();
+
+ List constraints = new ArrayList<>();
+ MethodAndConstraints result = new MethodAndConstraints(m.getId(), constraints);
+
+ if(localTphs.isEmpty())
+ return result;
+
+ if(localTphs.size() == 1 && tphsClass.isEmpty()) {
+ generateGGConstraintsForTPH(localTphs, constraints, simplifiedConstraints);
+ return result;
+ }
+
+ final Set visitedTPHs = new HashSet<>();
+
+ for (String tph : localTphs) {
+
+ if (visitedTPHs.contains(tph))
+ continue;
+
+ final LinkedList tphsInRel = GenericsGeneratorUtility.createLinkedListForTPHsInRelation(allCons,
+ localTphs, tphsClass, visitedTPHs, tph);
+ if(!tphsInRel.isEmpty()) {
+ GenericsGeneratorUtility.removeNotLocalTphs(tphsInRel, localTphs, tphsClass);
+ if(!GenericsGeneratorUtility.isInResult(tphsInRel.getFirst(),constraints)) {
+ GenericsGeneratorResult constraint = generateGGResult(tphsInRel, simplifiedConstraints);
+ constraints.add(constraint);
+ }
+ }
+ }
+
+ generateGGConstraintsForTPH(localTphs, constraints, simplifiedConstraints);
+ return result;
+ }
+
+ private static void generateGGConstraintsForTPH(List localTphs,
+ final List constraints, final ConstraintsSimplierResult simplifiedConstraints) {
+
+ localTphs.forEach(tph -> {
+ boolean isInSimplifiedConstraints = GenericsGeneratorUtility.isTPHInGenericGeneratorResult(simplifiedConstraints.getGenericsGenResults(), tph);
+ if(isInSimplifiedConstraints) {
+ GenericsGeneratorResult ggResult = GenericsGeneratorUtility.getGenericGeneratorResult(simplifiedConstraints.getGenericsGenResults(),tph).get();
+// GenericsGeneratorUtility.removeGenericGeneratorResult(simplifiedConstraints.getGenericsGenResults(),ggResult);
+ constraints.add(ggResult);
+ } else {
+ boolean isInConstraints = GenericsGeneratorUtility.isTPHInGenericGeneratorResult(constraints, tph);
+
+ if (!isInConstraints) {
+ GenericsGeneratorResult ggResult = generateGGResult(tph, simplifiedConstraints.getNameReplacementResults());
+ constraints.add(ggResult);
+ }
+ }
+ });
+
+ }
+
+ private static GenericsGeneratorResult generateGGResult(String tph,
+ List nameReplacementResults) {
+ String superType = Type.getInternalName(Object.class);
+ TPHConstraint constraint = new ExtendsConstraint(tph, superType);
+ Set equalSet = GenericsGeneratorUtility.createEqualSet(nameReplacementResults, tph);
+
+ return new GenericsGeneratorResult(constraint, equalSet);
+ }
+
+ private static GenericsGeneratorResult generateGGResult(final LinkedList tphsInRel,
+ final ConstraintsSimplierResult simplifiedConstraints) {
+
+ String subType = tphsInRel.getFirst();
+
+ if (tphsInRel.size() == 1) {
+ Optional constraintFromSimplifiedCon = simplifiedConstraints
+ .getGenericsGenResults().stream().filter(g -> g.getConstraint().getLeft().equals(subType))
+ .findFirst();
+ if (constraintFromSimplifiedCon.isPresent())
+ return constraintFromSimplifiedCon.get();
+ }
+
+ String superType = tphsInRel.size() > 1 ? tphsInRel.getLast() : Type.getInternalName(Object.class);
+
+ TPHConstraint constraint = new ExtendsConstraint(subType, superType);
+
+ Set equalSet = GenericsGeneratorUtility
+ .createEqualSet(simplifiedConstraints.getNameReplacementResults(), subType);
+
+ return new GenericsGeneratorResult(constraint, equalSet);
+ }
+
}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGeneratorUtility.java b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGeneratorUtility.java
index e5ad4ccc..9db65374 100644
--- a/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGeneratorUtility.java
+++ b/src/main/java/de/dhbwstuttgart/bytecode/genericsGenerator/GenericsGeneratorUtility.java
@@ -5,11 +5,12 @@ package de.dhbwstuttgart.bytecode.genericsGenerator;
import java.util.ArrayList;
import java.util.HashSet;
+import java.util.LinkedList;
import java.util.List;
-import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
+import java.util.stream.Stream;
import org.objectweb.asm.Type;
@@ -118,30 +119,121 @@ public class GenericsGeneratorUtility {
}
- public static List simplifyConstraintsWithSameLeftSide(List consWithSameLeftSide,
- TPHExtractor tphExtractor, List tphsClass) {
- List eqCons = new ArrayList<>();
+ public static void simplifyConstraintsWithSameLeftSide(List consWithSameLeftSide,
+ TPHExtractor tphExtractor, List tphsClass, List equalCons) {
List allCons = tphExtractor.allCons;
for (ConstraintsWithSameLeftSide list : consWithSameLeftSide) {
+ if(!stillWithSameLeftSide(list))
+ continue;
NameReplacementResult replRes = modifyNames(allCons, list, tphExtractor.ListOfMethodsAndTph,tphsClass);
- createEqualConstraintsForNames(replRes.getName(), replRes.getOldNames(),eqCons);
+ createEqualConstraintsForNames(replRes.getName(), replRes.getOldNames(),equalCons);
for (TPHConstraint c : allCons) {
if (c.getRel() == Relation.EQUAL && replRes.getName().equals(c.getRight())) {
- eqCons.add(c);
+ equalCons.add(c);
}
}
- updateEqualCons(replRes, eqCons);
+ updateEqualCons(replRes, equalCons);
TPHConstraint c = list.getConstraints().get(0);
allCons.removeAll(list.getConstraints());
+ removeConstraintsWithSameLeftAndRightSide(allCons);
allCons.add(c);
+ removeDuplicate(allCons);
}
- return eqCons;
+ consWithSameLeftSide.clear();
+ consWithSameLeftSide = findConstraintsWithLeftSameLeftSide(allCons);
+ if(!consWithSameLeftSide.isEmpty())
+ simplifyConstraintsWithSameLeftSide(consWithSameLeftSide,tphExtractor,tphsClass,equalCons);
}
+ private static void removeDuplicate(List allCons) {
+ List visited = new ArrayList<>();
+ List toRemove = new ArrayList<>();
+ checkForDuplicate(allCons, visited, toRemove);
+ removeConstraintsByIndex(allCons, toRemove);
+ }
+
+ private static void removeConstraintsByIndex(List allCons, List toRemove) {
+ for(int index : toRemove)
+ allCons.remove(index);
+ }
+
+ private static void checkForDuplicate(List allCons, List visited, List toRemove) {
+ for(int i = 0;i allCons, List visited, List toRemove, int i) {
+ TPHConstraint constraint = allCons.get(i);
+ if(containsInList(visited,constraint.getLeft(),constraint.getRight())){
+ toRemove.add(i);
+ } else {
+ visited.add(constraint);
+ }
+ }
+
+ private static boolean containsInList(List allConstraints, String left, String right) {
+ for(TPHConstraint constraint : allConstraints)
+ if(constraint.getLeft().equals(left) && constraint.getRight().equals(right))
+ return true;
+ return false;
+ }
+
+ private static void addNewConstraintsWithSameLeftSide(ConstraintsWithSameLeftSide constraintsWithSameLeftSide, List allCons) {
+ allCons.forEach(c->{
+ if (constraintsWithSameLeftSide.getConstraints().get(0).getLeft().equals(c.getLeft())){
+ if(!constraintsWithSameLeftSide.getConstraints().contains(c))
+ constraintsWithSameLeftSide.getConstraints().add(c);
+ }
+ });
+ }
+
+ private static void checkForNewConstraintsWithSameLeftSide(List consWithSameLeftSide, List allCons) {
+ allCons.forEach(c->{
+ Optional constraintsWithSameLeftSide = getConstraintsWithSameLeftSide(consWithSameLeftSide, c.getLeft());
+ constraintsWithSameLeftSide.ifPresent(cls->{
+ if(!cls.getConstraints().contains(c))
+ cls.getConstraints().add(c);
+ });
+ });
+ }
+
+ private static Optional getConstraintsWithSameLeftSide(List consWithSameLeftSide, String left) {
+ return consWithSameLeftSide.stream().filter(csl->isContainedInConsWithLeftSide(csl,left)).findAny();
+ }
+
+ private static boolean isContainedInConsWithLeftSide(ConstraintsWithSameLeftSide csl, String left) {
+ return csl.getConstraints().get(0).getLeft().equals(left);
+ }
+
+ private static boolean stillWithSameLeftSide(ConstraintsWithSameLeftSide constraints) {
+ removeUnvalidConstraints(constraints);
+ return constraints.getConstraints().size()>1;
+ }
+
+ private static void removeUnvalidConstraints(ConstraintsWithSameLeftSide constraints) {
+ List toRemove = new ArrayList<>();
+ constraints.getConstraints().forEach(c->{
+ if(c.getLeft().equals(c.getRight()))
+ toRemove.add(c);
+ });
+ if(!toRemove.isEmpty())
+ constraints.getConstraints().removeAll(toRemove);
+ }
+
+ private static void removeConstraintsWithSameLeftAndRightSide(List allCons) {
+ List toRemove = new ArrayList<>();
+ allCons.forEach(c->{
+ if(c.getLeft().equals(c.getRight()))
+ toRemove.add(c);
+ });
+ allCons.removeAll(toRemove);
+ }
+
private static void createEqualConstraintsForNames(String name, List equalNames, List eqCons) {
// create an equal constraint for each value in repres
for (String eName : equalNames) {
@@ -191,20 +283,39 @@ public class GenericsGeneratorUtility {
longCycles.stream().map(lc->lc.getConstraints()).flatMap(e->e.stream()).forEach(c->c.setRel(Relation.EQUAL));
}
- public static List substituteTPHSFormLongCycle(List allCons, List longCycles) {
+ public static List substituteTPHSFormLongCycle(List allCons, List longCycles, TPHExtractor tphExtractor) {
List results = new ArrayList<>();
longCycles.forEach(lc->{
Set names = getNamesFromCycle(lc);
String newName = names.stream().findFirst().get();
List equalNames = new ArrayList<>(names);
+
+ Stream> tphsOfMethods = tphExtractor.ListOfMethodsAndTph.stream().map(m->m.getTphs());
+ Stream> localTphsOfMethods = tphExtractor.ListOfMethodsAndTph.stream().map(m->m.getLocalTphs());
+
+ replaceOldNames(newName, equalNames, tphsOfMethods);
+ replaceOldNames(newName, equalNames, localTphsOfMethods);
+
NameReplacementResult res = new NameReplacementResult(newName, equalNames);
results.add(res);
substituteAll(allCons,names,newName);
});
return results;
}
-
+
+ /**
+ * @param newName
+ * @param names
+ * @param tphsOfMethods
+ */
+ public static void replaceOldNames(final String newName, final List names, Stream> tphsOfMethods) {
+ tphsOfMethods.forEach(tphsMethod->{
+ if(tphsMethod.removeAll(names))
+ tphsMethod.add(newName);
+ });
+ }
+
public static void substituteAll(List allCons, Set names, String newName) {
allCons.stream()
.filter(c-> c.getRel()==Relation.EXTENDS)
@@ -268,4 +379,224 @@ public class GenericsGeneratorUtility {
return false;
return nameReplacementResults.stream().map(r->r.getName()).filter(n->name.equals(n)).findFirst().isPresent();
}
+
+ public static Optional getConstraintByLeftSide(List allCons, String tph) {
+ return allCons.stream().filter(c->c.getLeft().equals(tph)).findFirst();
+ }
+
+ public static Optional getConstraintByRightSide(List allCons, String tph) {
+ return allCons.stream().filter(c->c.getRight().equals(tph)).findFirst();
+ }
+
+ public static void replaceOldNames(List simpleCycles, TPHExtractor tphExtractor) {
+ simpleCycles.forEach(sc -> {
+ Stream> tphsOfMethods = tphExtractor.ListOfMethodsAndTph.stream().map(m->m.getTphs());
+ Stream> localTphsOfMethods = tphExtractor.ListOfMethodsAndTph.stream().map(m->m.getLocalTphs());
+
+ String newName = sc.getConstraint().getRight();
+ String oldName = sc.getConstraint().getLeft();
+
+ List names = new ArrayList<>();
+ names.add(oldName);
+ names.add(newName);
+
+ replaceOldNames(newName, names, tphsOfMethods);
+ replaceOldNames(newName, names, localTphsOfMethods);
+ });
+ }
+
+ public static boolean isTPHInGenericGeneratorResult(final List constraints, final String tph) {
+ return constraints.stream().filter(g->g.getConstraint().getLeft().equals(tph)).findFirst().isPresent();
+ }
+
+ /**
+ * @param allCons
+ * @param tphsMethod
+ * @param tphsClass
+ * @param visitedTPHs
+ * @param tph
+ * @return
+ */
+ public static LinkedList createLinkedListForTPHsInRelation(final List allCons,
+ List tphsMethod, List tphsClass, final Set visitedTPHs, String tph) {
+
+ final LinkedList tphsInRel = new LinkedList<>();
+ List tphsList = new ArrayList<>(tphsMethod);
+ tphsList.addAll(tphsClass);
+
+ boolean isNextSuperTypeFound = findNextSuperTyp(allCons,tphsList ,visitedTPHs, tph, tphsInRel);
+ if(!isNextSuperTypeFound)
+ findNextSubType(allCons, tphsList,visitedTPHs, tph, tphsInRel);
+ return tphsInRel;
+ }
+
+ public static boolean findNextSubType(List allCons, List tphsList, Set visitedTPHs, String tph,
+ LinkedList tphsInRel) {
+ Optional con = getConstraintByRightSide(allCons, tph);
+ while (con.isPresent()) {
+ String left = con.get().getLeft();
+ String right = con.get().getRight();
+
+ addTPHsToListFromLeft(tphsInRel, left, right);
+ markAsVisited(visitedTPHs, left);
+
+ if(tphsList.contains(left))
+ return true;
+
+ con = getConstraintByRightSide(allCons, left);
+ }
+ return false;
+ }
+
+ public static void addTPHsToListFromLeft(LinkedList tphsInRel, String left, String right) {
+ if (tphsInRel.isEmpty())
+ tphsInRel.add(right);
+
+ tphsInRel.addFirst(left);
+ }
+
+ /**
+ * @param allCons
+ * @param tphsList
+ * @param visitedTPHs
+ * @param tph
+ * @param tphsInRel
+ * @return
+ */
+ public static boolean findNextSuperTyp(final List allCons, List tphsList, final Set visitedTPHs, String tph,
+ final LinkedList tphsInRel) {
+ Optional con = getConstraintByLeftSide(allCons, tph);
+
+ markAsVisited(visitedTPHs, tph);
+
+ while (con.isPresent()) {
+ String left = con.get().getLeft();
+ String right = con.get().getRight();
+ addTPHsToList(tphsInRel, left, right);
+
+ if(tphsList.contains(right))
+ return true;
+
+ markAsVisited(visitedTPHs, right);
+
+ con = getConstraintByLeftSide(allCons, right);
+ }
+ return false;
+ }
+
+ public static void markAsVisited(Set visitedTPHs, String left) {
+ visitedTPHs.add(left);
+ }
+
+ /**
+ * @param visitedTPHs
+ * @param right
+ * @param left
+ */
+ public static void markAsVisited(final Set visitedTPHs, String left, String right) {
+ visitedTPHs.add(left);
+ visitedTPHs.add(right);
+ }
+
+ /**
+ * @param tphsInRel
+ * @param right
+ * @param left
+ */
+ public static void addTPHsToList(final LinkedList tphsInRel, String left, String right) {
+ if (tphsInRel.isEmpty())
+ tphsInRel.add(left);
+ tphsInRel.add(right);
+ }
+
+ public static void removeNotLocalTphs(final LinkedList tphsInRel, final List localTphs,
+ List tphsClass) {
+
+ while (!localTphs.contains(tphsInRel.peek())) {
+ tphsInRel.removeFirst();
+ }
+
+ String last = tphsInRel.peekLast();
+ while (!localTphs.contains(last) && !tphsClass.contains(last)) {
+ tphsInRel.removeLast();
+ last = tphsInRel.peekLast();
+ }
+ }
+
+ /**
+ * @param nameReplacementResults
+ * @param subType
+ * @return
+ */
+ public static Set createEqualSet(final List nameReplacementResults, String subType) {
+ Optional equals = nameReplacementResults.stream()
+ .filter(n -> n.getName().equals(subType)).findFirst();
+
+ Set equalSet = equals.isPresent() ? new HashSet<>(equals.get().getOldNames()) : new HashSet<>();
+ return equalSet;
+ }
+
+ public static String getNextClassTph(List tphsClass, LinkedList tphsInRel) {
+
+ for(int i = 1;i constraints, final List tphsClass) {
+
+ List lefts = constraints.stream().map(r->r.getConstraint()).map(c->c.getLeft()).collect(Collectors.toList());
+ List rights = constraints.stream().map(r->r.getConstraint()).map(c->c.getRight()).collect(Collectors.toList());
+ List leftsAndRights = new ArrayList<>(lefts);
+ leftsAndRights.addAll(rights);
+ List shouldBeClassTphs = leftsAndRights.stream().distinct().collect(Collectors.toList());
+
+ for(String tph : shouldBeClassTphs) {
+ if(!tphsClass.contains(tph))
+ tphsClass.add(tph);
+ }
+ }
+
+ public static void removeClassTPHsFromMethodTPHs(List tphsClass, TPHExtractor tphExtractor) {
+
+ tphExtractor.ListOfMethodsAndTph.stream().map(m->m.getTphs()).forEach(tphs->{
+ tphs.removeAll(tphsClass);
+ });
+
+ tphExtractor.ListOfMethodsAndTph.stream().map(m->m.getLocalTphs()).forEach(tphs->{
+ tphs.removeAll(tphsClass);
+ });
+
+ }
+
+ public static LinkedList createLinkedListForTPHsInRelationClass(List allCons,
+ List tphsClass, List methodTPHs, Set visitedTPHs, String tph) {
+ final LinkedList tphsInRel = new LinkedList<>();
+ boolean isNextSuperTypeFound = findNextSuperTyp(allCons,tphsClass ,visitedTPHs, tph, tphsInRel);
+ if(!tphsInRel.isEmpty() && !isNextSuperTypeFound && !methodTPHs.contains(tphsInRel.getLast()))
+ findNextSubType(allCons, tphsClass,visitedTPHs, tph, tphsInRel);
+ return tphsInRel;
+ }
+
+ public static boolean isInResult(String first, List constraints) {
+ return constraints.stream().map(r->r.getConstraint()).filter(c->c.getLeft().equals(first)).findFirst().isPresent();
+ }
+
+ public static Optional getGenericGeneratorResult(List list,
+ String tph) {
+ return list.stream().filter(g->g.getConstraint().getLeft().equals(tph)).findFirst();
+ }
+
+ public static void removeGenericGeneratorResult(List genericsGenResults,
+ GenericsGeneratorResult ggResult) {
+ genericsGenResults.remove(ggResult);
+ }
+
+ public static Optional getMethodAndTphs(ArrayList listOfMethodsAndTph, String id) {
+ return listOfMethodsAndTph.stream().filter(m->m.getId().equals(id)).findFirst();
+ }
}
diff --git a/src/main/java/de/dhbwstuttgart/bytecode/signature/Signature.java b/src/main/java/de/dhbwstuttgart/bytecode/signature/Signature.java
index cb18e6b5..bad62616 100644
--- a/src/main/java/de/dhbwstuttgart/bytecode/signature/Signature.java
+++ b/src/main/java/de/dhbwstuttgart/bytecode/signature/Signature.java
@@ -1,12 +1,6 @@
package de.dhbwstuttgart.bytecode.signature;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
import org.objectweb.asm.Type;
import org.objectweb.asm.signature.SignatureVisitor;
@@ -16,6 +10,7 @@ import de.dhbwstuttgart.bytecode.constraint.ExtendsConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor;
+import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult;
import de.dhbwstuttgart.bytecode.utilities.Simplify;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Constructor;
@@ -32,86 +27,147 @@ import de.dhbwstuttgart.typeinference.result.ResolvedType;
import de.dhbwstuttgart.typeinference.result.ResultSet;
public class Signature {
+ private static final char SUPER_CHAR = '-';
+ private static final char EXTENDS_CHAR = '+';
+ private static final String SPECIAL_CHAR_FOR_FUN = "$$";
+ private static final String SPECIAL_CHAR = "$";
private ClassOrInterface classOrInterface;
private HashMap genericsAndBounds;
private HashMap genericsAndBoundsMethod;
- private SignatureWriter sw;
+ private final SignatureWriter sw = new SignatureWriter();;
private Constructor constructor;
private Method method;
- private HashMap methodParamsAndTypes;
+ private HashMap methodParamsAndTypes;
private ResultSet resultSet;
private Map> methodConstraints;
- private List consClass;
-
- public Signature(ClassOrInterface classOrInterface, HashMap genericsAndBounds,
- List consClass) {
- this.classOrInterface = classOrInterface;
- this.genericsAndBounds = genericsAndBounds;
- this.consClass = consClass;
- sw = new SignatureWriter();
- createSignatureForClassOrInterface();
- }
+ private List consClass;
+ private List constraints;
- public Signature(Constructor constructor, HashMap genericsAndBounds,
- HashMap methodParamsAndTypes,ResultSet resultSet,
- Map> methodConstraints) {
- this.constructor = constructor;
+// public Signature(ClassOrInterface classOrInterface, HashMap genericsAndBounds,
+// List consClass) {
+// this.classOrInterface = classOrInterface;
+// this.genericsAndBounds = genericsAndBounds;
+// this.consClass = consClass;
+// sw = new SignatureWriter();
+// createSignatureForClassOrInterface();
+// }
+
+ public Signature(HashMap genericsAndBounds,
+ HashMap methodParamsAndTypes, ResultSet resultSet,
+ List constraints) {
+ //this.constructor = constructor;
this.genericsAndBounds = genericsAndBounds;
this.methodParamsAndTypes = methodParamsAndTypes;
this.resultSet = resultSet;
- this.methodConstraints = methodConstraints;
- sw = new SignatureWriter();
- createSignatureForConsOrMethod(this.constructor,true);
+ this.constraints = constraints;
}
- public Signature(Method method, HashMap genericsAndBoundsMethod,HashMap genericsAndBounds,
- HashMap methodParamsAndTypes, ResultSet resultSet,
- Map> constraints) {
- this.method = method;
+ public Signature(int numberOfParams) {
+ createSignatureForFunN(numberOfParams);
+ }
+
+ public Signature(int numberOfParams, String to, String[] paramTypes) {
+ createSignatureForFunN(numberOfParams, to, paramTypes);
+ }
+
+ public Signature(ClassOrInterface classOrInterface, HashMap genericsAndBounds,
+ List consClass) {
+ this.classOrInterface = classOrInterface;
+ this.genericsAndBounds = genericsAndBounds;
+ this.consClass = consClass;
+ createSignatureForClassOrInterface();
+ }
+
+ public Signature(HashMap genericsAndBoundsMethod,
+ HashMap genericsAndBounds,
+ HashMap methodParamsAndTypes, ResultSet resultSet,
+ List constraints,List consClass) {
+ //this.method = method;
this.genericsAndBoundsMethod = genericsAndBoundsMethod;
this.genericsAndBounds = genericsAndBounds;
this.methodParamsAndTypes = methodParamsAndTypes;
this.resultSet = resultSet;
- this.methodConstraints = constraints;
- sw = new SignatureWriter();
- createSignatureForConsOrMethod(this.method,false);
+ this.constraints = constraints;
+ this.consClass = consClass;
+ //createSignatureForMethod(this.method);
}
-
- public Signature(int numberOfParams) {
- sw = new SignatureWriter();
- createSignatureForFunN(numberOfParams);
+
+ public String createSignatureForConstructor(Constructor constructor) {
+ visitParams();
+ sw.visitReturnType().visitBaseType('V');
+ return sw.toString();
}
-
- public Signature(int numberOfParams, String to, String[] paramTypes) {
- sw = new SignatureWriter();
- createSignatureForFunN(numberOfParams, to, paramTypes);
+
+ public String createSignatureForMethod(Method method) {
+ defineGenerics(method);
+
+ String ret = resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToSignature());
+
+ visitParams();
+ visitReturnType(method, ret);
+ return sw.toString();
}
-
+
+ /**
+ * @param method
+ * @param ret
+ */
+ private void visitReturnType(Method method, String ret) {
+ if (ret.equals("V")) {
+ sw.visitReturnType().visitBaseType('V');
+ } else {
+ RefTypeOrTPHOrWildcardOrGeneric returnType = method.getReturnType();
+ SignatureVisitor sv = sw.visitReturnType();
+ doVisitParamsOrReturn(returnType, sv);
+ }
+ }
+
+ /**
+ *
+ */
+ private void visitParams() {
+ for (String paramName : methodParamsAndTypes.keySet()) {
+ RefTypeOrTPHOrWildcardOrGeneric t = methodParamsAndTypes.get(paramName);
+ SignatureVisitor sv = sw.visitParameterType();
+ doVisitParamsOrReturn(t, sv);
+ }
+ }
+
+ /**
+ *
+ * @param method
+ */
+ private void defineGenerics(Method method) {
+ method.getGenerics().forEach(g -> {
+ visitTypeVarsAndTheirBounds(g, genericsAndBoundsMethod);
+ });
+ defineGenericsFromConstraints(constraints,genericsAndBoundsMethod);
+ }
+
private void createSignatureForFunN(int numberOfParams, String to, String[] paramTypes) {
- for(int i = 0;i(params L.. OR T.. Or basistape)ReturnType
+ * Creates signature for a method or constructor with @see
+ * {@link SignatureWriter} Signature looks like: (params L.. OR T.. Or basistape)ReturnType
*
- * @param method method or constructor
+ * @param method method or constructor
* @param isConstructor true if constructor
*/
private void createSignatureForConsOrMethod(Method method, boolean isConstructor) {
Iterator extends GenericTypeVar> itr = method.getGenerics().iterator();
// visits all formal type parameter and visits their bounds
- while(itr.hasNext()) {
+ while (itr.hasNext()) {
System.out.println("HAS GENERICS!!");
GenericTypeVar g = itr.next();
- getBoundsOfTypeVar(g,genericsAndBoundsMethod);
+ visitTypeVarsAndTheirBounds(g, genericsAndBoundsMethod);
}
-
+
// Wenn die RückgabeType eine TPH ist, wird als generic behandelt
- // z.B: Type = TPH K => wird eine Formal Type Parameter K$ erzeugt und Bound = Object
- if(!isConstructor) {
+ // z.B: Type = TPH K => wird eine Formal Type Parameter K$ erzeugt und Bound =
+ // Object
+ if (!isConstructor) {
String ret = resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToSignature());
ArrayList allConsBeforeSimplify = new ArrayList<>();
-
- Map> allConstraints = new HashMap<>();
-
- if(ret.contains("<")) {
- allConsBeforeSimplify = getAllConstraints((RefType) resultSet.resolveType(method.getReturnType()).resolvedType);
+
+ Map> allConstraints = new HashMap<>();
+
+ if (ret.contains("<")) {
+ allConsBeforeSimplify = getAllConstraints(
+ (RefType) resultSet.resolveType(method.getReturnType()).resolvedType);
}
-
- for(String paramName : methodParamsAndTypes.keySet()) {
+
+ for (String paramName : methodParamsAndTypes.keySet()) {
RefTypeOrTPHOrWildcardOrGeneric t = methodParamsAndTypes.get(paramName);
- String pT = t.acceptTV(new TypeToSignature());
-
- if(pT.contains("<"))
+ String pT = t.acceptTV(new TypeToSignature());
+
+ if (pT.contains("<"))
allConsBeforeSimplify = getAllConstraints((RefType) t);
}
boolean doSimplify = false;
- if(!allConsBeforeSimplify.isEmpty()) {
- addConstraintsToMap(allConstraints,allConsBeforeSimplify);
+ if (!allConsBeforeSimplify.isEmpty()) {
+ addConstraintsToMap(allConstraints, allConsBeforeSimplify);
doSimplify = true;
}
createTypeVars(allConstraints, doSimplify);
-
- if(!ret.equals("V")) {
- if(ret.contains("$") && !ret.contains("$$") && !ret.contains("<")) {
- if(genericsAndBounds.containsKey(ret)) {
+
+ if (!ret.equals("V")) {
+ if (ret.contains(SPECIAL_CHAR) && !ret.contains(SPECIAL_CHAR_FOR_FUN) && !ret.contains("<")) {
+ if (genericsAndBounds.containsKey(ret)) {
genericsAndBoundsMethod.put(ret.substring(1), genericsAndBounds.get(ret.substring(1)));
}
}
-
+
}
}
-
- // visit each method-parameter to create the signature
- for(String paramName : methodParamsAndTypes.keySet()) {
- RefTypeOrTPHOrWildcardOrGeneric t = methodParamsAndTypes.get(paramName);
- // parameter type deswegen ist true
- doVisitParamsOrReturn(t,true);
- }
- if(isConstructor ||
- resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToSignature()).equals("V")) {
+
+ visitParams();
+ if (isConstructor || resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToSignature())
+ .equals("V")) {
sw.visitReturnType().visitBaseType('V');
- }else {
+ } else {
RefTypeOrTPHOrWildcardOrGeneric returnType = method.getReturnType();
// return type deswegen ist false
- doVisitParamsOrReturn(returnType, false);
+// doVisitParamsOrReturn(returnType, false);
}
// sw.visitEnd();
}
-
+
private void addConstraintsToMap(Map> allConstraints,
ArrayList