java.lang.reflect.InvocationTargetException.getTargetException()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(363)

本文整理了Java中java.lang.reflect.InvocationTargetException.getTargetException()方法的一些代码示例,展示了InvocationTargetException.getTargetException()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。InvocationTargetException.getTargetException()方法的具体详情如下:
包路径:java.lang.reflect.InvocationTargetException
类名称:InvocationTargetException
方法名:getTargetException

InvocationTargetException.getTargetException介绍

[英]Returns the target exception, which may be null.
[中]返回目标异常,该异常可能为空。

代码示例

代码示例来源:origin: apache/incubator-dubbo

private Object resolve(Object obj)
  throws Exception {
  // if there's a readResolve method, call it
  try {
    if (_readResolve != null)
      return _readResolve.invoke(obj, new Object[0]);
  } catch (InvocationTargetException e) {
    if (e.getTargetException() != null)
      throw e;
  }
  return obj;
}

代码示例来源:origin: jenkinsci/jenkins

public Object call(Object instance) throws Exception {
  try {
    return method.invoke(instance,arguments);
  } catch (InvocationTargetException e) {
    Throwable t = e.getTargetException();
    if (t instanceof Exception)
      throw (Exception) t;
    throw e;
  }
}

代码示例来源:origin: jenkinsci/jenkins

private IOException handleInvocationTargetException(InvocationTargetException e) {
  Throwable t = e.getTargetException();
  if (t instanceof Error) {
    throw (Error) t;
  }
  if (t instanceof RuntimeException) {
    throw (RuntimeException) t;
  }
  if (t instanceof IOException) {
    return (IOException) t;
  }
  throw new Error(t);
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Perform the invocation and convert InvocationTargetException
 * into the underlying target exception.
 */
@Nullable
protected Object invokeWithTargetException() throws Exception {
  try {
    return invoke();
  }
  catch (InvocationTargetException ex) {
    if (ex.getTargetException() instanceof Exception) {
      throw (Exception) ex.getTargetException();
    }
    if (ex.getTargetException() instanceof Error) {
      throw (Error) ex.getTargetException();
    }
    throw ex;
  }
}

代码示例来源:origin: spring-projects/spring-framework

private void invokeHandlerMethod(MethodInvocation mi, Throwable ex, Method method) throws Throwable {
  Object[] handlerArgs;
  if (method.getParameterCount() == 1) {
    handlerArgs = new Object[] {ex};
  }
  else {
    handlerArgs = new Object[] {mi.getMethod(), mi.getArguments(), mi.getThis(), ex};
  }
  try {
    method.invoke(this.throwsAdvice, handlerArgs);
  }
  catch (InvocationTargetException targetEx) {
    throw targetEx.getTargetException();
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Handle the given invocation target exception. Should only be called if no
 * checked exception is expected to be thrown by the target method.
 * <p>Throws the underlying RuntimeException or Error in case of such a root
 * cause. Throws an UndeclaredThrowableException otherwise.
 * @param ex the invocation target exception to handle
 */
public static void handleInvocationTargetException(InvocationTargetException ex) {
  rethrowRuntimeException(ex.getTargetException());
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void run() {
  try {
    ReflectionUtils.makeAccessible(this.method);
    this.method.invoke(this.target);
  }
  catch (InvocationTargetException ex) {
    ReflectionUtils.rethrowRuntimeException(ex.getTargetException());
  }
  catch (IllegalAccessException ex) {
    throw new UndeclaredThrowableException(ex);
  }
}

代码示例来源:origin: jenkinsci/jenkins

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
      return call(object,method,args);
    } catch (InvocationTargetException e) {
      throw e.getTargetException();
    }
  }
}));

代码示例来源:origin: spring-projects/spring-framework

@Nullable
private static Object invokeJdbcMethod(Method method, @Nullable Object target, @Nullable Object... args)
    throws SQLException {
  try {
    return method.invoke(target, args);
  }
  catch (IllegalAccessException ex) {
    ReflectionUtils.handleReflectionException(ex);
  }
  catch (InvocationTargetException ex) {
    if (ex.getTargetException() instanceof SQLException) {
      throw (SQLException) ex.getTargetException();
    }
    ReflectionUtils.handleInvocationTargetException(ex);
  }
  throw new IllegalStateException("Should never get here");
}

代码示例来源:origin: spring-projects/spring-framework

