Java:如何检查字段是否属于 java.util.Collection 类型

发布于 2024-12-19 20:55:40 字数 274 浏览 1 评论 0原文

我有一个实用程序方法,可以遍历各种类并递归地检索字段。我想检查该字段是否是一个集合。 这是一些示例代码:

void myMethod(Class<?> classToCheck)

Field[] fields = classToCheck.getDeclaredFields();

for(Field field:fields)
{
   // check if field if a Collection<?>

}

提前感谢您的帮助。

I have a utility method that goes through various classes and recursively retrieves the fields. I want to check if that field is a Collection.
Here is some sample code:

void myMethod(Class<?> classToCheck)

Field[] fields = classToCheck.getDeclaredFields();

for(Field field:fields)
{
   // check if field if a Collection<?>

}

Thanks in advance for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

写下不归期 2024-12-26 20:55:40
if (Collection.class.isAssignableFrom(field.getType())) {

}
if (Collection.class.isAssignableFrom(field.getType())) {

}
笑梦风尘 2024-12-26 20:55:40

您应该使用Class.isAssignableFrom:

if (Collection.class.isAssignableFrom(field.getType())
    ...

You should use Class.isAssignableFrom:

if (Collection.class.isAssignableFrom(field.getType())
    ...
司马昭之心 2024-12-26 20:55:40

使用 getType() 方法

Field field =  ...;
if ( Collection.class.isAssignableFrom( field.getType() ) ){
  //do something with your collection
}

Using the getType() method

Field field =  ...;
if ( Collection.class.isAssignableFrom( field.getType() ) ){
  //do something with your collection
}
世俗缘 2024-12-26 20:55:40

//这里执行if

 List<String> cashType = split(" ,AlL ");
 if(cashType instanceof Collection){
     System.out.println("cashType instanceof Collection");
 }else{
     System.out.println("cashType is not instanceof Collection");
 }

//这里执行else

List<String> cashType = split(" ,AlL ");
 if(cashType instanceof Hashtable){
     System.out.println("cashType instanceof Collection");
 }else{
     System.out.println("cashType is not instanceof Collection");
 }

//This execute if

 List<String> cashType = split(" ,AlL ");
 if(cashType instanceof Collection){
     System.out.println("cashType instanceof Collection");
 }else{
     System.out.println("cashType is not instanceof Collection");
 }

//This executes else

List<String> cashType = split(" ,AlL ");
 if(cashType instanceof Hashtable){
     System.out.println("cashType instanceof Collection");
 }else{
     System.out.println("cashType is not instanceof Collection");
 }
云柯 2024-12-26 20:55:40

您可以按以下方式使用 getType()

if (field.getType().equals(Collection.class) {
    // Do something
}

仅当字段声明为 Collection 时,此方法才有效。如果该字段是 Collection 的子类型,例如 ListVector,则它将不起作用。

You can use getType() in the following way:

if (field.getType().equals(Collection.class) {
    // Do something
}

This will only work if the field is declared as a Collection. It will not work if the field is a subtype of Collection, such as List or Vector.

智商已欠费 2024-12-26 20:55:40
for(Field field:fields) { // check if field if a Collection
  Object myInstance = field.get(classInstance); 
    // where classInstance is the instance in which the fields is stored
    if(myInstance instanceof Collection) {
        //Do your thing
    }
}

这测试字段“field”(对象 classInstance 的)引用的实际对象是否实现 Collection。如果您想测试 Field 的声明类型是否实现 Collection,那么情况会有所不同。

for(Field field:fields) { // check if field if a Collection
  Object myInstance = field.get(classInstance); 
    // where classInstance is the instance in which the fields is stored
    if(myInstance instanceof Collection) {
        //Do your thing
    }
}

This tests whether the actual object referred to by the field 'field' (of the object classInstance) implements Collection. If you want to tests whether the declared type of Field implements Collection then that will be different.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文