Example usage for java.lang.reflect InvocationTargetException getTargetException

List of usage examples for java.lang.reflect InvocationTargetException getTargetException

Introduction

In this page you can find the example usage for java.lang.reflect InvocationTargetException getTargetException.

Prototype

public Throwable getTargetException() 

Source Link

Document

Get the thrown target exception.

Usage

From source file:org.echocat.adam.profile.ExtendedPeopleDirectoryAction.java

@Nullable
protected <T> T invoke(@Nonnull Method method, @Nullable Object... arguments) {
    try {/*ww w  . j  a v  a2s. com*/
        // noinspection unchecked
        return (T) method.invoke(this, arguments);
    } catch (final InvocationTargetException e) {
        final Throwable cause = e.getTargetException();
        if (cause instanceof RuntimeException) {
            // noinspection ThrowInsideCatchBlockWhichIgnoresCaughtException
            throw (RuntimeException) cause;
        } else if (cause instanceof Error) {
            // noinspection ThrowInsideCatchBlockWhichIgnoresCaughtException
            throw (Error) cause;
        } else {
            throw new RuntimeException("Could not invoke " + method + ".", cause != null ? cause : e);
        }
    } catch (final Exception e) {
        throw new RuntimeException("Could not invoke " + method + ".", e);
    }
}

From source file:org.echocat.jomon.format.mylyn.MylynWikitextFormatter.java

protected void formatWith(@Nonnull Reader reader, @Nonnull Object markupParser) throws IOException {
    try {//from   ww  w  .j  av  a  2s  .c  o m
        _parse.invoke(markupParser, reader, false);
    } catch (final InvocationTargetException e) {
        final Throwable target = e.getTargetException();
        if (target instanceof IOException) {
            //noinspection ThrowInsideCatchBlockWhichIgnoresCaughtException
            throw (IOException) target;
        } else if (target instanceof RuntimeException) {
            //noinspection ThrowInsideCatchBlockWhichIgnoresCaughtException
            throw (RuntimeException) target;
        } else if (target instanceof Error) {
            //noinspection ThrowInsideCatchBlockWhichIgnoresCaughtException
            throw (Error) target;
        } else {
            throw new RuntimeException("Could not parse.", target != null ? target : e);
        }
    } catch (final Exception e) {
        throw new RuntimeException("Could not parse.", e);
    }
}

From source file:org.echocat.redprecursor.impl.sun.compilertree.SunNodeFactoryIntegrationTest.java

private void executeMethodIn(File classFile) throws Throwable {
    final URLClassLoader loader = new URLClassLoader(new URL[] { classFile.getParentFile().toURI().toURL() });
    final Class<?> testClass = loader.loadClass("Test");
    final Object test = testClass.newInstance();
    final Method callMethod = testClass.getMethod("call", String.class, Integer.class);
    try {/*from   ww  w  .j a  v  a 2s . co  m*/
        callMethod.invoke(test, new Object[] { "abc", 2 });
    } catch (InvocationTargetException e) {
        throw e.getTargetException();
    }
}

From source file:org.eclipse.equinox.servletbridge.FrameworkLauncher.java

/** start is used to "start" a previously deployed OSGi framework
 * The default behavior will read launcher.ini to create a set of initial properties and
 * use the "commandline" configuration parameter to create the equivalent command line arguments
 * available when starting Eclipse. //  w  w  w  .j a v a  2s . com
 */
