Specifying generic collection type param at runtime (Java Reflection)

前端 未结 4 749
情书的邮戳
情书的邮戳 2021-01-06 02:40

I\'d like to get the generic type of a collection, using reflection, at runtime.

Code (JAVA):

Field collectionObject = object.getClass().getDeclaredF         


        
4条回答
  •  既然无缘
    2021-01-06 03:21

    You can get the generic type of the field you read the object from with Field.getGenericType. Note that the field may be a raw type, be a rare type (generic but with a generic argument that is raw), use the type of the instance, use wildcards, etc.

提交回复
热议问题