From f7101da621e54c5b446d8806d11e486d53f01d2d Mon Sep 17 00:00:00 2001 From: AluAli Date: Fri, 4 Dec 2020 13:17:31 +0100 Subject: [PATCH] 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 --- .../FamilyOfGeneratedGenerics.java | 17 +++++--- .../insertGenerics/PairTphMethod.java | 43 +++++++++++++++++++ .../insertGenerics/PositionFinder.java | 21 +++++---- .../FamilyOfGeneratedGenericsTest.java | 18 +++++--- 4 files changed, 77 insertions(+), 22 deletions(-) create mode 100644 src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/PairTphMethod.java diff --git a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java index f2d067df..e661b510 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/FamilyOfGeneratedGenerics.java @@ -3,6 +3,7 @@ package de.dhbwstuttgart.bytecode.insertGenerics; import de.dhbwstuttgart.bytecode.TPHExtractor; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint.Relation; +import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH; import java.util.ArrayList; import java.util.HashMap; @@ -10,11 +11,11 @@ import java.util.List; public class FamilyOfGeneratedGenerics { public List allConstraints = new ArrayList<>(); - public HashMap posOfTPHs = new HashMap<>(); + public HashMap> posOfTPHs = new HashMap<>(); public FamilyOfGeneratedGenerics(TPHExtractor tphExtractor) { this.allConstraints = tphExtractor.allCons; - this.posOfTPHs = positionConverter(tphExtractor.allTPHS); + this.posOfTPHs = positionConverter(tphExtractor.allTPHS, tphExtractor.ListOfMethodsAndTph); } public static List getClassConstraints(List cs, HashMap posOfTphs) { //Inputparameter List constraintsSet weg @@ -232,13 +233,17 @@ public class FamilyOfGeneratedGenerics { } - public static HashMap positionConverter(HashMap allTphs) { - HashMap convertedPositions = new HashMap<>(); + public static HashMap> positionConverter(HashMap allTphs, List listOfMethodsAndTphs) { + HashMap> convertedPositions = new HashMap<>(); for(String tph: allTphs.keySet()) { 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 - convertedPositions.put(tph, PositionFinder.Position.FIELD); + convertedPositions.put(tph, new PairTphMethod<>(PositionFinder.Position.FIELD, null)); } } return convertedPositions; diff --git a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/PairTphMethod.java b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/PairTphMethod.java new file mode 100644 index 00000000..75313ba9 --- /dev/null +++ b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/PairTphMethod.java @@ -0,0 +1,43 @@ +package de.dhbwstuttgart.bytecode.insertGenerics; + +import java.util.Objects; + +/** A generic class for pairs. + * + *

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. + */ + + +public class PairTphMethod { + 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 PairTphMethod of(A a, B b) { + return new PairTphMethod<>(a,b); + } +} diff --git a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/PositionFinder.java b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/PositionFinder.java index c5638360..9dee27b9 100644 --- a/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/PositionFinder.java +++ b/src/main/java/de/dhbwstuttgart/bytecode/insertGenerics/PositionFinder.java @@ -7,15 +7,16 @@ import java.util.HashMap; import java.util.Set; public class PositionFinder{ - static HashMap posOfTphs = new HashMap(); + static HashMap> posOfTphs = new HashMap>(); + static PairTphMethod whichMethod; // gibt an, in welcher Methode sich TPH befindet (Position.METHOD, id_of_method) public enum Position{ METHOD, CONSTRUCTOR, FIELD } - public static HashMap getPositionOfTPH(SourceFile sf, Set tphs) { + public static HashMap> getPositionOfTPH(SourceFile sf, Set tphs) { new Walker().visit(sf); for (String tph: posOfTphs.keySet()) { @@ -24,16 +25,16 @@ public class PositionFinder{ return null; } - public static void putPositionInMethod(String tph) { - posOfTphs.put(tph, Position.METHOD); + public static void putPositionInMethod(String tph, String methodId) { + posOfTphs.put(tph, new PairTphMethod<>(Position.METHOD, methodId)); } 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) { - posOfTphs.put(tph, Position.CONSTRUCTOR); + public static void putPositionInConstructor(String tph, String id) { + posOfTphs.put(tph, new PairTphMethod<>(Position.CONSTRUCTOR, id)); } @@ -46,9 +47,11 @@ public class PositionFinder{ public void visit(TypePlaceholder tph) { if (inMethod) { if (inConstructor) { - putPositionInConstructor(tph.getName()); +// System.out.println(tph); +// putPositionInConstructor(tph.getName(),); } - putPositionInMethod(tph.getName()); +// System.out.println(tph); +// putPositionInMethod(tph.getName(),); } else { putPositionInField(tph.getName()); } diff --git a/src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java b/src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java index b3157f53..ce8222b5 100644 --- a/src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java +++ b/src/test/java/insertGenerics/FamilyOfGeneratedGenericsTest.java @@ -1,10 +1,8 @@ package insertGenerics; import de.dhbwstuttgart.bytecode.constraint.TPHConstraint; -import de.dhbwstuttgart.bytecode.insertGenerics.ClassConstraint; -import de.dhbwstuttgart.bytecode.insertGenerics.FamilyOfGeneratedGenerics; -import de.dhbwstuttgart.bytecode.insertGenerics.MethodConstraint; -import de.dhbwstuttgart.bytecode.insertGenerics.PositionFinder; +import de.dhbwstuttgart.bytecode.insertGenerics.*; +import de.dhbwstuttgart.bytecode.utilities.MethodAndTPH; import junit.framework.TestCase; import java.util.ArrayList; @@ -154,13 +152,19 @@ public class FamilyOfGeneratedGenericsTest extends TestCase { public void testPositionConverter() { HashMap allTphsOld = new HashMap<>(); + List listOfMethodsAndTphs = new ArrayList<>(); allTphsOld.put("A", true); allTphsOld.put("B", false); - HashMap allTphsNew = FamilyOfGeneratedGenerics.positionConverter(allTphsOld); + listOfMethodsAndTphs.add(new MethodAndTPH("bla")); + listOfMethodsAndTphs.add(new MethodAndTPH("blubb")); + + + + HashMap> allTphsNew = FamilyOfGeneratedGenerics.positionConverter(allTphsOld, listOfMethodsAndTphs); System.out.println(allTphsNew); - assertTrue(allTphsNew.get("A").equals(PositionFinder.Position.METHOD)); - assertTrue(allTphsNew.get("B").equals(PositionFinder.Position.FIELD)); + assertTrue(allTphsNew.get("A").fst.equals(PositionFinder.Position.METHOD)); + assertTrue(allTphsNew.get("B").fst.equals(PositionFinder.Position.FIELD)); }