So I've read a lot of other questions out there, but I cannot find one with similar case as mine. Assume I have the following code:
public class TestClass
{
public Class clazz;
public TestClass(Object input)
{
this.clazz = ........ ?? // how to get String.class from "input" parameter?
}
public static void main(String[] args)
{
List<String> all = new ArrayList<>();
new TestClass(all);
}
}
The constructor of TestClass has 1 parameter which is an Object type. But at runtime, it receives a variable of type List, but the actual instance is an ArrayList. Inside the TestClass constructor, I want to extract the generic type of that ArrayList from input parameter. The expected result should be String.class object, which can be stored in clazz variable.
Aucun commentaire:
Enregistrer un commentaire