使用反射来获得方法;找不到接口类型的方法参数 [英] Using reflection to get a method; method parameters of interface types aren't found

查看:808
本文介绍了使用反射来获得方法;找不到接口类型的方法参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我在这里缺少一些简单的东西,但是我如何得到一个方法,其参数是使用反射的接口。

Maybe I'm missing something simple here but how do I get a method whose parameter is an interface using reflection.

在下面的例子中 newValue 将是 List< String> ,名为 foo 。所以我会调用 addModelProperty(Bar,foo); 但是这只适用于我,如果我不使用该接口并只使用 LinkedList< ;字符串> FOO 。如何使用 newValue 的接口,并从 model 获取具有接口作为参数<$ c的方法$ c> addBar(List< String> a0)?

In the following case 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)?

这是一个更详细的例子。 (基于:此示例

Here is a more detailed example. (based on: This example)

public class AbstractController {
  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) {}
    }
}

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);
}


推荐答案

你真的需要搜索模型中的所有方法,找到与您拥有的参数兼容的方法。它有点乱,因为,一般来说,可能会有更多。

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.

如果你只对公共方法感兴趣, getMethods ()方法是最容易使用的方法,因为它为您提供了所有可访问的方法而无需走类层次结构。

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. */

这篇关于使用反射来获得方法;找不到接口类型的方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