Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://www.angelikalanger.com/GenericsFAQ/FAQSections/TechnicalDetails.html#What%20is%20type%20erasure?" rel="noreferrer">Type erasure</a> means that information about the generic type of an <em>object</em> simply isn't present at execution time.</p> <p>(The link is to the relevant section of Angelika Langer's <a href="http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html" rel="noreferrer">Java Generics FAQ</a> which should answer virtually every question you could possibly ask about Java generics :)</p> <p>However, you're not really interested in the type of an object - you're interested in the type of a <em>field</em>. I misread the question, and although the answer has been accepted I hope to make amends by fixing it now :)</p> <p>If the field doesn't use a type parameter itself, it can be done. For example:</p> <pre><code>import java.lang.reflect.*; import java.util.*; public class Test { public List&lt;String&gt; names; public static void main(String [] args) throws Exception // Just for simplicity! { Field field = Test.class.getDeclaredField("names"); ParameterizedType type = (ParameterizedType) field.getGenericType(); // List System.out.println(type.getRawType()); // Just String in this case for (Type typeArgument : type.getActualTypeArguments()) { System.out.println(" " + typeArgument); } } } </code></pre> <p>If the field were in a class <code>T</code> with the field being <code>List&lt;T&gt;</code> then you'd have to know the type argument for the instance in order to know the type argument for the collection.</p> <p>Translating this into your required code is somewhat tricky though - you really need to know the type argument at the point of the collection class. For instance, if someone declared:</p> <pre><code>public class StringCollection implements Collection&lt;String&gt; </code></pre> <p>and then had a field of type <code>StringCollection</code>, that field itself wouldn't have any type arguments. You'd then need to check <code>getGenericSuperType</code> and <code>getGenericInterfaces</code> recursively until you found what you wanted.</p> <p>It's really not going to be easy to do that, even though it's possible. If I were you I'd try to change your design so that you don't need this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload