我想用Class.getMethod()反射和自動裝箱的任何解決方案?
Class.getMethod(String name, Class... parameterTypes)
找到我需要用給定的參數調用該方法,但顯然在Bug 6176992描述Java不包括自動裝箱那裏。所以如果我的反射類有一個帶有(String,int)簽名的方法,你仍然會得到一個帶{String.class,Integer.class}數組作爲參數的NoSuchMethodException。
有沒有這方面的溶劑?我可以想到的唯一方法就是調用getMethod()以及我不想要的原始和非原始類型的每種排列。
編輯:爲了更清楚:我很清楚原始類型類,但我沒有看到他們如何幫助解決我的問題。我的parameterTypes數組來自某處,我知道它只會返回非原始類型。我不能認爲界面只會用原始類型聲明,這正是我的問題:
public interface TestInterface()
{
public void doTest(Integer i1, int i2, double d3, Double d);
}
Class<?>[] classes = { Integer.class, Integer.class, Double.class, Double.class }
// Due to autoboxing I should become the doTest method here, but it doesn't work
TestInterface.class.getMethod("doTest", classes);
您知道方法的簽名是提前? – PSpeed 2009-12-12 21:27:19
不,不是。那麼好的另一個解決方案是獲得具有該名稱的所有方法,並查看簽名是否與(un)盒裝類型相匹配。 – Daff 2009-12-12 21:30:03