问题
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