0

I have the following code:

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

The code of the 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;
}

The interesting thing is, that the two different invokations lead to different output, when they are called. So the Output of my test() looks like this:

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

So one time the method is invoked successfully, one time I get an Exception. Allthough it must be the same methods (I make this sure by writing its Name, Annotation and ParameterLength).

Any ideas?

Thanks Willy

  • OK, I found out after writing this post. Strangely invoke has a problem with passing "Object = null" as parameter. When I just pass "null", it works. So I changed --- retObject = methodToCall.invoke(classObject, object); --- to --- if(object == null) retObject = methodToCall.invoke(classObject, null); else retObject = methodToCall.invoke(classObject, object); --- and everything works nicely! Does anyone know, why this happens? Thanks Willy – user3673403 May 25 '14 at 10:10

1 Answers1

0

When you pass an object as parameter it will search its appropriate storage in the heap, and when not finding it, it will throw an exception.

So always better to pass a direct null and be at the safe side.