diff --git a/resources/insertGenerics/javFiles/TestVector.jav b/resources/insertGenerics/javFiles/TestVector.jav index 6c8f97f0..76c06ff9 100644 --- a/resources/insertGenerics/javFiles/TestVector.jav +++ b/resources/insertGenerics/javFiles/TestVector.jav @@ -3,9 +3,7 @@ import java.util.Vector; public class TestVector { m(v, w) { - var a = v.elementAt(0); - var b = id(a); - w.addElement(b); + w.addElement(id(v.elementAt(0))); } id(x) { diff --git a/src/main/java/de/dhbwstuttgart/target/generate/Bound.java b/src/main/java/de/dhbwstuttgart/target/generate/Bound.java index 1b39df6c..3e749092 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/Bound.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/Bound.java @@ -6,5 +6,19 @@ import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import java.util.List; public record Bound(boolean isOnMethod, RefTypeOrTPHOrWildcardOrGeneric bound) { + public static Bound onMethod(String tph) { + return new Bound(true, TypePlaceholder.of(tph)); + } + public static Bound onMethod(RefTypeOrTPHOrWildcardOrGeneric bound) { + return new Bound(true, bound); + } + + public static Bound onClass(String tph) { + return new Bound(false, TypePlaceholder.of(tph)); + } + + public static Bound onClass(RefTypeOrTPHOrWildcardOrGeneric bound) { + return new Bound(false, bound); + } } diff --git a/src/test/java/targetast/TestGenerics.java b/src/test/java/targetast/TestGenerics.java index 3d4ad553..5f74ae3a 100644 --- a/src/test/java/targetast/TestGenerics.java +++ b/src/test/java/targetast/TestGenerics.java @@ -7,10 +7,11 @@ import de.dhbwstuttgart.syntaxtree.Method; import de.dhbwstuttgart.syntaxtree.type.RefType; import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder; import de.dhbwstuttgart.target.generate.ASTToTargetAST; +import static de.dhbwstuttgart.target.generate.ASTToTargetAST.OBJECT; import de.dhbwstuttgart.target.generate.Bound; +import static de.dhbwstuttgart.target.generate.Bound.*; import de.dhbwstuttgart.target.generate.BoundsList; import de.dhbwstuttgart.target.generate.GenericsResult; -import org.junit.Ignore; import org.junit.Test; import static org.junit.Assert.*; @@ -23,7 +24,7 @@ import java.util.List; public class TestGenerics { - private static final String rootDirectory = System.getProperty("user.dir") + "/resources/insertGenericsJav/"; + private static final String rootDirectory = System.getProperty("user.dir") + "/resources/insertGenerics/javFiles/"; private static final String bytecodeDirectory = System.getProperty("user.dir") + "/src/test/resources/testBytecode/generatedBC/"; private record Result(List genericsResults, ClassOrInterface clazz) { @@ -60,12 +61,12 @@ public class TestGenerics { var ECK1 = generics.getBounds(otherMethod.getParameterList().getParameterAt(0).getType(), result.clazz, anyMethod); var ECK2 = generics.getBounds(otherMethod.getReturnType(), result.clazz, anyMethod); - var ECKChain = new BoundsList(new Bound(false, ASTToTargetAST.OBJECT)); + var ECKChain = new BoundsList(onClass(OBJECT)); assertEquals(ECK1, ECK2); assertEquals(ECK2, generics.getBounds(b.getType(), result.clazz)); var M = generics.getBounds(a.getType(), result.clazz); - var MChain = new BoundsList(new Bound(false, TypePlaceholder.of("ECK")), new Bound(false, ASTToTargetAST.OBJECT)); + var MChain = new BoundsList(onClass("ECK"), onClass(OBJECT)); assertEquals(M, MChain); } @@ -79,7 +80,7 @@ public class TestGenerics { assertEquals(0, generics.get(fReturn).size()); var N = generics.getBounds(fReturn.getReturnType(), result.clazz); - var NChain = new BoundsList(new Bound(false, ASTToTargetAST.OBJECT)); + var NChain = new BoundsList(onClass(OBJECT)); assertEquals(N, NChain); } @@ -97,7 +98,7 @@ public class TestGenerics { var N = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m); var N2 = generics.getBounds(m.getReturnType(), result.clazz, m); - var NChain = new BoundsList(new Bound(true, ASTToTargetAST.OBJECT)); + var NChain = new BoundsList(onMethod(OBJECT)); assertEquals(N, N2); assertEquals(N2, NChain); @@ -123,13 +124,13 @@ public class TestGenerics { assertEquals(2, generics.get(m).size()); var R = generics.getBounds(a.getType(), result.clazz); - var RChain = new BoundsList(new Bound(false, ASTToTargetAST.OBJECT)); + var RChain = new BoundsList(onClass(OBJECT)); assertEquals(R, RChain); var O = generics.getBounds(id.getParameterList().getParameterAt(0).getType(), result.clazz, id); var AB = generics.getBounds(id.getReturnType(), result.clazz, id); assertEquals(O, AB); - assertEquals(AB, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(AB, new BoundsList(onMethod(OBJECT))); var S = generics.getBounds(setA.getParameterList().getParameterAt(0).getType(), result.clazz, setA); assertEquals(S, RChain); @@ -137,7 +138,7 @@ public class TestGenerics { var X = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m); var Y = generics.getBounds(m.getParameterList().getParameterAt(1).getType(), result.clazz, m); - var XChain = new BoundsList(new Bound(true, ASTToTargetAST.OBJECT)); + var XChain = new BoundsList(onMethod(OBJECT)); assertEquals(X, XChain); assertEquals(Y, X); } @@ -163,25 +164,25 @@ public class TestGenerics { assertEquals(3, generics.get(main).size()); var N = generics.getBounds(a.getType(), result.clazz); - assertEquals(N, new BoundsList(new Bound(false, ASTToTargetAST.OBJECT))); + assertEquals(N, new BoundsList(onClass(OBJECT))); var P = generics.getBounds(id.getParameterList().getParameterAt(0).getType(), result.clazz, id); var O = generics.getBounds(id.getReturnType(), result.clazz, id); assertEquals(P, O); - assertEquals(O, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(O, new BoundsList(onMethod(OBJECT))); assertEquals(generics.resolve(m.getParameterList().getParameterAt(0).getType()), generics.resolve(m.getReturnType())); var Y = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m); var AA = generics.getBounds(m.getParameterList().getParameterAt(1).getType(), result.clazz, m); assertEquals(Y, AA); - assertEquals(AA, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(AA, new BoundsList(onMethod(OBJECT))); var AI = generics.getBounds(m.getParameterList().getParameterAt(0).getType(), result.clazz, m); var AJ = generics.getBounds(m.getParameterList().getParameterAt(1).getType(), result.clazz, m); var AH = generics.getBounds(m.getReturnType(), result.clazz, m); assertEquals(AI, AJ); assertEquals(AJ, AH); - assertEquals(AH, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(AH, new BoundsList(onMethod(OBJECT))); } @Test @@ -193,7 +194,7 @@ public class TestGenerics { assertEquals(1, generics.get(anyMethod).size()); var M = generics.getBounds(anyMethod.getReturnType(), result.clazz, anyMethod); - assertEquals(M, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(M, new BoundsList(onMethod(OBJECT))); } @Test @@ -207,12 +208,12 @@ public class TestGenerics { var generics = result.genericsResults.get(0); var M = generics.getBounds(a.getType(), result.clazz); var DYX = generics.getBounds(b.getType(), result.clazz); - var MChain = new BoundsList(new Bound(false, TypePlaceholder.of("DYX")), new Bound(false, ASTToTargetAST.OBJECT)); - var DYXChain = new BoundsList(new Bound(false, ASTToTargetAST.OBJECT)); + var MChain = new BoundsList(onClass("DYX"), onClass(OBJECT)); + var DYXChain = new BoundsList(onClass(OBJECT)); assertEquals(M, MChain); var Q = generics.getBounds(anyMethod.getReturnType(), result.clazz, anyMethod); - assertEquals(Q, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(Q, new BoundsList(onMethod(OBJECT))); System.out.println(otherMethod.getReturnType()); var DYX2 = generics.getBounds(otherMethod.getReturnType(), result.clazz, otherMethod); @@ -235,17 +236,17 @@ public class TestGenerics { var generics = result.genericsResults.get(0); var U = generics.getBounds(id2.getParameterList().getParameterAt(0).getType(), result.clazz, id2); var FPT = generics.getBounds(id2.getReturnType(), result.clazz, id2); - assertEquals(U, new BoundsList(new Bound(true, TypePlaceholder.of("FPT")), new Bound(false, ASTToTargetAST.OBJECT))); - assertEquals(FPT, new BoundsList(new Bound(false, ASTToTargetAST.OBJECT))); + assertEquals(U, new BoundsList(onMethod("FPT"), onClass(OBJECT))); + assertEquals(FPT, new BoundsList(onClass(OBJECT))); var AA = generics.getBounds(m.getReturnType(), result.clazz, m); var AC = generics.getBounds(m.getParameterList().getParameterAt(1).getType(), result.clazz, m); - assertEquals(AA, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); - assertEquals(AC, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(AA, new BoundsList(onMethod(OBJECT))); + assertEquals(AC, new BoundsList(onMethod(OBJECT))); var AH = generics.getBounds(m2.getReturnType(), result.clazz, m2); var AL = generics.getBounds(m2.getParameterList().getParameterAt(0).getType(), result.clazz, m2); - assertEquals(AH, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(AH, new BoundsList(onMethod(OBJECT))); assertEquals(AH, AL); } @@ -262,32 +263,32 @@ public class TestGenerics { var generics = result.genericsResults.get(0); var AO = generics.getBounds(a.getType(), result.clazz); var AOBound = new BoundsList( - new Bound(false, TypePlaceholder.of("Y")), - new Bound(false, TypePlaceholder.of("AK")), - new Bound(false, TypePlaceholder.of("AE")), - new Bound(false, ASTToTargetAST.OBJECT) + onClass("Y"), + onClass("AK"), + onClass("AE"), + onClass(OBJECT) ); assertEquals(AO, AOBound); var S = generics.getBounds(setA.getParameterList().getParameterAt(0).getType(), result.clazz, setA); var c = new ArrayList(); - c.add(new Bound(true, TypePlaceholder.of("AO"))); + c.add(onMethod("AO")); c.addAll(AOBound); var SChain = new BoundsList(c); assertEquals(S, SChain); var Y = generics.getBounds(m.getParameterList().getParameterAt(1).getType(), result.clazz, m); - var YChain = new BoundsList(new Bound(true, TypePlaceholder.of("AE")), new Bound(false, ASTToTargetAST.OBJECT)); + var YChain = new BoundsList(onMethod("AE"), onClass(OBJECT)); var AE = generics.getBounds(m.getReturnType(), result.clazz, m); assertEquals(Y, YChain); - assertEquals(AE, new BoundsList(new Bound(false, ASTToTargetAST.OBJECT))); + assertEquals(AE, new BoundsList(onClass(OBJECT))); // TODO main seems to change between runs /*var AE2 = generics.getBounds(main.getReturnType(), result.clazz, main); var AF = generics.getBounds(main.getParameterList().getParameterAt(0).getType(), result.clazz, main); var AG = generics.getBounds(main.getParameterList().getParameterAt(1).getType(), result.clazz, main); assertEquals(AE, AE2)); - assertEquals(AF, new BoundsList(new Bound(true, TypePlaceholder.of("AK")), new Bound(true, TypePlaceholder.of("AE")), new Bound(false, ASTToTargetAST.OBJECT)))); + assertEquals(AF, new BoundsList(onMethod("AK"), onMethod("AE"), onClass(OBJECT)))); assertEquals(AG, SChain));*/ } @@ -307,15 +308,15 @@ public class TestGenerics { var O = generics.getBounds(id.getReturnType(), result.clazz, id); var O2 = generics.getBounds(id.getParameterList().getParameterAt(0).getType(), result.clazz, id); assertEquals(O, O2); - assertEquals(O2, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(O2, new BoundsList(onMethod(OBJECT))); // TODO Maybe test in other ways if the parameter generics equals the return generics var S = generics.getBounds(main.getReturnType(), result.clazz, main); var S2 = generics.getBounds(main.getParameterList().getParameterAt(0).getType(), result.clazz, main); var T = generics.getBounds(main.getParameterList().getParameterAt(1).getType(), result.clazz, main); assertEquals(S, S2); - assertEquals(S2, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); - assertEquals(T, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(S2, new BoundsList(onMethod(OBJECT))); + assertEquals(T, new BoundsList(onMethod(OBJECT))); } @Test @@ -330,11 +331,11 @@ public class TestGenerics { var S = generics.getBounds(((RefType) par1).getParaList().get(0), result.clazz, m); var ACM = generics.getBounds(((RefType) par2).getParaList().get(0), result.clazz, m); - assertEquals(S, new BoundsList(new Bound(true, TypePlaceholder.of("V")), new Bound(true, ASTToTargetAST.OBJECT))); - assertEquals(ACM, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(S, new BoundsList(onMethod("V"), onMethod(OBJECT))); + assertEquals(ACM, new BoundsList(onMethod(OBJECT))); var Y = generics.getBounds(id.getParameterList().getParameterAt(0).getType(), result.clazz, id); - assertEquals(Y, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(Y, new BoundsList(onMethod(OBJECT))); assertEquals(Y, generics.getBounds(id.getReturnType(), result.clazz, id)); } @@ -348,8 +349,8 @@ public class TestGenerics { var par1 = generics.resolve(add.getParameterList().getParameterAt(0).getType()); var ACK = generics.getBounds(((RefType) par1).getParaList().get(0), result.clazz, add); var O = generics.getBounds(add.getParameterList().getParameterAt(1).getType(), result.clazz, add); - assertEquals(ACK, new BoundsList(new Bound(true, ASTToTargetAST.OBJECT))); - assertEquals(O, new BoundsList(new Bound(true, TypePlaceholder.of("ACK")), new Bound(true, ASTToTargetAST.OBJECT))); + assertEquals(ACK, new BoundsList(onMethod(OBJECT))); + assertEquals(O, new BoundsList(onMethod("ACK"), onMethod(OBJECT))); var par2 = generics.resolve(main.getParameterList().getParameterAt(0).getType()); var ACK2 = generics.getBounds(((RefType) par2).getParaList().get(0), result.clazz, add);