ParametrizedType – empty array as result of getActualTypeArguments()

I’m testing code with reflections and I have a problem with reproduce one if statement in my code, probably because I don’t understand correctly how the method works.

class TestClass {
    public Collection<TestField> field;
}
...
ParameterizedType type = (ParameterizedType) TestClass.class.getField("field").getGenericType();

Now, I am looking for a FieldType, for which the getActualTypeArguments() method will return an EMPTY Type[] array.

Based on documentation:

Note that in some cases, the returned array be empty. This can occur if this type represents a non-parameterized type nested within a parameterized type.

Unfortunately, I have a problem with understanding this fragment, so I can’t design a class that will fulfill it.

Answer

Section 4.5 of the JLS has a footnote about this:

A parameterized type may be an parameterization of a generic class or interface which is nested. For example, if a non-generic class C has a generic member class D<T>, then C.D<Object> is a parameterized type. And if a generic class C<T> has a non-generic member class D, then the member type C<String>.D is a parameterized type, even though the class D is not generic.

So C<String>.D is also a ParameterizedType! This is what the documentation of getActualTypeArguments was referring to. And according to the docs, getActualTypeArguments would return an empty array for C<String>.D. Here is an example:

class Foo<T> {
    class Bar {

    }
}
public class Main {

    public Foo<Integer>.Bar bar;

    public static void main(String[] args) throws Exception {
        ParameterizedType type = (ParameterizedType) Main.class.getField("bar").getGenericType();

        // this prints 0
        System.out.println(type.getActualTypeArguments().length);
    }

}