Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Have a look at <a href="http://java.sun.com/docs/books/tutorial/reflect/member/fieldTypes.html" rel="noreferrer">Obtaining Field Types</a> from the Java Tutorial <a href="http://java.sun.com/docs/books/tutorial/reflect/" rel="noreferrer">Trail: The Reflection API</a>. </p> <p>Basically, what you need to do is to get all <a href="http://java.sun.com/javase/6/docs/api/java/lang/reflect/Field.html" rel="noreferrer"><code>java.lang.reflect.Field</code></a> of your class <strike>and call <a href="http://java.sun.com/javase/6/docs/api/java/lang/reflect/Field.html#getType%28%29" rel="noreferrer"><code>Field#getType()</code></a> on each of them</strike> (check edit below). To get <strong>all</strong> object fields including public, protected, package and private access fields, simply use <a href="http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getDeclaredFields%28%29" rel="noreferrer"><code>Class.getDeclaredFields()</code></a>. Something like this:</p> <pre><code>for (Field field : Person.class.getDeclaredFields()) { System.out.format("Type: %s%n", field.getType()); System.out.format("GenericType: %s%n", field.getGenericType()); } </code></pre> <p><strong>EDIT:</strong> As pointed out by <a href="https://stackoverflow.com/users/49237/wowest">wowest</a> in a comment, you actually need to call <a href="http://java.sun.com/javase/6/docs/api/java/lang/reflect/Field.html#getGenericType%28%29" rel="noreferrer"><code>Field#getGenericType()</code></a>, check if the returned <a href="http://java.sun.com/javase/6/docs/api/java/lang/reflect/Type.html" rel="noreferrer"><code>Type</code></a> is a <code>ParameterizedType</code> and then grab the parameters accordingly. Use <a href="http://java.sun.com/javase/6/docs/api/java/lang/reflect/ParameterizedType.html#getRawType%28%29" rel="noreferrer"><code>ParameterizedType#getRawType()</code></a> and <a href="http://java.sun.com/javase/6/docs/api/java/lang/reflect/ParameterizedType.html#getActualTypeArguments%28%29" rel="noreferrer"><code>ParameterizedType#getActualTypeArgument()</code></a> to get the raw type and an array of the types argument of a <code>ParameterizedType</code> respectively. The following code demonstrates this:</p> <pre><code>for (Field field : Person.class.getDeclaredFields()) { System.out.print("Field: " + field.getName() + " - "); Type type = field.getGenericType(); if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType)type; System.out.print("Raw type: " + pType.getRawType() + " - "); System.out.println("Type args: " + pType.getActualTypeArguments()[0]); } else { System.out.println("Type: " + field.getType()); } } </code></pre> <p>And would output:</p> <pre><code>Field: name - Type: class java.lang.String Field: children - Raw type: interface java.util.List - Type args: class foo.Person </code></pre>
    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