jeudi 13 juillet 2017

Why does the outer class appear twice in the generic type name?

Q: Why does the name of the containing class appear twice?

Context: I'm generating code and the goal is to get the declaration of the field, as written in the source (fully qualified is fine, but I need the type parameter): test.Foo.Bar<java.lang.String>

package test;

import java.lang.reflect.Field;
import java.lang.reflect.Type;

public class Foo
{
  public static class Bar<TYPE> {}

  private Bar<String> bar;

  public static void main(String[] args) throws Exception
  {
    Field field = Foo.class.getDeclaredField("bar");
    Type genericType = field.getGenericType();
    Type type = field.getType();

    System.out.println("genericType: " + genericType.getTypeName());
    System.out.println("       type: " + type.getTypeName());
  }
}

Output:

genericType: test.Foo.test.Foo$Bar<java.lang.String>
       type: test.Foo$Bar





Aucun commentaire:

Enregistrer un commentaire