我有以下代码:
private static void test() {
try {
TestWetterAdapter adap = new TestWetterAdapter();
ClassParserUtil.executeMethod(adap.getClass().getName(), "getWeather", null);
} catch(Exception e) {
e.printStackTrace();
}
try {
TestWetterAdapter adap = new TestWetterAdapter();
Method methodToCall = ClassParserUtil.getMethod(adap.getClass(), "getWeather", null);
System.out.println("Invoke Method " + methodToCall.getName() + " (ParametersLen: " + methodToCall.getParameterTypes().length + ", Annotation: " + methodToCall.getAnnotations()[0] + ") with Class " + adap + " and null.");
Object retObject = methodToCall.invoke(adap, null);
} catch(Exception e) {
e.printStackTrace();
}
}
executeMethod的代码:
public static Object executeMethod(String className, String methodName, Object object) throws ExecuteFailedException {
Object retObject = null;
try {
Class<?> klasse = Class.forName(className);
Constructor<?> constructor = ClassParserUtil.getDefaultConstructor(klasse);
Object classObject = constructor.newInstance();
Method methodToCall = ClassParserUtil.getMethod(classObject.getClass(), methodName, object);
System.out.println("Invoke Method " + methodToCall.getName() + " (ParametersLen: " + methodToCall.getParameterTypes().length + ", Annotation: " + methodToCall.getAnnotations()[0] + ") with Class " + classObject + " and " + object + ".");
retObject = methodToCall.invoke(classObject, object);
} catch(Exception e) {
ExecuteFailedException efe = new ExecuteFailedException("Couldn't execute Method " + methodName + " in class " + className + ".");
efe.initCause(e);
throw(efe);
}
return retObject;
}
有趣的是,两个不同的调用会在调用它们时导致不同的输出。所以我的test()的输出如下所示:
> Call getMethod(class at.wmfsoftware.cct.adapter.TestWetterAdapter, getWeather, null).
Invoke Method getWeather (ParametersLen: 0, Annotation: @at.wmfsoftware.cct.interfaces.AnnotationsInterface$ParseMethodGet()) with Class at.wmfsoftware.cct.adapter.TestWetterAdapter@7a1904 and null.
at.wmfsoftware.cct.exceptions.ExecuteFailedException: Couldn't execute Method getWeather in class at.wmfsoftware.cct.adapter.TestWetterAdapter.
at at.wmfsoftware.cct.utils.parsers.ClassParserUtil.executeMethod(ClassParserUtil.java:301)
at at.wmfsoftware.cct.test.TestMain.test(TestMain.java:92)
at at.wmfsoftware.cct.test.TestMain.main(TestMain.java:78)
Caused by: java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at at.wmfsoftware.cct.utils.parsers.ClassParserUtil.executeMethod(ClassParserUtil.java:299)
... 2 more
Call getMethod(class at.wmfsoftware.cct.adapter.TestWetterAdapter, getWeather, null).
Invoke Method getWeather (ParametersLen: 0, Annotation: @at.wmfsoftware.cct.interfaces.AnnotationsInterface$ParseMethodGet()) with Class at.wmfsoftware.cct.adapter.TestWetterAdapter@91a129 and null.
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
{"coord":{"lon":16.37,"lat":48.21},"sys":{"message":0.0348,"country":"AT","sunrise":1400986998,"sunset":1401043180},"weather":[{"id":800,"main":"Clear","description":"Sky is Clear","icon":"01d"}],"base":"cmc stations","main":{"temp":294.56,"humidity":56,"pressure":1016,"temp_min":293.15,"temp_max":296.35},"wind":{"speed":1.9,"gust":2.7,"deg":10},"rain":{"3h":0},"clouds":{"all":0},"dt":1401010263,"id":2761369,"name":"Vienna","cod":200}
所以有一次成功调用该方法,有一次我得到一个Exception。尽管它必须是相同的方法(我通过编写它的Name,Annotation和ParameterLength来确保这一点。)
有什么想法吗?
由于 威利
答案 0 :(得分:0)
当您将对象作为参数传递时,它将搜索堆中的相应存储,如果没有找到它,则会抛出异常。
所以总是更好地传递一个直接的null并保持安全。