java.lang.reflect.InvocationTargetException类的使用及代码示例

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

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

InvocationTargetException介绍

[英]This class provides a wrapper for an exception thrown by a Method or Constructor invocation.
[中]此类为方法或构造函数调用引发的异常提供包装。

代码示例

代码示例来源:origin: redisson/redisson

public static Exception extractTargetException(InvocationTargetException itex) {
  Throwable target = itex.getTargetException();
  return target instanceof Exception ? (Exception) target : itex;
}

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

private Object invokeGeneratorMethod(Method generator, Object... args) {
 try {
  return generator.invoke(this, args);
 } catch (InvocationTargetException e) {
  throwIfUnchecked(e.getCause());
  throw new RuntimeException(e.getCause());
 } catch (Exception e) {
  throwIfUnchecked(e);
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: square/otto

/**
 * Throw a {@link RuntimeException} with given message and cause lifted from an {@link
 * InvocationTargetException}. If the specified {@link InvocationTargetException} does not have a
 * cause, neither will the {@link RuntimeException}.
 */
private static void throwRuntimeException(String msg, InvocationTargetException e) {
 Throwable cause = e.getCause();
 if (cause != null) {
  throw new RuntimeException(msg + ": " + cause.getMessage(), cause);
 } else {
  throw new RuntimeException(msg + ": " + e.getMessage(), e);
 }
}

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

private static <T> void callAllPublicMethods(TypeToken<T> type, T object)
   throws InvocationTargetException {
  for (Method method : type.getRawType().getMethods()) {
   if ((method.getModifiers() & STATIC) != 0) {
    continue;
   }
   ImmutableList<Parameter> parameters = type.method(method).getParameters();
   Object[] args = new Object[parameters.size()];
   for (int i = 0; i < parameters.size(); i++) {
    args[i] = getDefaultValue(parameters.get(i).getType());
   }
   try {
    try {
     method.invoke(object, args);
    } catch (InvocationTargetException ex) {
     try {
      throw ex.getCause();
     } catch (UnsupportedOperationException unsupported) {
      // this is a legit exception
     }
    }
   } catch (Throwable cause) {
    throw new InvocationTargetException(
      cause, method + " with args: " + Arrays.toString(args));
   }
  }
 }
}

代码示例来源:origin: shuzheng/zheng

@Override
public int updateByExampleWithBLOBs(@Param("record") Record record, @Param("example") Example example) {
  try {
    DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
    Method updateByExampleWithBLOBs = mapper.getClass().getDeclaredMethod("updateByExampleWithBLOBs", record.getClass(), example.getClass());
    Object result = updateByExampleWithBLOBs.invoke(mapper, record, example);
    return Integer.parseInt(String.valueOf(result));
  } catch (IllegalAccessException e) {
    e.printStackTrace();
  } catch (InvocationTargetException e) {
    e.printStackTrace();
  } catch (NoSuchMethodException e) {
    e.printStackTrace();
  }
  DynamicDataSource.clearDataSource();
  return 0;
}

代码示例来源:origin: square/okhttp

@SuppressWarnings({"unchecked", "SuspiciousToArrayCall"}) // Reflection on List<Certificate>.
@Override public List<Certificate> clean(List<Certificate> chain, String hostname)
  throws SSLPeerUnverifiedException {
 try {
  X509Certificate[] certificates = chain.toArray(new X509Certificate[chain.size()]);
  return (List<Certificate>) checkServerTrusted.invoke(
    x509TrustManagerExtensions, certificates, "RSA", hostname);
 } catch (InvocationTargetException e) {
  SSLPeerUnverifiedException exception = new SSLPeerUnverifiedException(e.getMessage());
  exception.initCause(e);
  throw exception;
 } catch (IllegalAccessException e) {
  throw new AssertionError(e);
 }
}

代码示例来源:origin: shuzheng/zheng

@Override
public int updateByExampleSelective(@Param("record") Record record, @Param("example") Example example) {
  try {
    DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
    Method updateByExampleSelective = mapper.getClass().getDeclaredMethod("updateByExampleSelective", record.getClass(), example.getClass());
    Object result = updateByExampleSelective.invoke(mapper, record, example);
    return Integer.parseInt(String.valueOf(result));
  } catch (IllegalAccessException e) {
    e.printStackTrace();
  } catch (InvocationTargetException e) {
    e.printStackTrace();
  } catch (NoSuchMethodException e) {
    e.printStackTrace();
  }
  DynamicDataSource.clearDataSource();
  return 0;
}

代码示例来源:origin: prestodb/presto

@SuppressWarnings({"unchecked", "SuspiciousToArrayCall"}) // Reflection on List<Certificate>.
@Override public List<Certificate> clean(List<Certificate> chain, String hostname)
  throws SSLPeerUnverifiedException {
 try {
  X509Certificate[] certificates = chain.toArray(new X509Certificate[chain.size()]);
  return (List<Certificate>) checkServerTrusted.invoke(
    x509TrustManagerExtensions, certificates, "RSA", hostname);
 } catch (InvocationTargetException e) {
  SSLPeerUnverifiedException exception = new SSLPeerUnverifiedException(e.getMessage());
  exception.initCause(e);
  throw exception;
 } catch (IllegalAccessException e) {
  throw new AssertionError(e);
 }
}

代码示例来源: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: spring-projects/spring-framework

@Override
public void addTransformer(ClassFileTransformer transformer) {
  try {
    this.addTransformerMethod.invoke(this.classLoader, transformer);
  }
  catch (InvocationTargetException ex) {
    throw new IllegalStateException("Tomcat addTransformer method threw exception", ex.getCause());
  }
  catch (Throwable ex) {
    throw new IllegalStateException("Could not invoke Tomcat addTransformer method", ex);
  }
}

代码示例来源:origin: shuzheng/zheng

@Override
public int updateByExample(@Param("record") Record record, @Param("example") Example example) {
  try {
    DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
    Method updateByExample = mapper.getClass().getDeclaredMethod("updateByExample", record.getClass(), example.getClass());
    Object result = updateByExample.invoke(mapper, record, example);
    return Integer.parseInt(String.valueOf(result));
  } catch (IllegalAccessException e) {
    e.printStackTrace();
  } catch (InvocationTargetException e) {
    e.printStackTrace();
  } catch (NoSuchMethodException e) {
    e.printStackTrace();
  }
  DynamicDataSource.clearDataSource();
  return 0;
}

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

private static Object instantiate(Class<?> cl) throws Exception {
  Constructor<?>[] constructors = cl.getDeclaredConstructors();
  Constructor<?> constructor = null;
  int argc = Integer.MAX_VALUE;
  for (Constructor<?> c : constructors) {
    if (c.getParameterTypes().length < argc) {
      argc = c.getParameterTypes().length;
      constructor = c;
    }
  }
  if (constructor != null) {
    Class<?>[] paramTypes = constructor.getParameterTypes();
    Object[] constructorArgs = new Object[paramTypes.length];
    for (int i = 0; i < constructorArgs.length; i++) {
      constructorArgs[i] = getConstructorArg(paramTypes[i]);
    }
    try {
      constructor.setAccessible(true);
      return constructor.newInstance(constructorArgs);
    } catch (InstantiationException e) {
      LogHelper.warn(logger, e.getMessage(), e);
    } catch (IllegalAccessException e) {
      LogHelper.warn(logger, e.getMessage(), e);
    } catch (InvocationTargetException e) {
      LogHelper.warn(logger, e.getMessage(), e);
    }
  }
  return cl.newInstance();
}

代码示例来源:origin: redisson/redisson

/**
 * Constructs a CannotInvokeException with an InvocationTargetException.
 */
public CannotInvokeException(InvocationTargetException e) {
  super("by " + e.getTargetException().toString());
  err = e.getTargetException();
}

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

@Override
public void addTransformer(ClassFileTransformer transformer) {
  try {
    this.addTransformerMethod.invoke(this.classLoader, transformer);
  }
  catch (InvocationTargetException ex) {
    throw new IllegalStateException("GlassFish addTransformer method threw exception", ex.getCause());
  }
  catch (Throwable ex) {
    throw new IllegalStateException("Could not invoke GlassFish addTransformer method", ex);
  }
}

代码示例来源:origin: shuzheng/zheng

@Override
public int countByExample(Example example) {
  try {
    DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
    Method countByExample = mapper.getClass().getDeclaredMethod("countByExample", example.getClass());
    Object result = countByExample.invoke(mapper, example);
    return Integer.parseInt(String.valueOf(result));
  } catch (IllegalAccessException e) {
    e.printStackTrace();
  } catch (InvocationTargetException e) {
    e.printStackTrace();
  } catch (NoSuchMethodException e) {
    e.printStackTrace();
  }
  DynamicDataSource.clearDataSource();
  return 0;
}

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

@Override
  public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
    Type returnType = method.getGenericReturnType();
    assertTrue("Jaxb2Marshaller does not support JAXBElement<" + method.getName().substring(9) + ">",
        marshaller.supports(returnType));
    try {
      // make sure the marshalling does not result in errors
      Object returnValue = method.invoke(primitives);
      marshaller.marshal(returnValue, new StreamResult(new ByteArrayOutputStream()));
    }
    catch (InvocationTargetException e) {
      fail(e.getMessage());
    }
  }
}, new ReflectionUtils.MethodFilter() {

代码示例来源: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: google/guava

@Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  String methodName = method.getName();
  Method typeVariableMethod = typeVariableMethods.get(methodName);
  if (typeVariableMethod == null) {
   throw new UnsupportedOperationException(methodName);
  } else {
   try {
    return typeVariableMethod.invoke(typeVariableImpl, args);
   } catch (InvocationTargetException e) {
    throw e.getCause();
   }
  }
 }
}

代码示例来源:origin: shuzheng/zheng

@Override
public int deleteByExample(Example example) {
  try {
    DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
    Method deleteByExample = mapper.getClass().getDeclaredMethod("deleteByExample", example.getClass());
    Object result = deleteByExample.invoke(mapper, example);
    return Integer.parseInt(String.valueOf(result));
  } catch (IllegalAccessException e) {
    e.printStackTrace();
  } catch (InvocationTargetException e) {
    e.printStackTrace();
  } catch (NoSuchMethodException e) {
    e.printStackTrace();
  }
  DynamicDataSource.clearDataSource();
  return 0;
}

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

@Override
  public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
    Type returnType = method.getGenericReturnType();
    assertTrue("Jaxb2Marshaller does not support JAXBElement<" + method.getName().substring(13) + ">",
        marshaller.supports(returnType));
    try {
      // make sure the marshalling does not result in errors
      Object returnValue = method.invoke(standardClasses);
      marshaller.marshal(returnValue, new StreamResult(new ByteArrayOutputStream()));
    }
    catch (InvocationTargetException e) {
      fail(e.getMessage());
    }
  }
}, new ReflectionUtils.MethodFilter() {

相关文章