0

I need to supply class. I have a class Foo so if I supply Foo.class it works fine but I need to supply List<Foo>.class but this doesn't exist.

How can I pass class of List Type?

Any help is appreciated.

rsjaffe
  • 5,600
  • 7
  • 27
  • 39
Tim Liberty
  • 2,099
  • 4
  • 23
  • 39

1 Answers1

2

You cannot, due to type erasure. At runtime, generic type parameters are erased, and no longer available. As a result, although List<Foo> may exist at compile-time as a generic, at runtime you will only have List.class, of type Class<List>.

From the Java Language Specification, version 8, section 15.8.2:

It is a compile-time error if the named type is a type variable (§4.4) or a parameterized type (§4.5) or an array whose element type is a type variable or parameterized type.

For example, List<Foo>.class, T.class, List<Foo>[].class, and T[].class are all invalid (assuming that T is a type parameter).

Community
  • 1
  • 1
nanofarad
  • 40,330
  • 4
  • 86
  • 117