简体   繁体   English

如何用List获取方法 <SomeObject> 作为参数使用Reflection

[英]How get method with List<SomeObject> as parameter using Reflection

I have 我有

public void setContacts(List<PersonContact> contacts) {
    this.contacts = contacts;
}

A need get this method using Reflection, I have tried 需要使用Reflection获取此方法,我已经尝试过了

  clazz.getMethod("setContacts", ArrayList.class);

show the erro : 显示错误:

java.lang.NoSuchMethodException: model.person.Person.setContacts(java.util.ArrayList)

That's correct, the method signature is 这是正确的,方法签名是

    setContacts(List<PersonContact> contacts)

So, how can I pass correct signature in getMethod? 那么,我如何在getMethod中传递正确的签名?

the parameter type is java.util.List and not java.util.ArrayList 参数类型是java.util.List而不是java.util.ArrayList

clazz.getMethod("setContacts", List.class);

It is important that you uses the actual class because you could also have the method overloaded with an ArrayList parameter 使用实际类很重要,因为您也可以使用ArrayList参数重载该方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM