In the code below I expect name
to be BlackBox
, as this is the type of the complex
field. But it returns Pair
, which is the type of the other field, simple
:
public class QuestionTest {
abstract class BlackBox<TSimple, TComplex> {
public TComplex complex;
public TSimple simple;
}
abstract class Pair {
public String one;
public String two;
}
@Test
public void Test() throws NoSuchFieldException {
BlackBox<Integer, Pair> blackBox =
new BlackBox<Integer, Pair>() {};
blackBox.complex = new Pair() {};
blackBox.complex.one = "Uno";
blackBox.complex.two = "Duo";
blackBox.simple = 3;
Class cls = blackBox.getClass();
Field field = cls.getField("complex");
TypeVariable typeVariable = (TypeVariable) field.getGenericType();
GenericDeclaration genDec = typeVariable.getGenericDeclaration();
Class genCls = (Class) genDec;
String name = genCls.getSimpleName();
Assert.assertEquals("Pair", name); // Fails! name is "BlackBox"
}
}
Where's is my mistake?
Aucun commentaire:
Enregistrer un commentaire