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"); }
/** * Handle the given reflection 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 an * InvocationTargetException with such a root cause. Throws an * IllegalStateException with an appropriate message or * UndeclaredThrowableException otherwise. * @param ex the reflection exception to handle */ public static void handleReflectionException(Exception ex) { if (ex instanceof NoSuchMethodException) { throw new IllegalStateException("Method not found: " + ex.getMessage()); } if (ex instanceof IllegalAccessException) { throw new IllegalStateException("Could not access method: " + ex.getMessage()); } if (ex instanceof InvocationTargetException) { handleInvocationTargetException((InvocationTargetException) ex); } if (ex instanceof RuntimeException) { throw (RuntimeException) ex; } throw new UndeclaredThrowableException(ex); }
@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"); }
/** * Handle the given reflection 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 an * InvocationTargetException with such a root cause. Throws an * IllegalStateException with an appropriate message or * UndeclaredThrowableException otherwise. * @param ex the reflection exception to handle */ public static void handleReflectionException(Exception ex) { if (ex instanceof NoSuchMethodException) { throw new IllegalStateException("Method not found: " + ex.getMessage()); } if (ex instanceof IllegalAccessException) { throw new IllegalStateException("Could not access method: " + ex.getMessage()); } if (ex instanceof InvocationTargetException) { handleInvocationTargetException((InvocationTargetException) ex); } if (ex instanceof RuntimeException) { throw (RuntimeException) ex; } throw new UndeclaredThrowableException(ex); }
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"); }
/** * Invoke the specified JDBC API {@link Method} against the supplied target * object with the supplied arguments. * @param method the method to invoke * @param target the target object to invoke the method on * @param args the invocation arguments (may be {@code null}) * @return the invocation result, if any * @throws SQLException the JDBC API SQLException to rethrow (if any) * @see #invokeMethod(java.lang.reflect.Method, Object, Object[]) * @deprecated as of 5.0.11, in favor of custom SQLException handling */ @Deprecated @Nullable public static Object invokeJdbcMethod(Method method, @Nullable Object target, @Nullable Object... args) throws SQLException { try { return method.invoke(target, args); } catch (IllegalAccessException ex) { handleReflectionException(ex); } catch (InvocationTargetException ex) { if (ex.getTargetException() instanceof SQLException) { throw (SQLException) ex.getTargetException(); } handleInvocationTargetException(ex); } throw new IllegalStateException("Should never get here"); }
/** * Handle the given reflection 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 an * InvocationTargetException with such a root cause. Throws an * IllegalStateException with an appropriate message else. * @param ex the reflection exception to handle */ public static void handleReflectionException(Exception ex) { if (ex instanceof NoSuchMethodException) { throw new IllegalStateException("Method not found: " + ex.getMessage()); } if (ex instanceof IllegalAccessException) { throw new IllegalStateException("Could not access method: " + ex.getMessage()); } if (ex instanceof InvocationTargetException) { handleInvocationTargetException((InvocationTargetException) ex); } if (ex instanceof RuntimeException) { throw (RuntimeException) ex; } throw new UndeclaredThrowableException(ex); }
protected static Object invokeVfsMethod(Method method, 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"); }
/** * Invoke the specified JDBC API {@link Method} against the supplied target * object with the supplied arguments. * @param method the method to invoke * @param target the target object to invoke the method on * @param args the invocation arguments (may be <code>null</code>) * @return the invocation result, if any * @throws SQLException the JDBC API SQLException to rethrow (if any) * @see #invokeMethod(java.lang.reflect.Method, Object, Object[]) */ public static Object invokeJdbcMethod(Method method, Object target, Object... args) throws SQLException { try { return method.invoke(target, args); } catch (IllegalAccessException ex) { handleReflectionException(ex); } catch (InvocationTargetException ex) { if (ex.getTargetException() instanceof SQLException) { throw (SQLException) ex.getTargetException(); } handleInvocationTargetException(ex); } throw new IllegalStateException("Should never get here"); }
public static <T, E extends Exception> T invokeMethodWithExpectedExceptionType(Method method, Object target, Class<E> expectedExceptionType, Object... args) throws E { try { return (T) method.invoke(target, args); } catch (IllegalAccessException ex) { ReflectionUtils.handleReflectionException(ex); } catch (InvocationTargetException ex) { if (expectedExceptionType.isAssignableFrom(ex.getTargetException().getClass())) { throw (E) ex.getTargetException(); } ReflectionUtils.handleInvocationTargetException(ex); } throw new IllegalStateException("Should never get here"); }
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"); }
/** * Handle the given reflection 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 an InvocationTargetException with * such a root cause. Throws an IllegalStateException with an appropriate message or * UndeclaredThrowableException otherwise. * * @param ex the reflection exception to handle */ public static void handleReflectionException(Exception ex) { if (ex instanceof NoSuchMethodException) { throw new IllegalStateException("Method not found: " + ex.getMessage()); } if (ex instanceof IllegalAccessException) { throw new IllegalStateException("Could not access method: " + ex.getMessage()); } if (ex instanceof InvocationTargetException) { handleInvocationTargetException((InvocationTargetException) ex); } if (ex instanceof RuntimeException) { throw (RuntimeException) ex; } throw new UndeclaredThrowableException(ex); }
/** * Handle the given reflection 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 an * InvocationTargetException with such a root cause. Throws an * IllegalStateException with an appropriate message else. * @param ex the reflection exception to handle */ public static void handleReflectionException(Exception ex) { if (ex instanceof NoSuchMethodException) { throw new IllegalStateException("Method not found: " + ex.getMessage()); } if (ex instanceof IllegalAccessException) { throw new IllegalStateException("Could not access method: " + ex.getMessage()); } if (ex instanceof InvocationTargetException) { handleInvocationTargetException((InvocationTargetException) ex); } if (ex instanceof RuntimeException) { throw (RuntimeException) ex; } throw new UndeclaredThrowableException(ex); }
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"); }
public void storeToXml(Properties props, OutputStream os, String header, String encoding) throws IOException { // props.storeToXML(os, header, encoding); try { Method storeMethod = props.getClass().getMethod( "storeToXML", new Class[] {OutputStream.class, String.class, String.class}); storeMethod.invoke(props, new Object[] {os, header, encoding}); } catch (NoSuchMethodException ex) { throw new IOException("Cannot store properties XML file - not running on JDK 1.5+: " + ex.getMessage()); } catch (InvocationTargetException ex) { if (ex.getTargetException() instanceof IOException) { throw (IOException) ex.getTargetException(); } ReflectionUtils.handleInvocationTargetException(ex); } catch (Exception ex) { ReflectionUtils.handleReflectionException(ex); } }
/** * Handle the given reflection 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 an InvocationTargetException with such a root cause. Throws * an IllegalStateException with an appropriate message else. * @param ex the reflection exception to handle */ public static void handleReflectionException(Exception ex) { if (ex instanceof NoSuchMethodException) { throw new IllegalStateException("Method not found: " + ex.getMessage()); } if (ex instanceof IllegalAccessException) { throw new IllegalStateException("Could not access method: " + ex.getMessage()); } if (ex instanceof InvocationTargetException) { handleInvocationTargetException((InvocationTargetException) ex); } throw new IllegalStateException( "Unexpected reflection exception - " + ex.getClass().getName() + ": " + ex.getMessage()); }
public void loadFromXml(Properties props, InputStream is) throws IOException { // props.loadFromXML(is); try { Method loadMethod = props.getClass().getMethod("loadFromXML", new Class[] {InputStream.class}); loadMethod.invoke(props, new Object[] {is}); } catch (NoSuchMethodException ex) { throw new IOException("Cannot load properties XML file - not running on JDK 1.5+: " + ex.getMessage()); } catch (InvocationTargetException ex) { if (ex.getTargetException() instanceof IOException) { throw (IOException) ex.getTargetException(); } ReflectionUtils.handleInvocationTargetException(ex); } catch (Exception ex) { ReflectionUtils.handleReflectionException(ex); } }
public short errorCodeFromTopicMetadata(String topic, ZkUtils zkUtils) { try { Method fetchTopicMetadataFromZk = ReflectionUtils.findMethod(ADMIN_UTIL_CLASS, "fetchTopicMetadataFromZk", String.class, ZkUtils.class); Object result = fetchTopicMetadataFromZk.invoke(null, topic, zkUtils); Class<?> topicMetadataClass = ClassUtils.forName("kafka.api.TopicMetadata", null); Method errorCodeMethod = ReflectionUtils.findMethod(topicMetadataClass, "errorCode"); return (short) errorCodeMethod.invoke(result); } catch (ClassNotFoundException e) { throw new IllegalStateException("AdminUtils class not found", e); } catch (InvocationTargetException e) { ReflectionUtils.handleInvocationTargetException(e); } catch (IllegalAccessException e) { ReflectionUtils.handleReflectionException(e); } return 0; }
public void storeToXml(Properties props, OutputStream os, String header) throws IOException { // props.storeToXML(os, header); try { Method storeMethod = props.getClass().getMethod( "storeToXML", new Class[] {OutputStream.class, String.class}); storeMethod.invoke(props, new Object[] {os, header}); } catch (NoSuchMethodException ex) { throw new IOException("Cannot store properties XML file - not running on JDK 1.5+: " + ex.getMessage()); } catch (InvocationTargetException ex) { if (ex.getTargetException() instanceof IOException) { throw (IOException) ex.getTargetException(); } ReflectionUtils.handleInvocationTargetException(ex); } catch (Exception ex) { ReflectionUtils.handleReflectionException(ex); } }
@SuppressWarnings("unchecked") public int partitionSize(String topic, ZkUtils zkUtils) { try { Method fetchTopicMetadataFromZk = ReflectionUtils.findMethod(ADMIN_UTIL_CLASS, "fetchTopicMetadataFromZk", String.class, ZkUtils.class); Object result = fetchTopicMetadataFromZk.invoke(null, topic, zkUtils); Class<?> topicMetadataClass = ClassUtils.forName("kafka.api.TopicMetadata", null); Method partitionsMetadata = ReflectionUtils.findMethod(topicMetadataClass, "partitionsMetadata"); scala.collection.Seq<kafka.api.PartitionMetadata> partitionSize = (scala.collection.Seq<kafka.api.PartitionMetadata>)partitionsMetadata.invoke(result); return partitionSize.size(); } catch (ClassNotFoundException e) { throw new IllegalStateException("AdminUtils class not found", e); } catch (InvocationTargetException e) { ReflectionUtils.handleInvocationTargetException(e); } catch (IllegalAccessException e) { ReflectionUtils.handleReflectionException(e); } return 0; }