How to get all types of the inner structures of a nested type?

心已入冬 提交于 2021-02-05 09:45:20

问题


I have a function bellow,

public void park( List<List< List< Integer>>> l){ System.out.println("park"); }

and when I tried to use Method.getParameters() + Parameter.getParameterizedType(), I got

 java.util.List< java.util.List<java.util.List<java.lang.Integer>>>>

But, I need to get the type of the inner side of the nested structure, say, java.util.List< java.util.List<>> and Integer . When I get that, I use getAnnotationsByType to get all annotations of the inner structures.

I have thought to use the Parameter.getParameterizedType().toString() to parse the nested structure, but even if I do, I don't think the type string can be converted to Type.

Is it possible to get inner types of a nested structure? Anything is appreciated.


回答1:


You can get this information through the ParameterizedType interface.

For example:

final Type parameterType = method.getGenericParameterTypes()[0]; // List<List<List<Integer>>>

if (parameterType instancof ParameterizedType) {
    final Type nextDown // List<List<Integer>>
        = ((ParameterizedType) parameterType).getActualTypeArguments();
}

And keep repeating until it is not a ParameterizedType.



来源:https://stackoverflow.com/questions/60495176/how-to-get-all-types-of-the-inner-structures-of-a-nested-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!