title: Generics and java.beans.Introspector
body: <p>Given the following code skeleton, is it possible to determine that the property <code>foo</code> is in fact of type <code>String</code>?</p>
  
  <pre><code>public class TestIntrospection {
      public static class SuperBean&lt;T&gt; {
          private T foo;
  
          public T getFoo() { return foo; }
          public void setFoo(T foo) { this.foo = foo; }
      }
  
      public static class SubBean extends SuperBean&lt;String&gt; {
      }
  
      public static void main(String[] args) throws IntrospectionException {
          BeanInfo beanInfo = Introspector.getBeanInfo(SubBean.class);
          PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
          for (PropertyDescriptor prop : propertyDescriptors) {
              if ("foo".equals(prop.getName())) {
                  System.out.printf("%s of %s\n", prop.getName(), prop.getPropertyType());
  
                  Method readMethod = prop.getReadMethod();
                  Type returnType = prop.getReadMethod().getGenericReturnType();
                  if (returnType instanceof TypeVariable) {
                      TypeVariable t = (TypeVariable) returnType;
                      GenericDeclaration d = t.getGenericDeclaration();
                      System.out.println("TypeVariable : " + t.getName() + " " + t.getBounds()[0]);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>The actual output is</p>
  
  <blockquote>
    <p>foo of class java.lang.Object<br>
    TypeVariable : T class java.lang.Object</p>
  </blockquote>
  
  <p><strong>Edit:</strong> I should have mentionend that I know about type erasure and that the method is in fact returning an Object on the bytecode level. Nevertheless, the metadata about generic types is available in the class file and can be queried by reflection as in the sample code. Here is another snippet that shows that SubBean in fact has a type parameter of type String:</p>
  
  <pre><code>                Type superClass = SubBean.class.getGenericSuperclass();
                  ParameterizedType pt = (ParameterizedType) superClass;
                  System.out.println(pt.getActualTypeArguments()[0]);
  </code></pre>
  
  <p>output:</p>
  
  <blockquote>
    <p>class java.lang.String</p>
  </blockquote>
  
  <p>The question then remains, how do I relate this actual type argument to the type variable? If I know that there is only one type parameter this is simple, but I would like this code to work also for beans having multiple generic type parameters.</p>