Operatoren Java Reflections: Alle Methoden einer Klasse aufrufen ohne Exceptions

lam_tr

Top Contributor
Hallo zusammen,

kann ich irgendwie per Reflections alle Methodenaufrufe machen die kein Exceptions werfen? Oder anders gefragt, Aktuell führe ich alle Methoden einer bestimmten Klasse aus, aber bei einigen dieser Methoden wird es nicht mehr supported und wird beim Ausführen so ein Exception

Code:
Caused by: java.lang.UnsupportedOperationException
    at org.eclipse.emf.ecore.impl.MinimalEObjectImpl.eBasicAdapters(MinimalEObjectImpl.java:294)
    ... 68 more

Kann ich bevor ich ein invoke mache davor checken ob die Methode ausführbar ist?

Grüße
lam
 
Musst du diese Exception wohl catchen.

Das Catchen ist irgendwie nicht möglich weil die Methode kein throws Exception Signatur hat.

Die getClass().getMethod("...").invoke(null) Methode wird schon mit ein Try Catch versorgt, auch wenn ich mit catch(Exception e) versorge, kann es dem UnsupportedOperationException nicht abfangen.
 
Kannst Du Deinen Code einmal zeigen?

Natürlich lässt sich die Exception fangen und in Deinem Code musst Du die Exception abfangen, denn die kommt in einer Checked Exception: InvocationTargetException wird geworfen mit der ursprünglich geworfenen Exception als innere Exception.

Das sieht dann z.B. so aus im StackTrace:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at reflection.ReflectionTest.main(ReflectionTest.java:14)
Caused by: java.lang.UnsupportedOperationException: Operation not supported!
at reflection.Test.throwException(Test.java:9)
 
Du erhältst eine InvocationTargetException, deren Cause die geworfene Exception ist.

Java:
import java.lang.reflect.*;

public class Test {
    public void fail() {
        throw new IllegalArgumentException("Failed");
    }

    public static void call(Object obj, String method) {
        if (obj == null) {
            System.out.println("obj was null. nothing to call.");
            return;
        }

        try {
            obj.getClass().getMethod(method).invoke(obj);
        } catch (NoSuchMethodException nsme) {
            System.out.printf("no such method '%s'\n", method);
            return;
        } catch (IllegalAccessException iae) {
            System.out.println("illegal access: " + iae.getMessage());
        } catch (InvocationTargetException ite) {
            System.out.println("invocation caused an exception.");
            Throwable th = ite.getCause();
            if (th != null) {
                System.out.printf("a %s was thrown: %s\n", 
                    th.getClass().getName(), th.getMessage());
                System.out.println("stack trace:");
                th.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Test test = new Test();
        Test.call(null, "");
        Test.call(test, "xyz");
        Test.call(test, "fail");
    }
}
 

Zurück
Oben