public synchronized void start() {
    if (platformDirectory == null)
        throw new IllegalStateException("Could not start the Framework - (not deployed)"); //$NON-NLS-1$

    if (frameworkClassLoader != null) {
        context.log("Framework is already started"); //$NON-NLS-1$
        return;
    }

    Map initalPropertyMap = buildInitialPropertyMap();
    String[] args = buildCommandLineArguments();

    ClassLoader original = Thread.currentThread().getContextClassLoader();
    try {
        // MODIF_FROM_ORIGINAL begin
        /**
         * Setting this property will allow getting framework properties directly from System (e.g. we
         * need the <code>org.osgi.framework.os.name</code> property to be able to display tree nodes whose
         * paths are longer than 260 characters).
         * 
         * @see FrameworkProperties.internalGetProperties() 
         * 
         * @author Mariana
         */
        System.setProperty("osgi.framework.useSystemProperties", "true"); //$NON-NLS-1$ //$NON-NLS-2$
        // MODIF_FROM_ORIGINAL end

        URL[] osgiURLArray = { new URL((String) initalPropertyMap.get(OSGI_FRAMEWORK)) };
        frameworkClassLoader = new ChildFirstURLClassLoader(osgiURLArray, this.getClass().getClassLoader());
        Class clazz = frameworkClassLoader.loadClass(STARTER);

        Method setInitialProperties = clazz.getMethod("setInitialProperties", new Class[] { Map.class }); //$NON-NLS-1$
        setInitialProperties.invoke(null, new Object[] { initalPropertyMap });

        registerRestartHandler(clazz);

        Method runMethod = clazz.getMethod("startup", new Class[] { String[].class, Runnable.class }); //$NON-NLS-1$
        runMethod.invoke(null, new Object[] { args, null });

        frameworkContextClassLoader = Thread.currentThread().getContextClassLoader();
    } catch (InvocationTargetException ite) {
        Throwable t = ite.getTargetException();
        if (t == null)
            t = ite;
        context.log("Error while starting Framework", t); //$NON-NLS-1$
        throw new RuntimeException(t.getMessage());
    } catch (Exception e) {
        context.log("Error while starting Framework", e); //$NON-NLS-1$
        throw new RuntimeException(e.getMessage());
    } finally {
        Thread.currentThread().setContextClassLoader(original);
    }
}

From source file:org.eclipse.equinox.servletbridge.FrameworkLauncher.java

private Runnable createRestartHandler() throws ClassNotFoundException, NoSuchMethodException {
    Class frameworkPropertiesClazz = frameworkClassLoader.loadClass(FRAMEWORKPROPERTIES);
    final Method getProperty = frameworkPropertiesClazz.getMethod("getProperty", new Class[] { String.class }); //$NON-NLS-1$
    Runnable restartHandler = new Runnable() {
        public void run() {
            try {
                String forcedRestart = (String) getProperty.invoke(null, new Object[] { OSGI_FORCED_RESTART });
                if (Boolean.valueOf(forcedRestart).booleanValue()) {
                    stop();/* w  w w  .j av a 2  s . c  o m*/
                    start();
                }
            } catch (InvocationTargetException ite) {
                Throwable t = ite.getTargetException();
                if (t == null)
                    t = ite;
                throw new RuntimeException(t.getMessage());
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
            }
        }
    };
    return restartHandler;
}

From source file:org.eclipse.gemini.blueprint.compendium.internal.cm.ManagedFactoryDisposableInvoker.java

private void invokeMethod(Method method, Object[] args, String targetName, Object target) {
    try {/*from w  w  w  . j av  a2s . com*/
        method.invoke(target, args);
    } catch (InvocationTargetException ex) {
        String msg = "Invocation of destroy method '" + method.getName() + "' failed on bean with name '"
                + targetName + "'";
        if (log.isDebugEnabled()) {
            log.warn(msg, ex.getTargetException());
        } else {
            log.warn(msg + ": " + ex.getTargetException());
        }
    } catch (Throwable ex) {
        log.error("Couldn't invoke destroy method '" + method.getName() + "' on bean with name '" + targetName
                + "'", ex);
    }
}

From source file:org.eclipse.jubula.rc.common.commands.AbstractCapTestCommand.java

/**
 * calls the method of the implementation class per reflection
 * {@inheritDoc}//from w  w  w  . jav a 2s . c  om
 */
