modified: ../../java/targetast/TphTest.java

Test fuer Tph7 so angepasst, dass ueberprueft wird ob die richtigen Typvariablen generiert werden
This commit is contained in:
pl@gohorb.ba-horb.de 2022-08-06 22:17:49 +02:00
parent 6d12102fbf
commit 092d066774

View File

@ -5,6 +5,8 @@ import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;
import java.util.Arrays;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@ -12,23 +14,77 @@ public class TphTest {
private static Class<?> classToTest; private static Class<?> classToTest;
private static Object instanceOfClass; private static Object instanceOfClass;
@BeforeClass @BeforeClass
public static void setUpBeforeClass() throws Exception { public static void setUpBeforeClass() throws Exception {
var classFiles = TestCodegen.generateClassFiles("Tph.jav", new ByteArrayClassLoader()); var classFiles = TestCodegen.generateClassFiles("Tph.jav", new ByteArrayClassLoader());
classToTest = classFiles.get("Tph"); classToTest = classFiles.get("Tph");
instanceOfClass = classToTest.getDeclaredConstructor().newInstance(); instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
classFiles = TestCodegen.generateClassFiles("Tph7.jav", new ByteArrayClassLoader());
classToTest = classFiles.get("Tph7");
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
} }
@Test @Test
public void test1() throws Exception { public void test1() throws Exception {
var classFiles = TestCodegen.generateClassFiles("Tph7.jav", new ByteArrayClassLoader());
classToTest = classFiles.get("Tph7");
instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
//public <DZN, DZL, DZU extends DZN, DZM extends DZU> DZU m(DZL, DZM);
Method m = classToTest.getDeclaredMethod("m", Object.class, Object.class); Method m = classToTest.getDeclaredMethod("m", Object.class, Object.class);
Object result = m.invoke(instanceOfClass, 1,2);
//System.out.println(m.toString());
assertEquals(1,result);
//Argumenttypes of the method m
var paraTypes = m.getGenericParameterTypes();
//Typeparameters of the method m
var typeParaTypes = m.getTypeParameters();
//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();
//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();
//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();
//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> DZU m2(DZU);
Method m2 = classToTest.getDeclaredMethod("m2", Object.class);
//Argumenttypes of the method m2
var paraTypesm2 = m2.getGenericParameterTypes();
//Typeparameters of the method m2
var typeParaTypesm2 = m2.getTypeParameters();
//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();
//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());
} }
@Test @Test