参数类型不匹配下面是我的代码从我使用反射来调用一个方法,但我总是得到 例外java.lang.IllegalArgumentException异常:在使用反射
List<PdAttrKey> attrKeys = new ArrayList<PdAttrKey>();
Properties adapterProps = new Properties();
PdReadRequest pdReadRequest = new PdReadRequest(1L, 1L, (short) 0, new Date(),
dataDurationSec, 2L, 3L, attrKeys, null, adapterProps);
PdAdapterUserReadOnlyGemsReader adapter1 = new PdAdapterUserReadOnlyGemsReader();
PdReader reader = adapter1.acquireReader(pdReadRequest);
UserCacheDoImpl userDos = Some Value;
Method method = getClassMethod("createPdRecordFromUserDO");
// This line is throwing me exception. And I don't know why?
PdRecord onePdsxRecord = (PdRecord) method.invoke(reader, userDos);
这是从下面的方法我正在获取一个类的所有方法名称。
private Method getClassMethod(String methodName) {
Method method = null;
Method[] methodList = PdAdapterUserReadOnlyGemsReader.PdUserReadOnlyGemsReader.class
.getDeclaredMethods();
for (Method m : methodList) {
if (m.getName().equals(methodName)) {
method = m;
method.setAccessible(true);
break;
}
}
return method;
}
更多的代码: -
private PdRecord createPdRecordFromUserDO(UserCacheDoImpl userCache) {
// Some code here
}
这是我得到的例外。任何想法为什么?
java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:599)
任何建议都会有很大的帮助。
你可以显示方法'PdReader.createPdsxRecordFromUserDO()'?它只有一个参数,相同类型的'userDos'? – Alex
'userDos'的类型是什么?你给了我们无关的信息(你如何获取方法和设置东西),而不是关于你试图调用的方法的*关键*信息以及你调用的方法。 –
抱歉没有发布其他代码。我只是用其他代码更新了这个问题。让我知道是否需要提供其他信息。 – ferhan