Rodrigo Rodrigues
Rodrigo Rodrigues

Reputation: 669

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

  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?

Upvotes: 4

Views: 10235

Answers (1)

Philipp Sander
Philipp Sander

Reputation: 10249

the parameter type is java.util.List and not 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

Upvotes: 3

Related Questions