Example usage for java.lang.reflect InvocationTargetException getCause

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

Introduction

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

Prototype

public Throwable getCause() 

Source Link

Document

Returns the cause of this exception (the thrown target exception, which may be null ).

Usage

From source file:ai.general.net.MethodHandler.java

/**
 * Handles the request by calling the method represented by this handler.
 * Request parameters are converted to match the method signature if possible. If no conversion
 * is possible, the method is not called.
 *
 * @param request The request to handle.
 *//*  www  .  j  a va  2s. c  o  m*/
public void handle(Request request) {
    log.entry(request.getUri().toString());
    try {
        Object[] raw_args = request.getArguments().toArray();
        if (raw_args.length != parameter_types_.length) {
            request.getResult()
                    .addError(new Result.Error("invalid number of method arguments", "got " + raw_args.length
                            + " arguments for method with " + parameter_types_.length + " arguments"));
            log.exit("invalid number of arguments");
            return;
        }
        Object[] args = new Object[raw_args.length];
        for (int i = 0; i < raw_args.length; i++) {
            args[i] = json_parser_.convertValue(raw_args[i], parameter_types_[i]);
        }
        Object result = method_.invoke(instance_, args);
        if (result != null) {
            request.getResult().addValue(result);
        }
    } catch (InvocationTargetException e) {
        log.catching(Level.TRACE, e);
        Throwable cause = e.getCause();
        if (cause != null) {
            if (cause instanceof RpcException) {
                request.getResult()
                        .addError(new Result.Error(cause.getMessage(), ((RpcException) cause).getDetails()));
            } else {
                request.getResult().addError(new Result.Error(cause.getClass().getName(), cause.getMessage()));
            }
        } else {
            request.getResult().addError(new Result.Error("unspecified exception thrown by RPC method", null));
        }
    } catch (ReflectiveOperationException e) {
        log.catching(Level.TRACE, e);
        request.getResult().addError(new Result.Error("cannot call method with specified arguments", null));
    } catch (Exception e) {
        log.catching(Level.TRACE, e);
        request.getResult().addError(new Result.Error(e.getClass().getName(), e.getMessage()));
    }
    log.exit();
}

From source file:de.thkwalter.et.ortskurve.OrtskurveControllerTest.java

/**
 * Test fr die Methode {@link OrtskurveController#messpunkteValidieren(Vector2D[])}.
 * //from  w  w  w .j  a va2  s.c o m
 * @throws Throwable 
 */
@Test(expected = ApplicationRuntimeException.class)
public void testMesspunkteValidieren1() throws Throwable {
    // Die in diesem Test verwendeten Messpunkte werden erzeugt.
    Vector2D[] testMesspunkte = new Vector2D[] { new Vector2D(1.0, 0.0), new Vector2D(3.0, 0.0),
            new Vector2D(1, 0) };

    try {
        // Die zu testende Methode wird aufgerufen.
        Method methode = OrtskurveController.class.getDeclaredMethod("messpunkteValidieren", Vector2D[].class);
        methode.setAccessible(true);
        methode.invoke(this.ortskurveController, (Object) testMesspunkte);
    }

    // Die InvocationTargetException wird gefangen und die ursprngliche Ausnahme weitergeworfen.
    catch (InvocationTargetException invocationTargetException) {
        throw invocationTargetException.getCause();
    }
}

From source file:org.tango.server.attribute.ReflectAttributeBehavior.java

private void throwDevFailed(final InvocationTargetException e) throws DevFailed {
    if (e.getCause() instanceof DevFailed) {
        throw (DevFailed) e.getCause();
    } else {/*  w w  w  .j av a 2s.co  m*/
        DevFailedUtils.throwDevFailed(e.getCause());
    }
}

From source file:com.anrisoftware.prefdialog.miscswing.dialogsworker.AbstractCreateDialogWorker.java

