Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1034861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:27:29+00:00 2026-05-16T14:27:29+00:00

How do I get a method using reflection based on a parameter value when

  • 0

How do I get a method using reflection based on a parameter value when the parameter type is an interface?

In the following case (based on this example), newValue would be a List<String> called foo. So I would call addModelProperty("Bar", foo); But this only works for me if I don’t use the interface and only use LinkedList<String> foo. How do I use an interface for newValue and get the method from model that has an interface as the parameter addBar(List<String> a0)?

public class AbstractController {
  private final AbstractModel model;
  public setModel(AbstractModel model) {
    this.model = model;
  }
  protected void addModelProperty(String propertyName, Object newValue) {
    try {
      Method method = getMethod(model.getClass(), "add" + propertyName, newValue);
      method.invoke(model, newValue);
    } catch (NoSuchMethodException e) {
    } catch (InvocationTargetException e) {
    } catch (Exception e) {}
  }
  private Method getMethod(Class clazz, String name, Object parameter) {
    return clazz.getMethod(name, parameter.getClass());
  }
}

public class AbstractModel {
  protected PropertyChangeSupport propertyChangeSupport;
  protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
    propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
  }
}

public class Model extends AbstractModel {
  public void addList(List<String> list) {
    this.list.addAll(list);
  }
}

public class Controller extends AbstractController {
  public void addList(List<String> list) {
    addModelProperty(list);
  }
}
public void example() {
  Model model = new Model();
  Controller controller = new Controller();
  List<String> list = new LinkedList<String>();
  list.add("example");
// addList in the model is only found if LinkedList is used everywhere instead of List
  controller.addList(list);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:27:29+00:00Added an answer on May 16, 2026 at 2:27 pm

    You’ll actually have to search through all the methods in your model and find those that are compatible with the arguments you have. It is a bit messy, because, in general, there might be more that one.

    If you are interested in just public methods, the getMethods() method is the easiest to use, because it gives you all accessible methods without walking the class hierarchy.

    Collection<Method> candidates = new ArrayList<Method>();
    String target = "add" + propertyName;
    for (Method m : model.getClass().getMethods()) {
      if (target.equals(m.getName())) {
        Class<?>[] params = m.getParameterTypes();
        if (params.length == 1) {
          if (params[0].isInstance(newValue))
            candidates.add(m);
        }
      }
    }
    /* Now see how many matches you have... if there's exactly one, use it. */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to compare a value which was grabbed using the get method
I am using following method to get bitmap from url but image is not
I am using this method to get a list of week start from my
I am using the following method to get the time of the video with
To get all contacts I'm using ABAddressBookCopyArrayOfAllPeople method but, this method return all contacts
I'm using reflection to get at the TryParse method info (upvote for the first
I'm using reflection to get EntityCollection<Derived> properties from my EF4 domain entities. An example
How to send an http request with either post/get method using javascript as an
I am using rawQuery method to get the list of data as Cursor and
I am using query index() method to get the index of an element relative

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.