如何获取嵌套类型的所有内部结构类型? [英] How to get all types of the inner structures of a nested type?
问题描述
我有一个波纹管功能,
public void park( List<List< List< Integer>>> l){ System.out.println("park"); }
当我尝试使用Method.getParameters()+ Parameter.getParameterizedType()时,我得到了
and when I tried to use Method.getParameters() + Parameter.getParameterizedType(), I got
java.util.List< java.util.List<java.util.List<java.lang.Integer>>>>
但是,我需要获取嵌套结构内侧的类型,例如 java.util.List<java.util.List< >> 和 Integer .当我得到它时,我使用getAnnotationsByType来获取内部结构的所有注释.
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.
我曾经考虑过使用Parameter.getParameterizedType().toString()来解析嵌套结构,但是即使我这样做,我也不认为类型字符串可以转换为Type.
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.
推荐答案
您可以通过 ParameterizedType
界面获取此信息.
You can get this information through the ParameterizedType
interface.
例如:
final Type parameterType = method.getGenericParameterTypes()[0]; // List<List<List<Integer>>>
if (parameterType instancof ParameterizedType) {
final Type nextDown // List<List<Integer>>
= ((ParameterizedType) parameterType).getActualTypeArguments();
}
并不断重复直到它不是不是一个 ParameterizedType
.
And keep repeating until it is not a ParameterizedType
.
这篇关于如何获取嵌套类型的所有内部结构类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!