public Message execute() {
    final int oldMode = AUTServer.getInstance().getMode();
    TestErrorEvent event = null;
    CAPTestResponseMessage response = new CAPTestResponseMessage();
    if (oldMode != ChangeAUTModeMessage.TESTING) {
        AUTServer.getInstance().setMode(ChangeAUTModeMessage.TESTING);
    }
    try {
        response.setMessageCap(m_capTestMessage.getMessageCap());

        // get the implementation class
        Object implClass = getImplClass(response);
        if (implClass == null) {
            return response;
        }
        MethodInvoker invoker = new MethodInvoker(m_capTestMessage.getMessageCap());
        Object returnValue = invoker.invoke(implClass);
        response.setReturnValue((String) returnValue);
    } catch (NoSuchMethodException nsme) {
        LOG.error("implementation class method not found", nsme); //$NON-NLS-1$
        event = EventFactory.createUnsupportedActionError();
    } catch (IllegalAccessException iae) {
        LOG.error("Failed accessing implementation class method", iae); //$NON-NLS-1$
        event = EventFactory.createConfigErrorEvent();
    } catch (InvocationTargetException ite) {
        if (ite.getTargetException() instanceof EventSupportException) {
            EventSupportException e = (EventSupportException) ite.getTargetException();
            event = e.getEvent();
            if (LOG.isDebugEnabled()) {
                LOG.debug(e.getLocalizedMessage(), e);
            }
        } else if (ite.getTargetException() instanceof ExecutionEvent) {
            ExecutionEvent e = (ExecutionEvent) ite.getTargetException();
            response.setState(e.getEvent());
            if (LOG.isDebugEnabled()) {
                LOG.debug(e.getLocalizedMessage(), e);
            }
        } else {
            event = EventFactory.createConfigErrorEvent();
            if (LOG.isErrorEnabled()) {
                LOG.error("InvocationTargetException: ", ite); //$NON-NLS-1$
                LOG.error("TargetException: ", ite.getTargetException()); //$NON-NLS-1$
            }
        }
    } catch (IllegalArgumentException e) {
        LOG.error(e.getLocalizedMessage(), e);
    } catch (MethodParamException e) {
        LOG.error(e.getLocalizedMessage(), e);
    } finally {
        if (AUTServer.getInstance().getMode() != oldMode) {
            AUTServer.getInstance().setMode(oldMode);
        }
    }
    if (event != null) {
        response.setTestErrorEvent(event);
    }
    if (m_capTestMessage.isRequestAnswer()) {
        return response;
    }
    return null;
}

From source file:org.eclipse.jubula.rc.common.commands.CapTestCommand.java

/**
 * calls the method of the implementation class per reflection
 * {@inheritDoc}//from  w w  w .j av  a  2  s.  c o  m
 */
public Message execute() {
    AUTServer autServer = AUTServer.getInstance();
    final int oldMode = autServer.getMode();
    TestErrorEvent event = null;
    CAPTestResponseMessage response = new CAPTestResponseMessage();
    if (oldMode != ChangeAUTModeMessage.TESTING) {
        autServer.setMode(ChangeAUTModeMessage.TESTING);
    }
    try {
        MessageCap messageCap = m_capTestMessage.getMessageCap();
        response.setMessageCap(messageCap);

        // get the implementation class
        Object implClass = getImplClass(response);
        if (implClass == null) {
            return response;
        }
        MethodInvoker invoker = new MethodInvoker(messageCap);
        Object returnValue = invoker.invoke(implClass);
        response.setReturnValue((String) returnValue);
    } catch (NoSuchMethodException nsme) {
        LOG.error("implementation class method not found", nsme); //$NON-NLS-1$
        event = EventFactory.createUnsupportedActionError();
    } catch (IllegalAccessException iae) {
        LOG.error("Failed accessing implementation class method", iae); //$NON-NLS-1$
        event = EventFactory.createConfigErrorEvent();
    } catch (InvocationTargetException ite) {
        if (ite.getTargetException() instanceof EventSupportException) {
            EventSupportException e = (EventSupportException) ite.getTargetException();
            event = e.getEvent();
            if (LOG.isDebugEnabled()) {
                LOG.debug(e.getLocalizedMessage(), e);
            }
        } else if (ite.getTargetException() instanceof ExecutionEvent) {
            ExecutionEvent e = (ExecutionEvent) ite.getTargetException();
            response.setState(e.getEvent());
            if (LOG.isDebugEnabled()) {
                LOG.debug(e.getLocalizedMessage(), e);
            }
        } else {
            event = EventFactory.createConfigErrorEvent();
            if (LOG.isErrorEnabled()) {
                LOG.error("InvocationTargetException: ", ite); //$NON-NLS-1$
                LOG.error("TargetException: ", ite.getTargetException()); //$NON-NLS-1$
            }
        }
    } catch (IllegalArgumentException e) {
        LOG.error(e.getLocalizedMessage(), e);
    } catch (MethodParamException e) {
        LOG.error(e.getLocalizedMessage(), e);
    } finally {
        if (autServer.getMode() != oldMode) {
            autServer.setMode(oldMode);
        }
    }
    if (event != null) {
        response.setTestErrorEvent(event);
    }
    if (m_capTestMessage.isRequestAnswer()) {
        return response;
    }
    return null;
}

