Merge remote-tracking branch 'origin/bytecode2' into simplifyRes

Conflicts:
	src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericGenratorResultForSourceFile.java
	src/main/java/de/dhbwstuttgart/bytecode/simplifyRes/GenericsGeneratorResultForClass.java
This commit is contained in:
Michael Uhl 2019-09-17 09:36:56 +02:00
commit 9741b5e14e
36 changed files with 356 additions and 1840 deletions

View File

@ -3,13 +3,9 @@ package de.dhbwstuttgart.bytecode;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
@ -18,22 +14,18 @@ import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import de.dhbwstuttgart.bytecode.Exception.BytecodeGeneratorError;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.descriptor.DescriptorToString;
import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor;
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.GenericGenratorResultForSourceFile;
import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult;
import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResultForClass;
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResultForClass;
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;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
@ -80,7 +72,6 @@ import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.result.GenericInsertPair;
import de.dhbwstuttgart.typeinference.result.ResultSet;
public class BytecodeGen implements ASTVisitor {
@ -204,7 +195,7 @@ public class BytecodeGen implements ASTVisitor {
List<GenericsGeneratorResult> consClass = generatedGenerics.getClassConstraints();
//
Signature signature = new Signature(classOrInterface, genericsAndBounds, consClass);
sig = signature.toString();
sig = signature.createSignatureForClassOrInterface();
System.out.println("Signature: => " + sig);
}
@ -219,12 +210,7 @@ public class BytecodeGen implements ASTVisitor {
}
for (Constructor c : classOrInterface.getConstructors()) {
// if(!isConsWithNoParamsVisited) {
c.accept(this);
// }
// if(!c.getParameterList().iterator().hasNext())
// isConsWithNoParamsVisited = true;
}
for (Method m : classOrInterface.getMethods()) {
@ -237,12 +223,6 @@ public class BytecodeGen implements ASTVisitor {
@Override
public void visit(Constructor field) {
System.out.println("ResultSet: ");
resultSet.results.forEach(a -> {
System.out.println(a.getLeft().toString() + " = " + a.getRight().toString());
});
System.out.println("---------------");
// stores generics and their bounds of method
HashMap<String, String> genericsAndBoundsMethod = new HashMap<>();
@ -270,15 +250,10 @@ public class BytecodeGen implements ASTVisitor {
String sig = null;
if (hasGen) {
List<GenericsGeneratorResult> constraints = generatedGenerics.getClassConstraints();
// Map<TPHConstraint, Set<String>> constraints = generatedGenerics.getMethodConstraintsByID(methParamTypes);
//// ArrayList<GenericInsertPair> pairs = simplifyPairs(method.name,tphExtractor.allPairs,tphExtractor.allCons);
Signature signature = new Signature(genericsAndBounds,
methodParamsAndTypes, resultSet, constraints);
sig = signature.createSignatureForConstructor(field);
// Map<TPHConstraint, Set<String>> 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()));
@ -363,8 +338,6 @@ public class BytecodeGen implements ASTVisitor {
List<GenericsGeneratorResult> constraints = generatedGenerics.getMethodConstraintsByID(id);
List<GenericsGeneratorResult> classConstraints = generatedGenerics.getClassConstraints();
// Map<TPHConstraint, Set<String>> constraints = generatedGenerics.getMethodConstraintsByID(methParamTypes);
//// ArrayList<GenericInsertPair> pairs = simplifyPairs(method.name,tphExtractor.allPairs,tphExtractor.allCons);
Signature signature = new Signature(genericsAndBoundsMethod, genericsAndBounds,
methodParamsAndTypes, resultSet, constraints,classConstraints);
sig = signature.createSignatureForMethod(method);
@ -373,7 +346,6 @@ public class BytecodeGen implements ASTVisitor {
NormalMethod meth = new NormalMethod(method, genericsAndBounds, genericsAndBoundsMethod, hasGen);
methDesc = meth.accept(new DescriptorToString(resultSet));
// System.out.println(methDesc);
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC + acc, method.getName(), methDesc, sig, null);
mv.visitCode();

View File

@ -1,12 +1,12 @@
/**
*
*/
package de.dhbwstuttgart.bytecode.simplifyRes;
package de.dhbwstuttgart.bytecode.genericsGenerator;
import java.util.List;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.genericsGenerator.NameReplacementResult;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.NameReplacementResult;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult;
/**
* This class represents the result of the constraints simplifier.

View File

@ -7,8 +7,7 @@ import java.util.*;
import de.dhbwstuttgart.bytecode.TPHExtractor;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.simplifyRes.ConstraintsSimplierResult;
import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.*;
/**
* @author fayez
@ -19,7 +18,6 @@ public class ConstraintsSimplifier {
List<TPHConstraint> allCons = tphExtractor.allCons;
List<TPHConstraint> equalCons = new ArrayList<>();
List<SimpleCycle> simpleCycles = findSimpleCycles(allCons);
/* Step 1 */
List<ConstraintsWithSameLeftSide> foundCons = GenericsGeneratorUtility.findConstraintsWithLeftSameLeftSide(allCons);
@ -34,10 +32,10 @@ public class ConstraintsSimplifier {
// equalCons.addAll(GenericsGeneratorUtility.getEqualConstraints(allCons));
// }
//
List<LongCycle> longCycles = findLongCycles(allCons);
List<Cycle> cycles = findCycles(allCons);
if(!longCycles.isEmpty()) {
handleLongCycle(genericsGeneratorResults, allCons, longCycles,tphExtractor);
if(!cycles.isEmpty()) {
handleCycle(genericsGeneratorResults, allCons, cycles,tphExtractor);
}
/* The right hand side of equal constraint is the new name in name replacement result */
List<NameReplacementResult> nameReplacementResults = GenericsGeneratorUtility.createNameReplacementResultsFromEqualConstraints(equalCons);
@ -114,13 +112,13 @@ public class ConstraintsSimplifier {
/**
* @param genericsGeneratorResults
* @param allCons
* @param longCycles
* @param cycles
* @param tphExtractor
*/
public static void handleLongCycle(List<GenericsGeneratorResult> genericsGeneratorResults,
List<TPHConstraint> allCons, List<LongCycle> longCycles, TPHExtractor tphExtractor) {
GenericsGeneratorUtility.modifyRelationForConstraintsinLongCycle(longCycles);
List<NameReplacementResult> nameReplacementResults = GenericsGeneratorUtility.substituteTPHSFormLongCycle(allCons, longCycles,tphExtractor);
public static void handleCycle(List<GenericsGeneratorResult> genericsGeneratorResults,
List<TPHConstraint> allCons, List<Cycle> cycles, TPHExtractor tphExtractor) {
GenericsGeneratorUtility.modifyRelationForConstraintsInCycle(cycles);
List<NameReplacementResult> nameReplacementResults = GenericsGeneratorUtility.substituteTPHSFormCycle(allCons, cycles,tphExtractor);
GenericsGeneratorUtility.createResults(genericsGeneratorResults,nameReplacementResults);
GenericsGeneratorUtility.removeEqualConstraints(allCons);
}
@ -129,33 +127,11 @@ public class ConstraintsSimplifier {
* @param allCons
* @return
*/
public static List<LongCycle> findLongCycles(List<TPHConstraint> allCons) {
/* find all long cycles */
public static List<Cycle> findCycles(List<TPHConstraint> allCons) {
/* find all cycles */
CyclesFinder cyclesFinder = new CyclesFinder(allCons);
List<LongCycle> longCycles = cyclesFinder.findLongCycles();
return longCycles;
List<Cycle> cycles = cyclesFinder.findCycles();
return cycles;
}
/**
* @param allCons
* @param simpleCycles
* @param tphExtractor
*/
public static void handleSimpleCycles(List<TPHConstraint> allCons, List<SimpleCycle> simpleCycles, TPHExtractor tphExtractor) {
GenericsGeneratorUtility.modifyRelation(simpleCycles);
GenericsGeneratorUtility.removeAllReverseConstraints(allCons,simpleCycles);
GenericsGeneratorUtility.substituteTPH(allCons, simpleCycles);
GenericsGeneratorUtility.replaceOldNames(simpleCycles,tphExtractor);
}
/**
* @param allCons
* @return
*/
public static List<SimpleCycle> findSimpleCycles(List<TPHConstraint> allCons) {
CyclesFinder cyclesFinder = new CyclesFinder(allCons);
/* find all simple cycles if they are exist */
List<SimpleCycle> simpleCycles = cyclesFinder.findSimpleCycles();
return simpleCycles;
}
}

View File

@ -10,6 +10,7 @@ import java.util.Optional;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.Cycle;
/**
* @author fayez
@ -22,46 +23,22 @@ public class CyclesFinder {
this.allCons = allCons;
}
public List<SimpleCycle> findSimpleCycles() {
List<SimpleCycle> simpleCycles = new ArrayList<>();
allCons.forEach(c -> {
String left = c.getLeft();
String right = c.getRight();
if (c.getRel() == Relation.EXTENDS && !containsInCycle(c, simpleCycles)) {
Optional<TPHConstraint> revCon = GenericsGeneratorUtility.getReverseConstraint(allCons, left, right);
if (revCon.isPresent()) {
TPHConstraint reverseConstraint = revCon.get();
SimpleCycle simpleCycle = new SimpleCycle(c, reverseConstraint);
simpleCycles.add(simpleCycle);
}
}
});
return simpleCycles;
}
private boolean containsInCycle(TPHConstraint c, List<SimpleCycle> simpleCycles) {
return simpleCycles.stream().filter(sc -> {
return sc.contains(c);
}).count() > 0;
}
public List<LongCycle> findLongCycles() {
public List<Cycle> findCycles() {
List<TPHConstraint> vistedConstraints = new ArrayList<>(allCons.size());
List<LongCycle> longCycles = new ArrayList<>();
List<Cycle> cycles = new ArrayList<>();
for (TPHConstraint constraint : allCons) {
if(constraint.getRel()==Relation.EQUAL)
continue;
if (!vistedConstraints.contains(constraint)) {
vistedConstraints.add(constraint);
checkConstraint(constraint, vistedConstraints, longCycles);
checkConstraint(constraint, vistedConstraints, cycles);
}
}
return longCycles;
return cycles;
}
private void checkConstraint(TPHConstraint constraint, List<TPHConstraint> vistedConstraints,
List<LongCycle> longCycles) {
List<Cycle> cycles) {
List<String> tphsInRelation = new LinkedList<>();
List<TPHConstraint> maybeCycle = new ArrayList<>();
maybeCycle.add(constraint);
@ -69,13 +46,13 @@ public class CyclesFinder {
tphsInRelation.add(constraint.getRight());
Optional<TPHConstraint> nextConstraint = getConstraintInRelation(tphsInRelation, maybeCycle);
while (nextConstraint.isPresent()) {
if(containsInLongCycle(nextConstraint.get(), longCycles)) {
if(containsInLongCycle(nextConstraint.get(), cycles)) {
break;
}
if (isCycle(tphsInRelation)) {
addAllToVisitedConstraints(vistedConstraints, maybeCycle);
LongCycle cycle = new LongCycle(maybeCycle);
longCycles.add(cycle);
Cycle cycle = new Cycle(maybeCycle);
cycles.add(cycle);
return;
}
nextConstraint = getConstraintInRelation(tphsInRelation, maybeCycle);
@ -84,8 +61,8 @@ public class CyclesFinder {
// addAllToVisitedConstraints(vistedConstraints, maybeCycle);
}
private boolean containsInLongCycle(TPHConstraint c, List<LongCycle> longCycles) {
for(LongCycle cycle : longCycles) {
private boolean containsInLongCycle(TPHConstraint c, List<Cycle> cycles) {
for(Cycle cycle : cycles) {
if(cycle.containConstraint(c))
return true;
}

View File

@ -1,32 +1,20 @@
/**
*
*/
package de.dhbwstuttgart.bytecode.simplifyRes;
package de.dhbwstuttgart.bytecode.genericsGenerator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import de.dhbwstuttgart.bytecode.BytecodeGen;
import de.dhbwstuttgart.bytecode.TPHExtractor;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor;
import de.dhbwstuttgart.bytecode.genericsGenerator.GenericsGenerator;
import de.dhbwstuttgart.bytecode.genericsGenerator.GenericsGeneratorUtility;
import de.dhbwstuttgart.bytecode.signature.Signature;
import de.dhbwstuttgart.bytecode.signature.TypeToSignature;
import de.dhbwstuttgart.bytecode.signature.TypeToString;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResultForClass;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.MethodAndConstraints;
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
import de.dhbwstuttgart.bytecode.utilities.MethodUtility;
import de.dhbwstuttgart.bytecode.utilities.Resolver;
import de.dhbwstuttgart.bytecode.utilities.Simplify;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.AssignToLocal;
import de.dhbwstuttgart.syntaxtree.ASTVisitor;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
@ -69,7 +57,6 @@ import de.dhbwstuttgart.syntaxtree.statement.WhileStmt;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.typeinference.result.ResultSet;
@ -148,14 +135,8 @@ public class GeneratedGenericsFinder implements ASTVisitor {
if(!isVisited) {
ggResult = GenericsGenerator.generateConstraints(className, tphExtractor, tphsClass,simplifiedConstraints);
isVisited = true;
} else {
}
// for(Constructor m : classOrInterface.getConstructors()) {
// addMethodConstraints(simplifiedConstraints, ggResult, m);
// }
for(Method m : classOrInterface.getMethods()) {
addMethodConstraints(simplifiedConstraints, ggResult, m);

View File

@ -11,24 +11,24 @@ import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.*;
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.simplifyRes.GenericsGeneratorResultForClass;
import de.dhbwstuttgart.bytecode.simplifyRes.MethodAndConstraints;
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
import de.dhbwstuttgart.syntaxtree.Method;
/**
* @author fayez
*
*/
public class GenericsGenerator {
/*TODO: When generating generics for a class if we have the following:
tphClass < tphMeth1 < tphMeth2
then we have to convert tphMeth1, tphMeth2 to tph class and generate the following
class constraints: tphClass < tphMeth1, tphMeth1 < tphMeth2, tphMeth2 < Object
*/
public static GenericsGeneratorResultForClass generateConstraints(final String className, final TPHExtractor tphExtractor,
final List<String> tphsClass, final ConstraintsSimplierResult simplifiedConstraints) {
@ -83,6 +83,9 @@ public class GenericsGenerator {
}
private static void createConstraintsForClassTphs(ConstraintsSimplierResult simplifiedConstraints, List<String> tphsClass, List<GenericsGeneratorResult> constraints, List<TPHConstraint> allCons, Set<String> visitedTPHs, List<String> methodTPHs) {
List<String> classAndMethodTphs = new ArrayList<>(tphsClass);
classAndMethodTphs.addAll(methodTPHs);
for (String tph : tphsClass) {
if (visitedTPHs.contains(tph))
@ -90,13 +93,46 @@ public class GenericsGenerator {
final LinkedList<String> tphsInRel = GenericsGeneratorUtility
.createLinkedListForTPHsInRelationClass(allCons, tphsClass, methodTPHs, visitedTPHs, tph);
if (!tphsInRel.isEmpty()) {
GenericsGeneratorResult constraint = generateGGResultForClass(tphsInRel, simplifiedConstraints, tphsClass);
constraints.add(constraint);
}
generateConstraintsForClassFromList(tphsInRel,simplifiedConstraints,classAndMethodTphs,constraints);
}
}
private static void generateConstraintsForClassFromList(LinkedList<String> tphsInRel, ConstraintsSimplierResult simplifiedConstraints, List<String> classAndMethodTphs, List<GenericsGeneratorResult> constraints) {
if(tphsInRel.isEmpty())
return;
List<GenericsGeneratorResult> resultConstraints = new ArrayList<>();
String subType = tphsInRel.getFirst();
String superType = GenericsGeneratorUtility.getNextClassTph(classAndMethodTphs, tphsInRel);
int idxOfSuper = tphsInRel.indexOf(superType);
int size = tphsInRel.size();
while (size > 2 && idxOfSuper < size-1){
GenericsGeneratorResult genericsGeneratorResult = getGenericsGeneratorResultForClass(simplifiedConstraints, subType, superType);
constraints.add(genericsGeneratorResult);
tphsInRel = new LinkedList<>(tphsInRel.subList(idxOfSuper, size));
subType = tphsInRel.getFirst();
superType = GenericsGeneratorUtility.getNextClassTph(classAndMethodTphs, tphsInRel);
idxOfSuper = tphsInRel.indexOf(superType);
size = tphsInRel.size();
}
GenericsGeneratorResult genericsGeneratorResult = getGenericsGeneratorResultForClass(simplifiedConstraints, subType, superType);
constraints.add(genericsGeneratorResult);
}
private static GenericsGeneratorResult getGenericsGeneratorResultForClass(ConstraintsSimplierResult simplifiedConstraints, String subType, String superType) {
TPHConstraint constraint = new ExtendsConstraint(subType, superType);
Set<String> equalSet = GenericsGeneratorUtility
.createEqualSet(simplifiedConstraints.getNameReplacementResults(), subType);
return new GenericsGeneratorResult(constraint, equalSet);
}
/* TODO Remove this method*/
private static GenericsGeneratorResult generateGGResultForClass(LinkedList<String> tphsInRel,
ConstraintsSimplierResult simplifiedConstraints, List<String> tphsClass) {
String subType = tphsInRel.getFirst();

View File

@ -12,6 +12,7 @@ import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.*;
import org.objectweb.asm.Type;
import de.dhbwstuttgart.bytecode.TPHExtractor;
@ -19,7 +20,6 @@ import de.dhbwstuttgart.bytecode.constraint.EqualConstraint;
import de.dhbwstuttgart.bytecode.constraint.ExtendsConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult;
import de.dhbwstuttgart.bytecode.utilities.ConstraintsFinder;
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
import de.dhbwstuttgart.bytecode.utilities.NameReplacer;
@ -74,21 +74,6 @@ public class GenericsGeneratorUtility {
}
public static void modifyRelation(List<SimpleCycle> simpleCycles) {
simpleCycles.forEach(sc->{
sc.getConstraint().setRel(Relation.EQUAL);
sc.getReverseConstraint().setRel(Relation.EQUAL);
});
}
public static void removeAllReverseConstraints(List<TPHConstraint> allCons, List<SimpleCycle> simpleCycles) {
simpleCycles.forEach(sc->allCons.remove(sc.getReverseConstraint()));
}
public static void substituteTPH(List<TPHConstraint> allCons, List<SimpleCycle> simpleCycles) {
simpleCycles.forEach(sc->substituteTPH(allCons, sc.getConstraint().getLeft(), sc.getConstraint().getRight()));
}
public static List<ConstraintsWithSameLeftSide> findConstraintsWithLeftSameLeftSide(List<TPHConstraint> allCons) {
// finder looks for constraints that have the same left hand side
// and put them in a list
@ -279,13 +264,13 @@ public class GenericsGeneratorUtility {
}
public static void modifyRelationForConstraintsinLongCycle(List<LongCycle> longCycles) {
longCycles.stream().map(lc->lc.getConstraints()).flatMap(e->e.stream()).forEach(c->c.setRel(Relation.EQUAL));
public static void modifyRelationForConstraintsInCycle(List<Cycle> cycles) {
cycles.stream().map(lc->lc.getConstraints()).flatMap(e->e.stream()).forEach(c->c.setRel(Relation.EQUAL));
}
public static List<NameReplacementResult> substituteTPHSFormLongCycle(List<TPHConstraint> allCons, List<LongCycle> longCycles, TPHExtractor tphExtractor) {
public static List<NameReplacementResult> substituteTPHSFormCycle(List<TPHConstraint> allCons, List<Cycle> cycles, TPHExtractor tphExtractor) {
List<NameReplacementResult> results = new ArrayList<>();
longCycles.forEach(lc->{
cycles.forEach(lc->{
Set<String> names = getNamesFromCycle(lc);
String newName = names.stream().findFirst().get();
@ -328,7 +313,7 @@ public class GenericsGeneratorUtility {
});
}
public static Set<String> getNamesFromCycle(LongCycle lc) {
public static Set<String> getNamesFromCycle(Cycle lc) {
Set<String> names = new HashSet<>();
lc.getConstraints().forEach(c->names.add(c.getLeft()));
return names;
@ -388,23 +373,6 @@ public class GenericsGeneratorUtility {
return allCons.stream().filter(c->c.getRight().equals(tph)).findFirst();
}
public static void replaceOldNames(List<SimpleCycle> simpleCycles, TPHExtractor tphExtractor) {
simpleCycles.forEach(sc -> {
Stream<ArrayList<String>> tphsOfMethods = tphExtractor.ListOfMethodsAndTph.stream().map(m->m.getTphs());
Stream<ArrayList<String>> localTphsOfMethods = tphExtractor.ListOfMethodsAndTph.stream().map(m->m.getLocalTphs());
String newName = sc.getConstraint().getRight();
String oldName = sc.getConstraint().getLeft();
List<String> names = new ArrayList<>();
names.add(oldName);
names.add(newName);
replaceOldNames(newName, names, tphsOfMethods);
replaceOldNames(newName, names, localTphsOfMethods);
});
}
public static boolean isTPHInGenericGeneratorResult(final List<GenericsGeneratorResult> constraints, final String tph) {
return constraints.stream().filter(g->g.getConstraint().getLeft().equals(tph)).findFirst().isPresent();
}
@ -576,6 +544,7 @@ public class GenericsGeneratorUtility {
public static LinkedList<String> createLinkedListForTPHsInRelationClass(List<TPHConstraint> allCons,
List<String> tphsClass, List<String> methodTPHs, Set<String> visitedTPHs, String tph) {
final LinkedList<String> 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);

View File

@ -1,69 +0,0 @@
/**
*
*/
package de.dhbwstuttgart.bytecode.genericsGenerator;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
/**
* @author fayez
*
*/
public class SimpleCycle {
private TPHConstraint constraint;
private TPHConstraint reverseConstraint;
/**
* @param constraint
* @param reverseConstraint
*/
public SimpleCycle(TPHConstraint constraint, TPHConstraint reverseConstraint) {
this.constraint = constraint;
this.reverseConstraint = reverseConstraint;
}
/**
* @return the constraint
*/
public TPHConstraint getConstraint() {
return constraint;
}
/**
* @param constraint the constraint to set
*/
public void setConstraint(TPHConstraint constraint) {
this.constraint = constraint;
}
/**
* @return the reverseConstraint
*/
public TPHConstraint getReverseConstraint() {
return reverseConstraint;
}
/**
* @param reverseConstraint the reverseConstraint to set
*/
public void setReverseConstraint(TPHConstraint reverseConstraint) {
this.reverseConstraint = reverseConstraint;
}
public boolean contains(TPHConstraint c) {
if (constraint.getLeft().equals(c.getLeft()) && constraint.getRight().equals(c.getRight())
&& constraint.getRel().equals(c.getRel()))
return true;
if (reverseConstraint.getLeft().equals(c.getLeft()) && reverseConstraint.getRight().equals(c.getRight())
&& reverseConstraint.getRel().equals(c.getRel()))
return true;
return false;
}
@Override
public String toString() {
return constraint.toString() + " -> " + reverseConstraint.toString();
}
}

View File

@ -1,4 +1,4 @@
package de.dhbwstuttgart.bytecode.genericsGenerator;
package de.dhbwstuttgart.bytecode.genericsGeneratorTypes;
import java.util.List;

View File

@ -1,7 +1,7 @@
/**
*
*/
package de.dhbwstuttgart.bytecode.genericsGenerator;
package de.dhbwstuttgart.bytecode.genericsGeneratorTypes;
import java.util.List;
@ -11,13 +11,13 @@ import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
* @author fayez
*
*/
public class LongCycle {
public class Cycle {
private final List<TPHConstraint> constraints;
/**
* @param constraints
*/
public LongCycle(List<TPHConstraint> constraints) {
public Cycle(List<TPHConstraint> constraints) {
this.constraints = constraints;
}

View File

@ -1,13 +1,11 @@
/**
*
*/
package de.dhbwstuttgart.bytecode.simplifyRes;
package de.dhbwstuttgart.bytecode.genericsGeneratorTypes;
import java.util.Collections;
import java.util.List;
import com.google.common.collect.Collections2;
/**
* @author fayez
*

View File

@ -0,0 +1,51 @@
/**
*
*/
package de.dhbwstuttgart.bytecode.genericsGeneratorTypes;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
/**
* The simplify results of a source file (package)
*
* @author fayez
*
*/
public class GenericGenratorResultForSourceFile {
private String pkgName;
private final List<GenericsGeneratorResultForClass> genericGeneratorResultForAllClasses = new ArrayList<>();
/**
* @param pkgName
*/
public GenericGenratorResultForSourceFile(String pkgName) {
this.pkgName = pkgName;
}
public List<GenericsGeneratorResultForClass> getGenericGeneratorResultForAllClasses() {
return genericGeneratorResultForAllClasses;
}
/**
* Appends the simplify results of a class to simplifyResForSF
*
* @param sResClass simplify results of a class to added
*/
public void addGenericGeneratorResultClass(GenericsGeneratorResultForClass sResClass) {
genericGeneratorResultForAllClasses.add(sResClass);
}
public GenericsGeneratorResultForClass getSimplifyResultsByName(String pkgName, String name) {
if (this.pkgName.equals(pkgName)) {
return genericGeneratorResultForAllClasses.stream()
.filter(sr -> sr.getClassName().equals(name))
.findAny()
.orElseThrow(() -> new NoSuchElementException(
"Simplify results for the class " + pkgName + "." + name + " are not found"));
}
throw new NoSuchElementException("Simplify results for the class " + pkgName + "." + name + " are not found");
}
}

View File

@ -1,7 +1,7 @@
/**
*
*/
package de.dhbwstuttgart.bytecode.simplifyRes;
package de.dhbwstuttgart.bytecode.genericsGeneratorTypes;
import java.util.Set;

View File

@ -0,0 +1,67 @@
/**
*
*/
package de.dhbwstuttgart.bytecode.genericsGeneratorTypes;
import java.util.Collections;
import java.util.List;
/**
* @author fayez
*
*/
public class GenericsGeneratorResultForClass {
private final String className;
private final List<GenericsGeneratorResult> classConstraints;
private final GenericGeneratorResultsForAllMethods methodsAndTheirConstraints;
public GenericsGeneratorResultForClass(String className) {
this(className, Collections.emptyList(), new GenericGeneratorResultsForAllMethods());
}
/**
* @param className
* @param classConstraints
* @param methodsAndTheirConstraints
*/
public GenericsGeneratorResultForClass(String className, List<GenericsGeneratorResult> classConstraints,
GenericGeneratorResultsForAllMethods methodsAndTheirConstraints) {
this.className = className;
this.classConstraints = classConstraints;
this.methodsAndTheirConstraints = methodsAndTheirConstraints;
}
/**
* @return the className
*/
public String getClassName() {
return className;
}
/**
* @return the classConstraints
*/
public List<GenericsGeneratorResult> getClassConstraints() {
return classConstraints;
}
/**
* @return the methodsAndTheirConstraints
*/
public GenericGeneratorResultsForAllMethods getMethodsAndTheirConstraints() {
return methodsAndTheirConstraints;
}
public boolean contains(String id) {
return methodsAndTheirConstraints.getMethodsAndConstraints().stream().map(mc -> mc.getMethodID())
.anyMatch(i -> i.equals(id));
}
public List<GenericsGeneratorResult> getMethodConstraintsByID(String id) {
return methodsAndTheirConstraints.getMethodsAndConstraints().stream().filter(mc -> mc.getMethodID().equals(id))
.findFirst().get().getConstraints();
}
}

View File

@ -1,7 +1,7 @@
/**
*
*/
package de.dhbwstuttgart.bytecode.simplifyRes;
package de.dhbwstuttgart.bytecode.genericsGeneratorTypes;
import java.util.List;

View File

@ -1,7 +1,7 @@
/**
*
*/
package de.dhbwstuttgart.bytecode.genericsGenerator;
package de.dhbwstuttgart.bytecode.genericsGeneratorTypes;
import java.util.List;

View File

@ -6,12 +6,9 @@ import org.objectweb.asm.Type;
import org.objectweb.asm.signature.SignatureVisitor;
import org.objectweb.asm.signature.SignatureWriter;
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.bytecode.genericsGeneratorTypes.GenericsGeneratorResult;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.Constructor;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
@ -22,8 +19,6 @@ import de.dhbwstuttgart.syntaxtree.type.RefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.SuperWildcardType;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.syntaxtree.type.WildcardType;
import de.dhbwstuttgart.typeinference.result.ResolvedType;
import de.dhbwstuttgart.typeinference.result.ResultSet;
public class Signature {
@ -75,21 +70,18 @@ public class Signature {
this.classOrInterface = classOrInterface;
this.genericsAndBounds = genericsAndBounds;
this.consClass = consClass;
createSignatureForClassOrInterface();
}
public Signature(HashMap<String, String> genericsAndBoundsMethod,
HashMap<String, String> genericsAndBounds,
HashMap<String, RefTypeOrTPHOrWildcardOrGeneric> methodParamsAndTypes, ResultSet resultSet,
List<GenericsGeneratorResult> constraints,List<GenericsGeneratorResult> consClass) {
//this.method = method;
this.genericsAndBoundsMethod = genericsAndBoundsMethod;
this.genericsAndBounds = genericsAndBounds;
this.methodParamsAndTypes = methodParamsAndTypes;
this.resultSet = resultSet;
this.constraints = constraints;
this.consClass = consClass;
//createSignatureForMethod(this.method);
}
public String createSignatureForConstructor(Constructor constructor) {
@ -145,33 +137,19 @@ public class Signature {
}
private void createSignatureForFunN(int numberOfParams, String to, String[] paramTypes) {
for (int i = 0; i < numberOfParams; i++) {
int j = i + 1;
sw.visitFormalTypeParameter("T" + j);
// getBounds von Params
sw.visitClassBound().visitClassType(paramTypes[i]);
sw.visitClassBound().visitEnd();
}
defineTypeVariablesForParametersOfFunN(numberOfParams);
sw.visitFormalTypeParameter("R");
visitClassBound(to);
// TODO: prüfe ob Return-Type = void,
sw.visitSuperclass().visitClassType(Type.getInternalName(Object.class));
;
sw.visitEnd();
}
private void createSignatureForFunN(int numberOfParams) {
// sw.visitClassBound().visitEnd();
for (int i = 0; i < numberOfParams; i++) {
int j = i + 1;
sw.visitFormalTypeParameter("T" + j);
// getBounds von Params
sw.visitClassBound().visitClassType(Type.getInternalName(Object.class));
sw.visitClassBound().visitEnd();
}
defineTypeVariablesForParametersOfFunN(numberOfParams);
sw.visitFormalTypeParameter("R");
// getBounds vom Return-Type
@ -179,171 +157,17 @@ public class Signature {
sw.visitClassBound().visitEnd();
// TODO: prüfe ob Return-Type = void,
sw.visitSuperclass().visitClassType(Type.getInternalName(Object.class));
;
sw.visitEnd();
}
/**
* Creates signature for a method or constructor with @see
* {@link SignatureWriter} Signature looks like: <typevaliables
* (K:Ljava/lang/Object "Bounds")>(params L.. OR T.. Or basistape)ReturnType
*
* @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 <T:...;B:...;...>
while (itr.hasNext()) {
System.out.println("HAS GENERICS!!");
GenericTypeVar g = itr.next();
visitTypeVarsAndTheirBounds(g, genericsAndBoundsMethod);
private void defineTypeVariablesForParametersOfFunN(int numberOfParams) {
for (int i = 0; i < numberOfParams; i++) {
int j = i + 1;
sw.visitFormalTypeParameter("T" + j);
// getBounds von Params
sw.visitClassBound().visitClassType(Type.getInternalName(Object.class));
sw.visitClassBound().visitEnd();
}
// 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) {
String ret = resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToSignature());
ArrayList<TPHConstraint> allConsBeforeSimplify = new ArrayList<>();
Map<TPHConstraint, Set<String>> allConstraints = new HashMap<>();
if (ret.contains("<")) {
allConsBeforeSimplify = getAllConstraints(
(RefType) resultSet.resolveType(method.getReturnType()).resolvedType);
}
for (String paramName : methodParamsAndTypes.keySet()) {
RefTypeOrTPHOrWildcardOrGeneric t = methodParamsAndTypes.get(paramName);
String pT = t.acceptTV(new TypeToSignature());
if (pT.contains("<"))
allConsBeforeSimplify = getAllConstraints((RefType) t);
}
boolean doSimplify = false;
if (!allConsBeforeSimplify.isEmpty()) {
addConstraintsToMap(allConstraints, allConsBeforeSimplify);
doSimplify = true;
}
createTypeVars(allConstraints, doSimplify);
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)));
}
}
}
}
visitParams();
if (isConstructor || resultSet.resolveType(method.getReturnType()).resolvedType.acceptTV(new TypeToSignature())
.equals("V")) {
sw.visitReturnType().visitBaseType('V');
} else {
RefTypeOrTPHOrWildcardOrGeneric returnType = method.getReturnType();
// return type deswegen ist false
// doVisitParamsOrReturn(returnType, false);
}
// sw.visitEnd();
}
private void addConstraintsToMap(Map<TPHConstraint, Set<String>> allConstraints,
ArrayList<TPHConstraint> allConsBeforeSimplify) {
for (TPHConstraint tphCons : allConsBeforeSimplify) {
allConstraints.put(tphCons, null);
}
}
private String getEqualTPH(Map<TPHConstraint, Set<String>> methodConstraints2, String tph) {
for (TPHConstraint cons : methodConstraints2.keySet()) {
if (methodConstraints2.get(cons) != null) {
if (methodConstraints2.get(cons).contains(tph)) {
return cons.getLeft();
}
}
}
return tph;
}
private void createTypeVars(Map<TPHConstraint, Set<String>> allConstraints, boolean doSimplify) {
allConstraints.putAll(methodConstraints);
Map<TPHConstraint, Set<String>> simplifiedConstraints;
if (doSimplify) {
simplifiedConstraints = Simplify.simplifyContraints(allConstraints);
} else {
simplifiedConstraints = allConstraints;
}
for (TPHConstraint cons : simplifiedConstraints.keySet()) {
// need subst.
String sub = cons.getLeft() + SPECIAL_CHAR;
if (!genericsAndBoundsMethod.containsKey(sub) && !genericsAndBounds.containsKey(sub)) {
sw.visitFormalTypeParameter(sub);
String bound = cons.getRight();
if (bound.equals(Type.getInternalName(Object.class))) {
sw.visitClassBound().visitClassType(Type.getInternalName(Object.class));
sw.visitClassBound().visitEnd();
genericsAndBoundsMethod.put(sub, bound);
} else {
String boundName = bound + SPECIAL_CHAR;
sw.visitClassBound().visitTypeVariable(boundName);
genericsAndBoundsMethod.put(sub, boundName);
}
}
}
}
private ArrayList<TPHConstraint> getAllConstraints(RefType ref) {
final ArrayList<TPHConstraint> res = new ArrayList<>();
for (RefTypeOrTPHOrWildcardOrGeneric p : ref.getParaList()) {
ResolvedType resType;
if (p instanceof WildcardType) {
resType = resultSet.resolveType(((WildcardType) p).getInnerType());
} else {
resType = resultSet.resolveType(p);
}
RefTypeOrTPHOrWildcardOrGeneric resolved = resType.resolvedType;
if (resolved instanceof TypePlaceholder) {
// resType.getAdditionalGenerics().forEach(ag ->{
resultSet.genIns.forEach(ag -> {
TPHConstraint constr = new ExtendsConstraint(ag.getLeft().getName(), ag.getRight().getName());
if (!contains(res, constr)) {
res.add(constr);
}
});
}
if (resolved instanceof WildcardType) {
WildcardType resWC = (WildcardType) resolved;
ResolvedType resType2 = resultSet.resolveType(resWC.getInnerType());
if (resType2.resolvedType instanceof TypePlaceholder) {
// resType2.getAdditionalGenerics().forEach(ag ->{
resultSet.genIns.forEach(ag -> {
TPHConstraint constr = new ExtendsConstraint(ag.getLeft().getName(), ag.getRight().getName());
if (!contains(res, constr)) {
res.add(constr);
}
});
}
}
}
System.out.println("RES GIP === " + res.size());
return res;
}
private boolean contains(ArrayList<TPHConstraint> constraints, TPHConstraint constr) {
for (int i = 0; i < constraints.size(); ++i) {
TPHConstraint p = constraints.get(i);
if (constr.getLeft().equals(p.getLeft()) && constr.getRight().equals(p.getRight()))
return true;
}
return false;
}
/**
@ -363,8 +187,7 @@ public class Signature {
break;
case "GRT":
GenericRefType g = (GenericRefType) t;
// sv.visitTypeVariable(g.acceptTV(new TypeToSignature()).substring(1));
sv.visitTypeVariable(g.acceptTV(new TypeToSignature()));
sv.visitTypeVariable(g.acceptTV(new TypeToSignature(constraints)));
break;
case "TPH":
RefTypeOrTPHOrWildcardOrGeneric r = resultSet.resolveType(t).resolvedType;
@ -375,13 +198,12 @@ public class Signature {
// das braucht man nicht es reicht: sv.visitTypeVariable(r.acceptTV(new
// TypeToSignature())
//
String sig2 = r.acceptTV(new TypeToSignature());
String sig2 = r.acceptTV(new TypeToSignature(constraints));
if (r instanceof GenericRefType) {
sv.visitTypeVariable(sig2);
} else if (!(r instanceof TypePlaceholder)) {
if (sig2.contains(SPECIAL_CHAR_FOR_FUN)) {
System.out.println(" Signature FUN$$: " + r);
sv.visitInterface().visitClassType(sig2.substring(1, sig2.length()));
sv.visitInterface().visitClassType(sig2.substring(1));
} else {
// Kann zwischen GenericRefType und RefType nicht unterscheiden
// Deswegen wird immer geprüft, ob der Name in Generic Maps liegt
@ -405,7 +227,6 @@ public class Signature {
} else {
toVisit = getEqualTPH(constraints, realName) + SPECIAL_CHAR;
}
System.out.println(r.getClass() + " Signature TPH: " + r.acceptTV(new TypeToSignature()));
}
sv.visitTypeVariable(toVisit);
}
@ -413,41 +234,18 @@ public class Signature {
break;
case "SWC":
System.out.println("SWC---Signature");
SuperWildcardType swc = (SuperWildcardType) t;
String sigInner = swc.getInnerType().acceptTV(new TypeToSignature(constraints));
if (swc.getInnerType() instanceof TypePlaceholder) {
sv.visitTypeArgument(SUPER_CHAR).visitTypeVariable(sigInner.substring(1, sigInner.length()));
} else if (swc.getInnerType() instanceof RefType) {
if (sigInner.contains(SPECIAL_CHAR_FOR_FUN)) {
sv.visitTypeArgument(SUPER_CHAR).visitInterface().visitClassType(sigInner.substring(1, sigInner.length()));
} else {
sv.visitTypeArgument(SUPER_CHAR).visitClassType(sigInner.substring(1, sigInner.length()));
}
} else {
sv.visitTypeArgument(SUPER_CHAR).visitTypeVariable(sigInner.substring(1));
}
int length = sigInner.length();
visitWildCard(sv, sigInner, length, swc.getInnerType(), SUPER_CHAR);
break;
case "EWC":
System.out.println("EWC---Signature");
ExtendsWildcardType ewc = (ExtendsWildcardType) t;
String esigInner = ewc.getInnerType().acceptTV(new TypeToSignature(constraints));
System.out.println(esigInner);
if (ewc.getInnerType() instanceof TypePlaceholder) {
sv.visitTypeArgument(EXTENDS_CHAR).visitTypeVariable(esigInner.substring(1, esigInner.length()));
} else if (ewc.getInnerType() instanceof RefType) {
if (esigInner.contains(SPECIAL_CHAR_FOR_FUN)) {
sv.visitTypeArgument(EXTENDS_CHAR).visitInterface()
.visitClassType(esigInner.substring(1, esigInner.length()));
} else {
// sv.visitClassType(esigInner.substring(1,esigInner.length()));
sv.visitTypeArgument(EXTENDS_CHAR).visitClassType(esigInner.substring(1, esigInner.length()));
}
} else {
sv.visitTypeArgument(EXTENDS_CHAR).visitTypeVariable(esigInner.substring(1));
}
int lengthEWCSig = esigInner.length();
visitWildCard(sv, esigInner, lengthEWCSig, ewc.getInnerType(), EXTENDS_CHAR);
break;
default:
@ -457,6 +255,24 @@ public class Signature {
}
}
private void visitWildCard(SignatureVisitor sv, String sigInner, int length, RefTypeOrTPHOrWildcardOrGeneric innerType, char superOrExtendsChar) {
if (innerType instanceof TypePlaceholder) {
sv.visitTypeArgument(superOrExtendsChar).visitTypeVariable(sigInner.substring(1, length));
} else if (innerType instanceof RefType) {
checkInnerSignatureOfWildCard(sv, sigInner, length, superOrExtendsChar);
} else {
sv.visitTypeArgument(superOrExtendsChar).visitTypeVariable(sigInner.substring(1));
}
}
private void checkInnerSignatureOfWildCard(SignatureVisitor sv, String sigInner, int length, char superOrExtendsChar) {
if (sigInner.contains(SPECIAL_CHAR_FOR_FUN)) {
sv.visitTypeArgument(superOrExtendsChar).visitInterface().visitClassType(sigInner.substring(1, length));
} else {
sv.visitTypeArgument(superOrExtendsChar).visitClassType(sigInner.substring(1, length));
}
}
private Optional<GenericsGeneratorResult> getEqualTPHFromClassConstraints(List<GenericsGeneratorResult> consClass, String tph) {
return consClass.stream()
.filter(c -> c.getConstraint().getLeft().equals(tph) || c.getEqualsTPHs().contains(tph))
@ -469,38 +285,29 @@ public class Signature {
.findFirst().get().getConstraint().getLeft();
}
/**
* @param isParameterType
* @return
*/
private SignatureVisitor getSignatureVisitor(boolean isParameterType) {
SignatureVisitor sv;
if (isParameterType) {
sv = sw.visitParameterType();
} else {
sv = sw.visitReturnType();
}
return sv;
}
/**
* Creates signature for class or interface with {@link SignatureWriter}
* Signature looks like: <typevaliables (K:Ljava/lang/Object
* "Bounds")>superclass
*/
private void createSignatureForClassOrInterface() {
Iterator<GenericTypeVar> itr = classOrInterface.getGenerics().iterator();
while (itr.hasNext()) {
GenericTypeVar g = itr.next();
visitTypeVarsAndTheirBounds(g, genericsAndBounds);
}
public String createSignatureForClassOrInterface() {
defineTypeVariablesForClassOrInterface();
defineGenericsFromConstraints(consClass,genericsAndBounds);
String sClass = classOrInterface.getSuperClass().acceptTV(new TypeToSignature());
sw.visitSuperclass().visitClassType(sClass.substring(1, sClass.length() - 1));
sw.visitEnd();
return sw.toString();
}
private void defineTypeVariablesForClassOrInterface() {
Iterator<GenericTypeVar> itr = classOrInterface.getGenerics().iterator();
while (itr.hasNext()) {
GenericTypeVar g = itr.next();
visitTypeVarsAndTheirBounds(g, genericsAndBounds);
}
}
/**
@ -553,7 +360,6 @@ public class Signature {
while (bItr.hasNext()) {
RefTypeOrTPHOrWildcardOrGeneric b = bItr.next();
String boundDesc = b.acceptTV(new TypeToDescriptor());
// System.out.println("GetBounds: " + boundDesc);
// Ensure that <...> extends java.lang.Object OR ...
if (b instanceof GenericRefType) {
sw.visitClassBound().visitTypeVariable(boundDesc);
@ -562,11 +368,6 @@ public class Signature {
}
genAndBounds.put(g.getName(), boundDesc);
}
// sw.visitClassBound().visitEnd();
}
public String toString() {
return sw.toString();
}
}

View File

@ -5,8 +5,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult;
import de.dhbwstuttgart.exceptions.NotImplementedException;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult;
import de.dhbwstuttgart.syntaxtree.type.ExtendsWildcardType;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefType;

View File

@ -1,64 +0,0 @@
/**
*
*/
package de.dhbwstuttgart.bytecode.simplifyRes;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
/**
* The class SimplifyResult represents the results of the simplify algorithm.
* This class contains all constraints and all type placeholders of a class. The type placeholders
* are represented by their names.
* The simplify results of each method of a class are also contained in this class.
*
* @author fayez
*
*/
public class SimplifyResult {
private List<TPHConstraint> classConstraints;
private List<String> tphsClass;
private final List<SimplifyResultMethod> simplifyResultForMethod = new ArrayList<>();
public SimplifyResult(List<TPHConstraint> classConstraints, List<String> tphsClass) {
this.classConstraints = classConstraints;
this.tphsClass = tphsClass;
}
public SimplifyResult() {
}
public List<TPHConstraint> getClassConstraints() {
return classConstraints;
}
public List<SimplifyResultMethod> getSimplifyResultForMethod() {
return simplifyResultForMethod;
}
public List<String> getTphsClass() {
return tphsClass==null?new ArrayList<>():tphsClass;
}
/**
* @param classConstraints the classConstraints to set
*/
public void setClassConstraints(List<TPHConstraint> classConstraints) {
this.classConstraints = classConstraints;
}
/**
* @param tphsClass the tphsClass to set
*/
public void setTphsClass(List<String> tphsClass) {
this.tphsClass = tphsClass;
}
public Map<TPHConstraint, Set<String>> getMethodConstraintsByID(String id){
return simplifyResultForMethod.stream().filter(sr->sr.getMethodID().equals(id)).findAny().get().getConastraintsAndEqualTPHs();
}
}

View File

@ -1,48 +0,0 @@
/**
*
*/
package de.dhbwstuttgart.bytecode.simplifyRes;
/**
* The simplify results of a class
*
* @author fayez
*
*/
public class SimplifyResultClass {
private final String className;
private SimplifyResult simplifyResults;
/**
* @param className
*/
public SimplifyResultClass(String className) {
this.className = className;
}
/**
* @param className
* @param simplifyResults
*/
public SimplifyResultClass(String className, SimplifyResult simplifyResults) {
this.className = className;
this.simplifyResults = simplifyResults;
}
public String getClassName() {
return className;
}
public SimplifyResult getSimplifyResults() {
return simplifyResults;
}
/**
* @param simplifyResults the simplifyResults to set
*/
public void setSimplifyResults(SimplifyResult simplifyResults) {
this.simplifyResults = simplifyResults;
}
}

View File

@ -1,42 +0,0 @@
package de.dhbwstuttgart.bytecode.simplifyRes;
import java.util.Map;
import java.util.Set;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
/**
* The simplify results of a methed
*
* @author fayez
*
*/
public class SimplifyResultMethod {
private final String methodID;
private final Map<TPHConstraint, Set<String>> conastraintsAndEqualTPHs;
public SimplifyResultMethod(String methodName, Map<TPHConstraint, Set<String>> conastraintsAndEqualTPHs) {
this.methodID = methodName;
this.conastraintsAndEqualTPHs = conastraintsAndEqualTPHs;
}
/**
* @return the methodName
*/
public String getMethodID() {
return methodID;
}
/**
* @return the conastraintsAndEqualTPHs
*/
public Map<TPHConstraint, Set<String>> getConastraintsAndEqualTPHs() {
return conastraintsAndEqualTPHs;
}
}

View File

@ -1,81 +0,0 @@
/**
*
*/
package de.dhbwstuttgart.bytecode.simplifyRes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.objectweb.asm.Type;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.type.GenericRefType;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
/**
* @author fayez
*
*/
public class TypeVariableFinder {
private ClassOrInterface classOrInterface;
private Map<String, String> genericsAndBounds;
private List<TPHConstraint> consClass;
public TypeVariableFinder(ClassOrInterface classOrInterface, Map<String, String> genericsAndBounds,
List<TPHConstraint> consClass) {
this.classOrInterface = classOrInterface;
this.genericsAndBounds = genericsAndBounds;
this.consClass = consClass;
}
public void findTV() {
Iterator<GenericTypeVar> itr = classOrInterface.getGenerics().iterator();
while(itr.hasNext()) {
GenericTypeVar g = itr.next();
getBoundsOfTypeVar(g,genericsAndBounds);
}
if(!consClass.isEmpty()) {
ArrayList<String> types = new ArrayList<>();
ArrayList<String> superTypes = new ArrayList<>();
for(TPHConstraint cons : consClass) {
types.add(cons.getLeft());
superTypes.add(cons.getRight());
}
for(TPHConstraint cons : consClass) {
String t = cons.getLeft()+"$";
String bound = cons.getRight();
if(!bound.equals(Type.getInternalName(Object.class)))
bound += "$";
genericsAndBounds.put(t, bound);
}
}
}
/**
* Get bounds of type variable
* @param g type variable
* @param genAndBounds
*/
private void getBoundsOfTypeVar(GenericTypeVar g, Map<String, String> genAndBounds) {
Iterator<? extends RefTypeOrTPHOrWildcardOrGeneric> bItr = g.getBounds().iterator();
while(bItr.hasNext()) {
RefTypeOrTPHOrWildcardOrGeneric b =bItr.next();
String boundDesc = b.acceptTV(new TypeToDescriptor());
// Ensure that <...> extends java.lang.Object OR ...
genAndBounds.put(g.getName(), boundDesc);
}
}
}

View File

@ -5,7 +5,7 @@ import java.util.List;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
import de.dhbwstuttgart.bytecode.genericsGenerator.ConstraintsWithSameLeftSide;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.ConstraintsWithSameLeftSide;
public class ConstraintsFinder {
private List<TPHConstraint> allConstaints;

View File

@ -7,7 +7,7 @@ import java.util.Map;
import java.util.stream.Stream;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.genericsGenerator.NameReplacementResult;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.NameReplacementResult;
import de.dhbwstuttgart.syntaxtree.factory.NameGenerator;
public class NameReplacer {

File diff suppressed because it is too large Load Diff

View File

@ -3,23 +3,18 @@ package de.dhbwstuttgart.core;
import de.dhbwstuttgart.bytecode.BytecodeGen;
import de.dhbwstuttgart.bytecode.Exception.BytecodeGeneratorError;
import de.dhbwstuttgart.bytecode.simplifyRes.SimplifyResult;
import de.dhbwstuttgart.bytecode.simplifyRes.GeneratedGenericsFinder;
import de.dhbwstuttgart.bytecode.simplifyRes.GenericGenratorResultForSourceFile;
import de.dhbwstuttgart.bytecode.genericsGenerator.GeneratedGenericsFinder;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile;
import de.dhbwstuttgart.environment.CompilationEnvironment;
import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.scope.GenericsRegistry;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator;
import de.dhbwstuttgart.parser.antlr.Java8Parser.CompilationUnitContext;
import de.dhbwstuttgart.parser.scope.JavaClassName;
import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.ParameterList;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.factory.ASTFactory;
import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.syntaxtree.visual.ASTTypePrinter;
import de.dhbwstuttgart.typeinference.constraints.Constraint;
@ -31,7 +26,6 @@ import de.dhbwstuttgart.typeinference.unify.RuleSet;
import de.dhbwstuttgart.typeinference.unify.TypeUnify;
import de.dhbwstuttgart.typeinference.unify.distributeVariance;
import de.dhbwstuttgart.typeinference.unify.interfaces.IFiniteClosure;
import de.dhbwstuttgart.typeinference.unify.model.FiniteClosure;
import de.dhbwstuttgart.typeinference.unify.model.PairOperator;
import de.dhbwstuttgart.typeinference.unify.model.PlaceholderType;
import de.dhbwstuttgart.typeinference.unify.model.UnifyPair;
@ -46,15 +40,10 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.antlr.v4.parse.ANTLRParser.throwsSpec_return;
import org.apache.commons.io.output.NullOutputStream;
//import org.apache.commons.io.output.NullOutputStream;
public class JavaTXCompiler {
@ -66,13 +55,6 @@ public class JavaTXCompiler {
Boolean log = true; //gibt an ob ein Log-File nach System.getProperty("user.dir")+"src/test/java/logFiles" geschrieben werden soll?
public volatile UnifyTaskModel usedTasks = new UnifyTaskModel();
/**
* Äußerste Liste der Source-Files.
* Danach Liste der Klassen in Source File.
* Danach Map Klassenname
*/
private List<List<HashMap<String, SimplifyResult>>> simplifyResultsSF = new ArrayList<>();
public JavaTXCompiler(File sourceFile) throws IOException, ClassNotFoundException {
this(Arrays.asList(sourceFile));
INSTANCE = this;

View File

@ -1,37 +1,20 @@
package de.dhbwstuttgart.typedeployment;
import de.dhbwstuttgart.bytecode.Exception.BytecodeGeneratorError;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.descriptor.TypeToDescriptor;
import de.dhbwstuttgart.bytecode.simplifyRes.GenericGenratorResultForSourceFile;
import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResult;
import de.dhbwstuttgart.bytecode.simplifyRes.GenericsGeneratorResultForClass;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResult;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericsGeneratorResultForClass;
import de.dhbwstuttgart.bytecode.utilities.MethodUtility;
import de.dhbwstuttgart.bytecode.utilities.Resolver;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.NewArray;
import de.dhbwstuttgart.syntaxtree.type.*;
import de.dhbwstuttgart.typeinference.result.*;
import org.antlr.v4.parse.ANTLRParser.action_return;
import org.antlr.v4.parse.ANTLRParser.block_return;
import org.antlr.v4.parse.ANTLRParser.labeledAlt_return;
import org.antlr.v4.parse.ANTLRParser.parserRule_return;
import org.antlr.v4.runtime.Token;
import org.objectweb.asm.Type;
import org.stringtemplate.v4.compiler.STParser.ifstat_return;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import javax.print.DocFlavor.STRING;
import javax.security.auth.kerberos.KerberosKey;
/**
* TODO:

View File

@ -40,7 +40,7 @@ public class FieldTphConsMethTest {
Field a = classToTest.getDeclaredField("a");
a.setAccessible(true);
Method m = classToTest.getDeclaredMethod("m", Object.class);
Method m = classToTest.getDeclaredMethod("id", Object.class);
Object result = m.invoke(instanceOfClass, 42);
assertEquals(42,result);

View File

@ -12,7 +12,7 @@ import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import de.dhbwstuttgart.bytecode.simplifyRes.GenericGenratorResultForSourceFile;
import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.typeinference.result.ResultSet;

View File

@ -0,0 +1,38 @@
package bytecode;
import static org.junit.Assert.*;
import java.io.File;
import java.lang.reflect.Field;
import java.net.URL;
import java.net.URLClassLoader;
import org.junit.BeforeClass;
import org.junit.Test;
import de.dhbwstuttgart.core.JavaTXCompiler;
public class mathStrucTest {
private static String path;
private static File fileToTest;
private static JavaTXCompiler compiler;
private static ClassLoader loader;
private static Class<?> classToTest;
private static String pathToClassFile;
private static Object instanceOfClass;
@Test
public void test() throws Exception {
path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/mathStruc.jav";
fileToTest = new File(path);
compiler = new JavaTXCompiler(fileToTest);
compiler.generateBytecode(System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/");
pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/";
loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
classToTest = loader.loadClass("mathStruc");
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
}
}

View File

@ -3,9 +3,9 @@ import java.lang.Integer;
//import java.lang.Short;
public class Faculty {
public fact;
Faculty() {
fact = (x) -> {
//public fact;
//Faculty() {
public fact = (x) -> {
if (x == 1) {
return 1;
}
@ -13,7 +13,7 @@ public class Faculty {
return x * (fact.apply(x-1));
}
};
}
public getFact(x) {
@ -48,4 +48,4 @@ public class Faculty {
// return x * m(x-1);
// }
// }
}
//}

View File

@ -1,11 +1,26 @@
public class FieldTphConsMeth {
a;
public FieldTphConsMeth(c) {
a = m(c);
}
/*public FieldTphConsMeth(c) {
a = id(c);
}*/
m(b) {
id(b) {
return b;
}
setA(x) {
a = x;
return a;
}
m(x,y) {
x = id(y);
}
m2(x,y) {
x = setA(y);
return x;
}
}

View File

@ -1,6 +1,13 @@
public class Tph2 {
id = x->x;
id3 (x) {
return id.apply(x);
}
m(a,b){
var c = m2(a,b);
//m2(a,b);
return a;
}

View File

@ -7,7 +7,8 @@ public class Tph3 {
// m2(a,b){
// return m(a,b);
// }
m1(x, y) { m2(x); x = y; }
m1(x, y) { m2(x); x = y;
}
m2(y) { m1(y, y); }
}

View File

@ -2,11 +2,11 @@ public class Tph4{
m(a,b){
var c = m2(b);
var d = m2(c);
return a;
return d;
}
m2(b){
return b
return b;
}
}

View File

@ -0,0 +1,11 @@
class mathStruc {
model;
//Fun1*<Fun2*<A,A,A>, Fun1*<MathStruc <A>,MathStruc <A>>>
innerOp = (o) -> (ms) -> new mathStruc<>(o.apply(model,ms.model));
mathStruc(m) {
model =m;
//innerOp = (o) -> (ms) -> new mathStruc<>(o.apply(this.model,ms.model));
}
}