private DialogType doCreateDialog() throws CreateDialogWorkerException, CreateDialogInterrupedException {
    try {//w  w  w. jav a2  s.c  o m
        return createDialogAWT();
    } catch (InvocationTargetException e) {
        throw new CreateDialogWorkerException(e.getCause());
    } catch (InterruptedException e) {
        throw new CreateDialogInterrupedException(e);
    }
}

From source file:com.goncalomb.bukkit.mylib.command.MySubCommand.java

private boolean invokeExeMethod(CommandSender sender, String[] args) {
    try {// w ww.  j a v a 2  s .  c  om
        return (Boolean) _exeMethod.invoke(_command, sender, args);
    } catch (InvocationTargetException e) {
        if (e.getCause() instanceof MyCommandException) {
            sender.sendMessage(e.getCause().getMessage());
            return true;
        } else {
            throw new RuntimeException(e.getCause());
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.goncalomb.bukkit.bkglib.bkgcommand.BKgSubCommand.java

private boolean invokeExeMethod(CommandSender sender, String[] args) {
    try {//  ww  w .ja  v  a 2  s  . com
        return (Boolean) _exeMethod.invoke(_command, sender, args);
    } catch (InvocationTargetException e) {
        if (e.getCause() instanceof BKgCommandException) {
            sender.sendMessage(e.getCause().getMessage());
            return true;
        } else {
            throw new RuntimeException(e.getCause());
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.liferay.arquillian.maven.internal.tasks.ExecuteDeployerTask.java

protected void executeTool(String deployerClassName, ClassLoader classLoader, String[] args) throws Exception {

    Thread currentThread = Thread.currentThread();

    ClassLoader contextClassLoader = currentThread.getContextClassLoader();

    currentThread.setContextClassLoader(classLoader);

    SecurityManager currentSecurityManager = System.getSecurityManager();

    // Required to prevent premature exit by DBBuilder. See LPS-7524.

    SecurityManager securityManager = new SecurityManager() {

        @Override// www.  j  ava2 s  .c o  m
        public void checkPermission(Permission permission) {
            //It is not needed to check permissions
        }

        @Override
        public void checkExit(int status) {
            throw new SecurityException();
        }
    };

    System.setSecurityManager(securityManager);

    try {
        System.setProperty("external-properties",
                "com/liferay/portal/tools/dependencies" + "/portal-tools.properties");
        System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Log4JLogger");

        Class<?> clazz = classLoader.loadClass(deployerClassName);

        Method method = clazz.getMethod("main", String[].class);

        method.invoke(null, (Object) args);
    } catch (InvocationTargetException ite) {
        if (!(ite.getCause() instanceof SecurityException)) {
            throw ite;
        }
    } finally {
        currentThread.setContextClassLoader(contextClassLoader);

        System.setSecurityManager(currentSecurityManager);
    }
}

From source file:org.kaleidofoundry.spring.context.BeanContextPostProcessor.java

@Override
public Object postProcessBeforeInitialization(final Object beanInstance, final String beanName)
        throws BeansException {

    Set<Field> fields = ReflectionHelper.getAllDeclaredFields(beanInstance.getClass());

    for (Field field : fields) {
        // @Autowired is injected using spring bean
        if (field.isAnnotationPresent(Context.class) && !field.isAnnotationPresent(Autowired.class)
                && !field.isAnnotationPresent(Inject.class)) {

            final Context context = field.getAnnotation(Context.class);
            // do field is a runtime context class
            if (field.getType().isAssignableFrom(RuntimeContext.class)) {
                ReflectionUtils.makeAccessible(field);
                ReflectionUtils.setField(field, beanInstance,
                        RuntimeContext.createFrom(context, field.getName(), field.getDeclaringClass()));
            }//w ww  .j av  a  2s  .  c o  m
            // does the plugin interface have a provider specify
            else if (field.getType().isAnnotationPresent(Provider.class)) {

                try {
                    ReflectionUtils.makeAccessible(field);
                    Object fieldValue = field.get(beanInstance);

                    if (fieldValue == null) {
                        // create provider using annotation meta-information
                        final Provider provideInfo = field.getType().getAnnotation(Provider.class);
                        final Constructor<? extends ProviderService<?>> providerConstructor = provideInfo
                                .value().getConstructor(Class.class);
                        final ProviderService<?> fieldProviderInstance = providerConstructor
                                .newInstance(field.getType());

                        // invoke provides method with Context annotation meta-informations
                        final Method providesMethod = ReflectionUtils.findMethod(provideInfo.value(),
                                ProviderService.PROVIDES_METHOD, Context.class, String.class, Class.class);
                        // get the provider result
                        fieldValue = ReflectionUtils.invokeMethod(providesMethod, fieldProviderInstance,
                                context, field.getName(), field.getType());
                        // set the field that was not yet injected
                        ReflectionUtils.setField(field, beanInstance, fieldValue);
                    }

                } catch (IllegalArgumentException e) {
                    throw new BeanCreationException("Unexpected error during injection", e);
                } catch (IllegalAccessException e) {
                    throw new BeanCreationException("Unexpected error during injection", e);
                } catch (SecurityException e) {
                    throw new BeanCreationException("Unexpected error during injection", e);
                } catch (NoSuchMethodException e) {
                    throw new BeanCreationException("Unexpected error during injection", e);
                } catch (InstantiationException e) {
                    throw new BeanCreationException("Unexpected error during injection", e);
                } catch (InvocationTargetException ite) {
                    throw new BeanCreationException("", ite.getCause() != null ? ite.getCause()
                            : (ite.getTargetException() != null ? ite.getTargetException() : ite));
                } finally {

                }
            }
        }
    }

    return beanInstance;
}

From source file:org.gradle.model.internal.method.WeaklyTypeReferencingMethod.java

public R invoke(T target, Object... args) {
    Method method = getMethod();/*from   w w  w  . j  a  v a2 s .co  m*/
    method.setAccessible(true);
    try {
        Object result = method.invoke(target, args);
        return returnType.getConcreteClass().cast(result);
    } catch (InvocationTargetException e) {
        throw UncheckedException.throwAsUncheckedException(e.getCause());
    } catch (Exception e) {
        throw new GradleException(String.format("Could not call %s.%s() on %s",
                method.getDeclaringClass().getSimpleName(), method.getName(), target), e);
    }
}

From source file:org.jboss.aerogear.cordova.oauth2.OauthGoogleServicesIntentHelper.java

private void getToken(final String accountName) {
    Runnable runnable = new Runnable() {
        public void run() {
            String token;//from w w  w . j  a va  2s  . c om
            try {
                Log.i(TAG, "Retrieving token for: " + accountName);
                Log.i(TAG, "with scope(s): " + scopes);
                token = (String) METHOD_getToken.invoke(null, cordova.getActivity(), accountName, scopes);
                callbackContext.success(token);
            } catch (InvocationTargetException ite) {
                Throwable userRecoverableException = ite.getCause();
                if (CLASS_UserRecoverableAuthException != null
                        && CLASS_UserRecoverableAuthException.isInstance(userRecoverableException)) {
                    try {
                        Intent intent = (Intent) METHOD_getIntent.invoke(userRecoverableException);
                        Log.e(TAG, "UserRecoverableAuthException: Attempting recovery...");
                        cordova.getActivity().startActivityForResult(intent, REQUEST_AUTHORIZATION);
                    } catch (IllegalAccessException e) {
                        Log.i(TAG, "error" + e.getMessage());
                        callbackContext.error("plugin failed to get token: " + e.getMessage());
                    } catch (InvocationTargetException e) {
                        Log.i(TAG, "error" + e.getCause().getMessage());
                        callbackContext.error("plugin failed to get token: " + e.getCause().getMessage());
                    }
                }
            } catch (Exception e) {
                Log.i(TAG, "error" + e.getMessage());
                callbackContext.error("plugin failed to get token: " + e.getMessage());
            }
        }
    };
    cordova.getThreadPool().execute(runnable);
}