private void invokePortableRemoteObject(@Nullable Method method) throws RemoteException {
  if (method != null) {
    try {
      method.invoke(null, this.exportedObject);
    }
    catch (InvocationTargetException ex) {
      Throwable targetEx = ex.getTargetException();
      if (targetEx instanceof RemoteException) {
        throw (RemoteException) targetEx;
      }
      ReflectionUtils.rethrowRuntimeException(targetEx);
    }
    catch (Throwable ex) {
      throw new IllegalStateException("PortableRemoteObject invocation failed", ex);
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
  LifecycleMetadata metadata = findLifecycleMetadata(bean.getClass());
  try {
    metadata.invokeDestroyMethods(bean, beanName);
  }
  catch (InvocationTargetException ex) {
    String msg = "Destroy method on bean with name '" + beanName + "' threw an exception";
    if (logger.isDebugEnabled()) {
      logger.warn(msg, ex.getTargetException());
    }
    else {
      logger.warn(msg + ": " + ex.getTargetException());
    }
  }
  catch (Throwable ex) {
    logger.warn("Failed to invoke destroy method on bean with name '" + beanName + "'", ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  LifecycleMetadata metadata = findLifecycleMetadata(bean.getClass());
  try {
    metadata.invokeInitMethods(bean, beanName);
  }
  catch (InvocationTargetException ex) {
    throw new BeanCreationException(beanName, "Invocation of init method failed", ex.getTargetException());
  }
  catch (Throwable ex) {
    throw new BeanCreationException(beanName, "Failed to invoke init method", ex);
  }
  return bean;
}

代码示例来源:origin: spring-projects/spring-framework

private void setThreadCallbacks(Callback[] callbacks) {
    try {
      setThreadCallbacks.invoke(generatedClass, (Object) callbacks);
    }
    catch (IllegalAccessException e) {
      throw new CodeGenerationException(e);
    }
    catch (InvocationTargetException e) {
      throw new CodeGenerationException(e.getTargetException());
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

protected static Object invokeVfsMethod(Method method, @Nullable Object target, Object... args) throws IOException {
  try {
    return method.invoke(target, args);
  }
  catch (InvocationTargetException ex) {
    Throwable targetEx = ex.getTargetException();
    if (targetEx instanceof IOException) {
      throw (IOException) targetEx;
    }
    ReflectionUtils.handleInvocationTargetException(ex);
  }
  catch (Exception ex) {
    ReflectionUtils.handleReflectionException(ex);
  }
  throw new IllegalStateException("Invalid code path reached");
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
protected TransactionManager retrieveTransactionManager() throws TransactionSystemException {
  Object helper = loadWebLogicTransactionHelper();
  try {
    logger.trace("Retrieving JTA TransactionManager from WebLogic TransactionHelper");
    Method getTransactionManagerMethod = helper.getClass().getMethod("getTransactionManager");
    return (TransactionManager) getTransactionManagerMethod.invoke(this.transactionHelper);
  }
  catch (InvocationTargetException ex) {
    throw new TransactionSystemException(
        "WebLogic's TransactionHelper.getTransactionManager() method failed", ex.getTargetException());
  }
  catch (Exception ex) {
    throw new TransactionSystemException(
        "Could not invoke WebLogic's TransactionHelper.getTransactionManager() method", ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
protected UserTransaction retrieveUserTransaction() throws TransactionSystemException {
  Object helper = loadWebLogicTransactionHelper();
  try {
    logger.trace("Retrieving JTA UserTransaction from WebLogic TransactionHelper");
    Method getUserTransactionMethod = helper.getClass().getMethod("getUserTransaction");
    return (UserTransaction) getUserTransactionMethod.invoke(this.transactionHelper);
  }
  catch (InvocationTargetException ex) {
    throw new TransactionSystemException(
        "WebLogic's TransactionHelper.getUserTransaction() method failed", ex.getTargetException());
  }
  catch (Exception ex) {
    throw new TransactionSystemException(
        "Could not invoke WebLogic's TransactionHelper.getUserTransaction() method", ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void run() {
  try {
    invoke();
  }
  catch (InvocationTargetException ex) {
    logger.error(getInvocationFailureMessage(), ex.getTargetException());
    // Do not throw exception, else the main loop of the scheduler might stop!
  }
  catch (Throwable ex) {
    logger.error(getInvocationFailureMessage(), ex);
    // Do not throw exception, else the main loop of the scheduler might stop!
  }
}

代码示例来源:origin: spring-projects/spring-framework

protected Object invokeAdviceMethodWithGivenArgs(Object[] args) throws Throwable {
  Object[] actualArgs = args;
  if (this.aspectJAdviceMethod.getParameterCount() == 0) {
    actualArgs = null;
  }
  try {
    ReflectionUtils.makeAccessible(this.aspectJAdviceMethod);
    // TODO AopUtils.invokeJoinpointUsingReflection
    return this.aspectJAdviceMethod.invoke(this.aspectInstanceFactory.getAspectInstance(), actualArgs);
  }
  catch (IllegalArgumentException ex) {
    throw new AopInvocationException("Mismatch on arguments to advice method [" +
        this.aspectJAdviceMethod + "]; pointcut expression [" +
        this.pointcut.getPointcutExpression() + "]", ex);
  }
  catch (InvocationTargetException ex) {
    throw ex.getTargetException();
  }
}

代码示例来源:origin: google/guava

@Override
 protected void runTest() throws Throwable {
  Monitor monitor = new Monitor(fair);
  FlagGuard guard = new FlagGuard(monitor);
  Object[] arguments =
    (timed ? new Object[] {guard, 0L, TimeUnit.MILLISECONDS} : new Object[] {guard});
  try {
   method.invoke(monitor, arguments);
   fail("expected IllegalMonitorStateException");
  } catch (InvocationTargetException e) {
   assertEquals(IllegalMonitorStateException.class, e.getTargetException().getClass());
  }
 }
};

代码示例来源:origin: spring-projects/spring-framework

private void doTest(Method rsetMethod, Method rowsetMethod, Object arg, Object ret) throws Exception {
  if (arg instanceof String) {
    given(resultSet.findColumn((String) arg)).willReturn(1);
    given(rsetMethod.invoke(resultSet, 1)).willReturn(ret).willThrow(new SQLException("test"));
  }
  else {
    given(rsetMethod.invoke(resultSet, arg)).willReturn(ret).willThrow(new SQLException("test"));
  }
  rowsetMethod.invoke(rowSet, arg);
  try {
    rowsetMethod.invoke(rowSet, arg);
    fail("InvalidResultSetAccessException should have been thrown");
  }
  catch (InvocationTargetException ex) {
    assertEquals(InvalidResultSetAccessException.class, ex.getTargetException().getClass());
  }
}

相关文章