1 Reply Latest reply on Jul 21, 2011 3:49 PM by 802316

    Parametrized collection get parametrized type

      Collection<String> x = new ArrayList<String>();

      Please anybody knows How can I obtain by reflection String Class from x ?

      Class<?> xClass = (Class<?>) ((ParameterizedType)x.getClass().getGenericSuperclass()).getActualTypeArguments()[0];

      This return "E" :((
        • 1. Re: Parametrized collection get parametrized type
          Due to type erasure, you cannot obtain a generics this way.
          You can obtain the generic type of a parent if a specific class was used.
          Collection<String> x = new ArrayList<String>() {}; // creates a subclass of ArrayList
          However, this approach is not recommended. If you must know the type, you can create a checked collection which stores the type.