modified: src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java

new file:   src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/PairTphMethod.java
	modified:   src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/PositionFinder.java
	modified:   src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java
This commit is contained in:
AluAli 2020-12-04 13:17:31 +01:00
parent c0c24eed3b
commit f7101da621
4 changed files with 77 additions and 22 deletions

View File

@ -3,6 +3,7 @@ package de.dhbwstuttgart.bytecode.insertGenerics;
import de.dhbwstuttgart.bytecode.TPHExtractor; import de.dhbwstuttgart.bytecode.TPHExtractor;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation;
import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -10,11 +11,11 @@ import java.util.List;
public class FamilyOfGeneratedGenerics { public class FamilyOfGeneratedGenerics {
public List<TPHConstraint> allConstraints = new ArrayList<>(); public List<TPHConstraint> allConstraints = new ArrayList<>();
public HashMap<String, PositionFinder.Position> posOfTPHs = new HashMap<>(); public HashMap<String, PairTphMethod<PositionFinder.Position, String>> posOfTPHs = new HashMap<>();
public FamilyOfGeneratedGenerics(TPHExtractor tphExtractor) { public FamilyOfGeneratedGenerics(TPHExtractor tphExtractor) {
this.allConstraints = tphExtractor.allCons; this.allConstraints = tphExtractor.allCons;
this.posOfTPHs = positionConverter(tphExtractor.allTPHS); this.posOfTPHs = positionConverter(tphExtractor.allTPHS, tphExtractor.ListOfMethodsAndTph);
} }
public static List<ClassConstraint> getClassConstraints(List<TPHConstraint> cs, HashMap<String, PositionFinder.Position> posOfTphs) { //Inputparameter List<TPHConstraint> constraintsSet weg public static List<ClassConstraint> getClassConstraints(List<TPHConstraint> cs, HashMap<String, PositionFinder.Position> posOfTphs) { //Inputparameter List<TPHConstraint> constraintsSet weg
@ -232,13 +233,17 @@ public class FamilyOfGeneratedGenerics {
} }
public static HashMap<String, PositionFinder.Position> positionConverter(HashMap<String, Boolean> allTphs) { public static HashMap<String, PairTphMethod<PositionFinder.Position, String>> positionConverter(HashMap<String, Boolean> allTphs, List<MethodAndTPH> listOfMethodsAndTphs) {
HashMap<String, PositionFinder.Position> convertedPositions = new HashMap<>(); HashMap<String, PairTphMethod<PositionFinder.Position, String>> convertedPositions = new HashMap<>();
for(String tph: allTphs.keySet()) { for(String tph: allTphs.keySet()) {
if(allTphs.get(tph)) { //if true, then tph is a method-TPH if(allTphs.get(tph)) { //if true, then tph is a method-TPH
convertedPositions.put(tph, PositionFinder.Position.METHOD); for(MethodAndTPH methTph: listOfMethodsAndTphs) {
if (methTph.getTphs().contains(tph)) {
convertedPositions.put(tph, new PairTphMethod<>(PositionFinder.Position.METHOD, methTph.getId()));
}
}
} else { // else it is in the class-TPH } else { // else it is in the class-TPH
convertedPositions.put(tph, PositionFinder.Position.FIELD); convertedPositions.put(tph, new PairTphMethod<>(PositionFinder.Position.FIELD, null));
} }
} }
return convertedPositions; return convertedPositions;

View File

@ -0,0 +1,43 @@
package de.dhbwstuttgart.bytecode.insertGenerics;
import java.util.Objects;
/** A generic class for pairs.
*
* <p><b>This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or
* deletion without notice.</b>
*/
public class PairTphMethod<A, B> {
public final A fst;
public final B snd;
public PairTphMethod(A fst, B snd) {
this.fst = fst;
this.snd = snd;
}
public String toString() {
return "PairTphMethod[" + fst + "," + snd + "]";
}
public boolean equals(Object other) {
return
other instanceof PairTphMethod<?,?> &&
Objects.equals(fst, ((PairTphMethod<?,?>)other).fst) &&
Objects.equals(snd, ((PairTphMethod<?,?>)other).snd);
}
public int hashCode() {
if (fst == null) return (snd == null) ? 0 : snd.hashCode() + 1;
else if (snd == null) return fst.hashCode() + 2;
else return fst.hashCode() * 17 + snd.hashCode();
}
public static <A,B> PairTphMethod<A,B> of(A a, B b) {
return new PairTphMethod<>(a,b);
}
}

View File

@ -7,15 +7,16 @@ import java.util.HashMap;
import java.util.Set; import java.util.Set;
public class PositionFinder{ public class PositionFinder{
static HashMap<String, Position> posOfTphs = new HashMap<String, Position>(); static HashMap<String, PairTphMethod<Position, String>> posOfTphs = new HashMap<String, PairTphMethod<Position, String>>();
static PairTphMethod<Position, String> whichMethod; // gibt an, in welcher Methode sich TPH befindet (Position.METHOD, id_of_method)
public enum Position{ public enum Position{
METHOD, METHOD,
CONSTRUCTOR, CONSTRUCTOR,
FIELD FIELD
} }
public static HashMap<String, Position> getPositionOfTPH(SourceFile sf, Set<String> tphs) { public static HashMap<String, PairTphMethod<Position, String>> getPositionOfTPH(SourceFile sf, Set<String> tphs) {
new Walker().visit(sf); new Walker().visit(sf);
for (String tph: posOfTphs.keySet()) { for (String tph: posOfTphs.keySet()) {
@ -24,16 +25,16 @@ public class PositionFinder{
return null; return null;
} }
public static void putPositionInMethod(String tph) { public static void putPositionInMethod(String tph, String methodId) {
posOfTphs.put(tph, Position.METHOD); posOfTphs.put(tph, new PairTphMethod<>(Position.METHOD, methodId));
} }
public static void putPositionInField(String tph) { public static void putPositionInField(String tph) {
posOfTphs.put(tph, Position.FIELD); posOfTphs.put(tph, new PairTphMethod<>(Position.FIELD, null));
} }
public static void putPositionInConstructor(String tph) { public static void putPositionInConstructor(String tph, String id) {
posOfTphs.put(tph, Position.CONSTRUCTOR); posOfTphs.put(tph, new PairTphMethod<>(Position.CONSTRUCTOR, id));
} }
@ -46,9 +47,11 @@ public class PositionFinder{
public void visit(TypePlaceholder tph) { public void visit(TypePlaceholder tph) {
if (inMethod) { if (inMethod) {
if (inConstructor) { if (inConstructor) {
putPositionInConstructor(tph.getName()); // System.out.println(tph);
// putPositionInConstructor(tph.getName(),);
} }
putPositionInMethod(tph.getName()); // System.out.println(tph);
// putPositionInMethod(tph.getName(),);
} else { } else {
putPositionInField(tph.getName()); putPositionInField(tph.getName());
} }

View File

@ -1,10 +1,8 @@
package insertGenerics; package insertGenerics;
import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint;
import de.dhbwstuttgart.bytecode.insertGenerics.ClassConstraint; import de.dhbwstuttgart.bytecode.insertGenerics.*;
import de.dhbwstuttgart.bytecode.insertGenerics.FamilyOfGeneratedGenerics; import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH;
import de.dhbwstuttgart.bytecode.insertGenerics.MethodConstraint;
import de.dhbwstuttgart.bytecode.insertGenerics.PositionFinder;
import junit.framework.TestCase; import junit.framework.TestCase;
import java.util.ArrayList; import java.util.ArrayList;
@ -154,13 +152,19 @@ public class FamilyOfGeneratedGenericsTest extends TestCase {
public void testPositionConverter() { public void testPositionConverter() {
HashMap<String, Boolean> allTphsOld = new HashMap<>(); HashMap<String, Boolean> allTphsOld = new HashMap<>();
List<MethodAndTPH> listOfMethodsAndTphs = new ArrayList<>();
allTphsOld.put("A", true); allTphsOld.put("A", true);
allTphsOld.put("B", false); allTphsOld.put("B", false);
HashMap<String, PositionFinder.Position> allTphsNew = FamilyOfGeneratedGenerics.positionConverter(allTphsOld); listOfMethodsAndTphs.add(new MethodAndTPH("bla"));
listOfMethodsAndTphs.add(new MethodAndTPH("blubb"));
HashMap<String, PairTphMethod<PositionFinder.Position, String>> allTphsNew = FamilyOfGeneratedGenerics.positionConverter(allTphsOld, listOfMethodsAndTphs);
System.out.println(allTphsNew); System.out.println(allTphsNew);
assertTrue(allTphsNew.get("A").equals(PositionFinder.Position.METHOD)); assertTrue(allTphsNew.get("A").fst.equals(PositionFinder.Position.METHOD));
assertTrue(allTphsNew.get("B").equals(PositionFinder.Position.FIELD)); assertTrue(allTphsNew.get("B").fst.equals(PositionFinder.Position.FIELD));
} }