public static Exception extractTargetException(InvocationTargetException itex) { Throwable target = itex.getTargetException(); return target instanceof Exception ? (Exception) target : itex; }
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); } }
/** * 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); } }
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)); } } } }
@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; }
@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); } }
@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; }
@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); } }
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; }
@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); } }
@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; }
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(); }
/** * Constructs a CannotInvokeException with an InvocationTargetException. */ public CannotInvokeException(InvocationTargetException e) { super("by " + e.getTargetException().toString()); err = e.getTargetException(); }
@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); } }
@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; }
@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() {
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; } }
@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(); } } } }
@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; }
@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() {