ReflectionUtils { public static <T> T tryReflection(ReflectionRunnable<T> runnable) { try { return runnable.run(); } catch (InstantiationException e) { throw new MisconfigurationException("core.INSTANTIATION", e.getMessage(), e); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); if (t instanceof Error) { throw (Error) t; } else if (t instanceof RuntimeException) { throw (RuntimeException) t; } else if (t instanceof Exception) { throw new RuntimeException(t); } else { throw new InternalError(t); } } catch (NoSuchMethodException e) { throw new MisconfigurationException("core.NO_SUCH_METHOD", e.getMessage(), e); } catch (NoSuchFieldException e) { throw new MisconfigurationException("core.NO_SUCH_FIELD", e.getMessage(), e); } catch (IllegalAccessException e) { throw new MisconfigurationException("core.ILLEGAL_ACCESS", e.getMessage(), e); } catch (ClassNotFoundException e) { throw new MisconfigurationException("core.CLASS_NOT_FOUND", e.getMessage(), getClasspathString(), e); } }  static String getClasspathString(); static T tryReflection(ReflectionRunnable<T> runnable);  }
@Test public void occursIllegalAccessException() { try { ReflectionUtils.tryReflection(() -> { Method m = TestPrivate.class.getDeclaredMethod("forbidden"); assertThat(m).isNotNull(); return m.invoke(new TestPrivate()); }); fail("Unreachable"); } catch (MisconfigurationException ex) { assertThat(ex.getCode()).isEqualTo("core.ILLEGAL_ACCESS"); } }
@SuppressWarnings("JavaReflectionMemberAccess") @Test public void occursNoSuchField() { try { ReflectionUtils.tryReflection(() -> TestPrivate.class.getDeclaredField("no-such-field")); } catch (MisconfigurationException ex) { assertThat(ex.getCode()).isEqualTo("core.NO_SUCH_FIELD"); } }
@Test public void occursNoSuchMethod() { try { ReflectionUtils.tryReflection(() -> TestPrivate.class.getDeclaredMethod("no-such-method")); } catch (MisconfigurationException ex) { assertThat(ex.getCode()).isEqualTo("core.NO_SUCH_METHOD"); } }
@Test public void occursClassNotFound() { try { ReflectionUtils.tryReflection(() -> Class.forName("no.such.clazz")); } catch (MisconfigurationException ex) { assertThat(ex.getCode()).isEqualTo("core.CLASS_NOT_FOUND"); } }
@SuppressWarnings("JavaReflectionMemberAccess") @Test public void occursInstantiationException() { try { ReflectionUtils.tryReflection(() -> InstantiationableClass.class.getConstructor().newInstance()); } catch (MisconfigurationException ex) { assertThat(ex.getCode()).isEqualTo("core.NO_SUCH_METHOD"); } }
@Test public void occursInvocationTargetException() { assertThatThrownBy(() -> ReflectionUtils.tryReflection(() -> { InvocationTarget t = new InvocationTarget(); return InvocationTarget.class.getMethod("throwRuntimeException").invoke(t); })).isExactlyInstanceOf(NumberFormatException.class); }
@Test public void occursInvocationTargetException_checked() { try { ReflectionUtils.tryReflection(() -> { InvocationTarget t = new InvocationTarget(); return InvocationTarget.class.getMethod("throwCheckedException").invoke(t); }); fail("Unreachable"); } catch (RuntimeException ex) { assertThat(ex.getCause()).isOfAnyClassIn(IOException.class); } }