简体   繁体   中英

get paramterized type java reflection

Is there anyway to get the inner type parameter of a field using reflection

For Ex:

public final class Main
{
        class A
        {
                public int aX;
                public String aS;
        }

        class B
        {
                public Set<A> bSet;
        }
        public static void main(String[] args)
        {
                Class clazz = B.class.getField("bSet").getType();
        }
};

Using the above I can only get "Set" is there anyway I could get A as well ? Thanks

Reflection can indeed deliver full parameterized types for fields (and classes, etc). It's only objects that have their types erased at runtime.

Call getGenericType instead of getType and start reading javadoc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM