简体   繁体   中英

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

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.

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 .

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