From 247eb0ef79498769b99c5416c33d92c35baf4b29 Mon Sep 17 00:00:00 2001 From: Victorious3 Date: Thu, 22 Sep 2022 18:48:50 +0200 Subject: [PATCH] Add tests for generic parameters --- src/test/java/targetast/TestComplete.java | 205 +++++++++++++++++----- 1 file changed, 157 insertions(+), 48 deletions(-) diff --git a/src/test/java/targetast/TestComplete.java b/src/test/java/targetast/TestComplete.java index c2b41371..0b8d6d46 100644 --- a/src/test/java/targetast/TestComplete.java +++ b/src/test/java/targetast/TestComplete.java @@ -4,7 +4,9 @@ import de.dhbwstuttgart.target.ByteArrayClassLoader; import org.junit.Ignore; import org.junit.Test; +import java.lang.reflect.Array; import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; import java.lang.reflect.TypeVariable; import java.util.Arrays; import java.util.Vector; @@ -342,13 +344,53 @@ public class TestComplete { @Test public void tph2Test() throws Exception { var classFiles = generateClassFiles("Tph2.jav", new ByteArrayClassLoader()); - var instance = classFiles.get("Tph2").getDeclaredConstructor().newInstance(); + var tph2 = classFiles.get("Tph2"); + var instance = tph2.getDeclaredConstructor().newInstance(); + + assertEquals(1, tph2.getTypeParameters().length); + // public class Tph2 + var DZG = tph2.getTypeParameters()[0]; + + var id = tph2.getDeclaredField("id"); + // public Fun1$$ id + var idParams = ((ParameterizedType) id.getGenericType()).getActualTypeArguments(); + assertEquals(2, idParams.length); + assertEquals(DZG, idParams[0]); + assertEquals(DZG, idParams[1]); + + var id3 = tph2.getDeclaredMethod("id3", Object.class); + // public DZG id3(U var1) + var paraTypes = id3.getGenericParameterTypes(); + var typeParaTypes = id3.getTypeParameters(); + + var U = Arrays.stream(typeParaTypes).filter(t -> t.equals(paraTypes[0])).findFirst().get(); + assertEquals(DZG, U.getBounds()[0]); + assertEquals(DZG, id3.getGenericReturnType()); } @Test public void tph3Test() throws Exception { var classFiles = generateClassFiles("Tph3.jav", new ByteArrayClassLoader()); - var instance = classFiles.get("Tph3").getDeclaredConstructor().newInstance(); + var tph3 = classFiles.get("Tph3"); + var instance = tph3.getDeclaredConstructor().newInstance(); + var m1 = tph3.getDeclaredMethod("m1", Object.class, Object.class); + + // public void m1(DZC var1, DZC var2) + var paraTypes = m1.getGenericParameterTypes(); + var typeParaTypes = m1.getTypeParameters(); + + var DZC = Arrays.stream(typeParaTypes).filter(t -> t.equals(paraTypes[0])).findFirst().get(); + assertEquals(DZC, Arrays.stream(typeParaTypes).filter(t -> t.equals(paraTypes[1])).findFirst().get()); + assertEquals(Object.class, DZC.getBounds()[0]); + + var m2 = tph3.getDeclaredMethod("m2", Object.class); + + // public void m2(V var1) + var paraTypes2 = m2.getGenericParameterTypes(); + var typeParaTypes2 = m2.getTypeParameters(); + + var V = Arrays.stream(typeParaTypes2).filter(t -> t.equals(paraTypes2[0])).findFirst().get(); + assertEquals(Object.class, V.getBounds()[0]); } @Test @@ -356,6 +398,30 @@ public class TestComplete { var classFiles = generateClassFiles("Tph4.jav", new ByteArrayClassLoader()); var tph4 = classFiles.get("Tph4"); var instance = tph4.getDeclaredConstructor().newInstance(); + var m = tph4.getDeclaredMethod("m", Object.class, Object.class); + + // public M m(N var1, O var2) + var paraTypes = m.getGenericParameterTypes(); + var typeParaTypes = m.getTypeParameters(); + + var N = Arrays.stream(typeParaTypes).filter(t -> t.equals(paraTypes[0])).findFirst().get(); + assertEquals(Object.class, N.getBounds()[0]); + + var O = Arrays.stream(typeParaTypes).filter(t -> t.equals(paraTypes[1])).findFirst().get(); + var P = Arrays.stream(typeParaTypes).filter(t -> t.equals(O.getBounds()[0])).findFirst().get(); + var M = Arrays.stream(typeParaTypes).filter(t -> t.equals(P.getBounds()[0])).findFirst().get(); + assertEquals(Object.class, M.getBounds()[0]); + assertEquals(M, m.getGenericReturnType()); + + var m2 = tph4.getDeclaredMethod("m2", Object.class); + + // public X m2(X var1) + var paraTypes2 = m2.getGenericParameterTypes(); + var typeParaTypes2 = m2.getTypeParameters(); + + var X = Arrays.stream(typeParaTypes2).filter(t -> t.equals(paraTypes2[0])).findFirst().get(); + assertEquals(Object.class, X.getBounds()[0]); + assertEquals(X, m2.getGenericReturnType()); } @Test @@ -364,6 +430,26 @@ public class TestComplete { var tph5 = classFiles.get("Tph5"); var instance = tph5.getDeclaredConstructor().newInstance(); var m = tph5.getDeclaredMethod("m", Object.class, Object.class); + + // public void m(N var1, O var2) + var paraTypes = m.getGenericParameterTypes(); + var typeParaTypes = m.getTypeParameters(); + + var N = Arrays.stream(typeParaTypes).filter(t -> t.equals(paraTypes[0])).findFirst().get(); + var O = Arrays.stream(typeParaTypes).filter(t -> t.equals(paraTypes[1])).findFirst().get(); + + assertEquals(O.getBounds()[0], N); + assertEquals(Object.class, N.getBounds()[0]); + + var m2 = tph5.getDeclaredMethod("m2", Object.class); + + // public R m2(R var1) + var paraTypes2 = m2.getGenericParameterTypes(); + var typeParaTypes2 = m2.getTypeParameters(); + + var R = Arrays.stream(typeParaTypes2).filter(t -> t.equals(paraTypes2[0])).findFirst().get(); + assertEquals(Object.class, R.getBounds()[0]); + assertEquals(R, m2.getGenericReturnType()); } @Test @@ -372,73 +458,96 @@ public class TestComplete { var tph5 = classFiles.get("Tph6"); var instance = tph5.getDeclaredConstructor().newInstance(); var m = tph5.getDeclaredMethod("m", Object.class, Object.class); + + // public void m(N var1, O var2) + var paraTypes = m.getGenericParameterTypes(); + var typeParaTypes = m.getTypeParameters(); + + var N = Arrays.stream(typeParaTypes).filter(t -> t.equals(paraTypes[0])).findFirst().get(); + var O = Arrays.stream(typeParaTypes).filter(t -> t.equals(paraTypes[1])).findFirst().get(); + + var PT = N.getBounds()[0]; + assertEquals(PT, O.getBounds()[0]); + + var P = Arrays.stream(typeParaTypes).filter(t -> t.equals(PT)).findFirst().get(); + assertEquals(Object.class, P.getBounds()[0]); + + var m2 = tph5.getDeclaredMethod("m2", Object.class); + + // public W m2(W var1) + var paraTypes2 = m2.getGenericParameterTypes(); + var typeParaTypes2 = m2.getTypeParameters(); + + var W = Arrays.stream(typeParaTypes2).filter(t -> t.equals(paraTypes2[0])).findFirst().get(); + assertEquals(Object.class, W.getBounds()[0]); + assertEquals(W, m2.getGenericReturnType()); } - @Test - public void Tph7Test() throws Exception { - var classFiles = TestCodegen.generateClassFiles("Tph7.jav", new ByteArrayClassLoader()); - var classToTest = classFiles.get("Tph7"); - var instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); + @Test + public void Tph7Test() throws Exception { + var classFiles = TestCodegen.generateClassFiles("Tph7.jav", new ByteArrayClassLoader()); + var classToTest = classFiles.get("Tph7"); + var instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); - //public DZU m(DZL, DZM); - Method m = classToTest.getDeclaredMethod("m", Object.class, Object.class); + //public DZU m(DZL, DZM); + Method m = classToTest.getDeclaredMethod("m", Object.class, Object.class); - //System.out.println(m.toString()); + //System.out.println(m.toString()); - //Argumenttypes of the method m - var paraTypes = m.getGenericParameterTypes(); + //Argumenttypes of the method m + var paraTypes = m.getGenericParameterTypes(); - //Typeparameters of the method m - var typeParaTypes = m.getTypeParameters(); + //Typeparameters of the method m + var typeParaTypes = m.getTypeParameters(); - //Typeparameters are extracted from the argumenttypes - //Conditions for the extracted typeparameters are set + //Typeparameters are extracted from the argumenttypes + //Conditions for the extracted typeparameters are set - //paraTypes[0] = DLZ - var boundFstArg = Arrays.stream(typeParaTypes) - .filter(x -> x.equals(paraTypes[0])).findFirst().get().getBounds(); + //paraTypes[0] = DLZ + var boundFstArg = Arrays.stream(typeParaTypes) + .filter(x -> x.equals(paraTypes[0])).findFirst().get().getBounds(); - //Bound of DLZ has to be Object - assertEquals(Object.class, Arrays.stream(boundFstArg).findFirst().get()); + //Bound of DLZ has to be Object + assertEquals(Object.class, Arrays.stream(boundFstArg).findFirst().get()); - //paraTypes[0] = DZM - var boundSndArg = Arrays.stream(typeParaTypes) - .filter(x -> x.equals(paraTypes[1])).findFirst().get().getBounds(); + //paraTypes[0] = DZM + var boundSndArg = Arrays.stream(typeParaTypes) + .filter(x -> x.equals(paraTypes[1])).findFirst().get().getBounds(); - //Bound of DZM has to be the return type of m - assertEquals(Arrays.stream(boundSndArg).findFirst().get(), m.getGenericReturnType()); + //Bound of DZM has to be the return type of m + assertEquals(Arrays.stream(boundSndArg).findFirst().get(), m.getGenericReturnType()); - //Bound of the bound of DZM - var boundBoundSndArg = Arrays.stream(typeParaTypes).filter(x -> x.equals(Arrays.stream(boundSndArg) - .findFirst().get())).findFirst().get().getBounds(); + //Bound of the bound of DZM + var boundBoundSndArg = Arrays.stream(typeParaTypes).filter(x -> x.equals(Arrays.stream(boundSndArg) + .findFirst().get())).findFirst().get().getBounds(); - //boundBoundSndArg have to be a type variable (type of the local variable c) - assertEquals(true, Arrays.stream(boundBoundSndArg).findFirst().get() instanceof TypeVariable); - m.getGenericParameterTypes(); + //boundBoundSndArg have to be a type variable (type of the local variable c) + assertEquals(true, Arrays.stream(boundBoundSndArg).findFirst().get() instanceof TypeVariable); + m.getGenericParameterTypes(); - //public DZU m2(DZU); - Method m2 = classToTest.getDeclaredMethod("m2", Object.class); + //public DZU m2(DZU); + Method m2 = classToTest.getDeclaredMethod("m2", Object.class); - //Argumenttypes of the method m2 - var paraTypesm2 = m2.getGenericParameterTypes(); + //Argumenttypes of the method m2 + var paraTypesm2 = m2.getGenericParameterTypes(); - //Typeparameters of the method m2 - var typeParaTypesm2 = m2.getTypeParameters(); + //Typeparameters of the method m2 + var typeParaTypesm2 = m2.getTypeParameters(); - //Typeparameters are extracted from the argumenttypes - //Conditions for the extracted typeparameters are set + //Typeparameters are extracted from the argumenttypes + //Conditions for the extracted typeparameters are set - //paraTypes[0] = DZU - var fstArgm2 = Arrays.stream(typeParaTypesm2) - .filter(x -> x.equals(paraTypesm2[0])).findFirst().get(); + //paraTypes[0] = DZU + var fstArgm2 = Arrays.stream(typeParaTypesm2) + .filter(x -> x.equals(paraTypesm2[0])).findFirst().get(); - //Bound of DZU has to be Object - assertEquals(Object.class, Arrays.stream(fstArgm2.getBounds()).findFirst().get()); + //Bound of DZU has to be Object + assertEquals(Object.class, Arrays.stream(fstArgm2.getBounds()).findFirst().get()); - //DZU has to be the return type of m - assertEquals(fstArgm2, m2.getGenericReturnType()); - } + //DZU has to be the return type of m + assertEquals(fstArgm2, m2.getGenericReturnType()); + } @Test