From source file:org.eclipse.jubula.rc.swing.driver.EventThreadQueuerAwtImpl.java

/** {@inheritDoc} */
public Object invokeAndWait(String name, IRunnable runnable) throws StepExecutionException {

    Validate.notNull(runnable, "runnable must not be null"); //$NON-NLS-1$

    RunnableWrapper wrapper = new RunnableWrapper(name, runnable);
    try {/*w w  w .j  a v a2 s .c  o  m*/
        if (SwingUtilities.isEventDispatchThread()) {
            wrapper.run();
        } else {
            SwingUtilities.invokeAndWait(wrapper);
        }

        StepExecutionException exception = wrapper.getException();
        if (exception != null) {
            throw new InvocationTargetException(exception);
        }
    } catch (InterruptedException ie) {
        // this (the waiting) thread was interrupted -> error
        log.error(ie);
        throw new StepExecutionException(ie);
    } catch (InvocationTargetException ite) {
        // the run() method from IRunnable has thrown an exception
        // -> log on info
        // -> throw a StepExecutionException
        Throwable thrown = ite.getTargetException();
        if (thrown instanceof StepExecutionException) {
            if (log.isInfoEnabled()) {
                log.info(ite);
            }
            throw (StepExecutionException) thrown;
        }

        // any other (unchecked) Exception from IRunnable.run()
        log.error("exception thrown by '" + wrapper.getName() //$NON-NLS-1$
                + "':", thrown); //$NON-NLS-1$
        throw new StepExecutionException(thrown);
    }

    return wrapper.getResult();
}

From source file:org.eclipse.jubula.rc.swt.driver.EventThreadQueuerSwtImpl.java

/** {@inheritDoc} */
public Object invokeAndWait(String name, IRunnable runnable)
        throws IllegalArgumentException, StepExecutionException {

    Validate.notNull(runnable, "runnable must not be null"); //$NON-NLS-1$
    RunnableWrapper wrapper = new RunnableWrapper(name, runnable);
    try {/*from   w  w w.  j  a  v a2  s .  c o  m*/
        Display display = getDisplay();
        if (display.isDisposed()) {
            // this may happen e.g. during the shutdown process of the AUT
            // see http://bugzilla.bredex.de/907 for additional information
            log.warn("Display has already been disposed - skipping IRunnable invocation!"); //$NON-NLS-1$
            return null;
        }
        display.syncExec(wrapper);
        StepExecutionException exception = wrapper.getException();
        if (exception != null) {
            throw new InvocationTargetException(exception);
        }
    } catch (InvocationTargetException ite) {
        // the run() method from IRunnable has thrown an exception
        // -> log on info
        // -> throw a StepExecutionException
        Throwable thrown = ite.getTargetException();
        if (thrown instanceof StepExecutionException) {
            if (log.isInfoEnabled()) {
                log.info(ite);
            }
            throw (StepExecutionException) thrown;
        }
        // any other (unchecked) Exception from IRunnable.run()
        log.error("exception thrown by '" + wrapper.getName() //$NON-NLS-1$
                + "':", thrown); //$NON-NLS-1$
        throw new StepExecutionException(thrown);
    }
    return wrapper.getResult();
}