如何使用反射获得List ?

时间:2022-11-22 21:38:56

I have am getting a method, and checking method.getParameterTypes()[0], which is a java.util.List. But I want to figure out what the containing type is, for instance if it's a java.util.List<String> I want to figure out that it should hold strings.

我得到了一个方法,并检查method.getParameterTypes()[0],它是一个java.util.List。但是我想知道包含的类型是什么,例如如果它是java.util。列出 我想知道它应该保存字符串。

How do I do this?

我该怎么做呢?

Thanks!

谢谢!

3 个解决方案

#1


22  

Type listType = method.getGenericParameterTypes()[0];
if (listType instanceof ParameterizedType) {
    Type elementType = ((ParameterizedType) listType).getActualTypeArguments()[0];
}

Note that the element type needn't be an actual Class like String -- it could be a type variable, a wildcarded type, etc.

注意,元素类型不必是真正的类,比如String——它可以是类型变量、通配符类型等等。

You can read more about scraping generics info from reflected items here and here.

你可以从这里和这里看到更多关于抓取泛型信息的信息。

#2


3  

It's simple : you can't. At compile time, Java erases generic types (see Type erasure).

很简单:你不能。在编译时,Java会删除泛型类型(参见类型擦除)。

You can see here and here for more information about GetGenericType (which I didn't know about, honestly), but it seems rather limited.

您可以在这里和这里看到关于GetGenericType的更多信息(说实话,我不知道),但它似乎相当有限。

#3


0  

I had the same issue, and I did as below

我有同样的问题,我做了如下。

in here : your class is initialize as clazz (eg - Company.Class) and your Collection as

在这里:您的类初始化为clazz(例如:Company.Class),而您的集合初始化为

List<Employees> companyEmployee

<员工> companyEmployee列表

String collectionType- List

字符串collectionType——列表

String collectionContent - Employee

字符串collectionContent——员工


Method[] methodList = clazz.getDeclaredMethods();
Method classMethod = methodList[0];

String[] collection= classMethod.getReturnType().toString().split("\\.");

String collectionType = collection[collection.length - 1]

String collectionContent = classMethod.getGenericReturnType().getTypeName().split("<")[1].split(">")[0]

#1


22  

Type listType = method.getGenericParameterTypes()[0];
if (listType instanceof ParameterizedType) {
    Type elementType = ((ParameterizedType) listType).getActualTypeArguments()[0];
}

Note that the element type needn't be an actual Class like String -- it could be a type variable, a wildcarded type, etc.

注意,元素类型不必是真正的类,比如String——它可以是类型变量、通配符类型等等。

You can read more about scraping generics info from reflected items here and here.

你可以从这里和这里看到更多关于抓取泛型信息的信息。

#2


3  

It's simple : you can't. At compile time, Java erases generic types (see Type erasure).

很简单:你不能。在编译时,Java会删除泛型类型(参见类型擦除)。

You can see here and here for more information about GetGenericType (which I didn't know about, honestly), but it seems rather limited.

您可以在这里和这里看到关于GetGenericType的更多信息(说实话,我不知道),但它似乎相当有限。

#3


0  

I had the same issue, and I did as below

我有同样的问题,我做了如下。

in here : your class is initialize as clazz (eg - Company.Class) and your Collection as

在这里:您的类初始化为clazz(例如:Company.Class),而您的集合初始化为

List<Employees> companyEmployee

<员工> companyEmployee列表

String collectionType- List

字符串collectionType——列表

String collectionContent - Employee

字符串collectionContent——员工


Method[] methodList = clazz.getDeclaredMethods();
Method classMethod = methodList[0];

String[] collection= classMethod.getReturnType().toString().split("\\.");

String collectionType = collection[collection.length - 1]

String collectionContent = classMethod.getGenericReturnType().getTypeName().split("<")[1].split(">")[0]