0

I have the following method where T is a particular class,

public Class<List<T>> responseClass();

While implementing this method, how should I return class of List<T> ?

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Arpit Agarwal
  • 15
  • 2
  • 7
  • 1
    You can't due to type erasure – Lino May 03 '19 at 06:53
  • 4
    What do you intend to do? What is the underlying problem you try to solve here? – GhostCat May 03 '19 at 06:57
  • 1
    @Lino you could using TypeTokens, for example – Eugene May 03 '19 at 10:55
  • @Eugene only works though if you know `T`. I think jackson has the same with `ParameterizedTypeReference`. Also it can't be done in a complete generic context, like a generic method – Lino May 03 '19 at 10:59
  • @Lino right. you could return a `Type` instead, though. – Eugene May 03 '19 at 11:01
  • @Lino it’s technically possible to capture a type parameter like `T`, the same way these type tokens work. But it wouldn’t be useful for anything. – Holger May 10 '19 at 10:45
  • @Holger from what I tried it just returns a `Type T` which is at most useful, to print the name of the type (which would still be only `T`) – Lino May 10 '19 at 10:48
  • 1
    @Lino well, it’s possible to match it with the declaration site, i.e. since it is not declared at the method in this example, it must be declared at a surrounding entity. However, the typical intention of being able to tell whether a particular object is of that type or to construct compatible instances, is not possible. At least not without another type token describing the parameterization of the surrounding entity that declared `T`. – Holger May 10 '19 at 10:52

0 Answers0