user614431
user614431

Reputation: 105

Create java Parameterized Type on the fly

How to create a java.lang.reflect.Type of type List<T> on the fly if I know T is of certain type like String.class or Integer.class?

Upvotes: 1

Views: 485

Answers (1)

Matt Ball
Matt Ball

Reputation: 360026

Java parameterized types are subject to type erasure so that generic types are lost at runtime. This means that, at runtime, a List<String> is indistinguishable from a List<Integer> or a List<Object>, without inspecting the elements in the list.

All this to say, you don't need to worry about the parameterized type at all. You can just create a List.

Upvotes: 4

Related Questions