/** * Get the reflection of a private and protected method. * * Required since {@link java.lang.Class#getMethod(String, Class...) * only find public method * * @param clazz the class to search in * @param name the method name * @param parameterTypes the method's parameter types * @return the method reflection * @throws NoSuchMethodException */ private static Method getMethod( final Class<?> clazz, final String name, final Class<?>... parameterTypes) throws NoSuchMethodException { try { /* Try to fetch the method reflection */ return clazz.getDeclaredMethod(name, parameterTypes); } catch (final NoSuchMethodException e1) { /* No such method. Search base class if there is one, * otherwise complain */ if (clazz.getSuperclass() != null) { try { /* Try to fetch the method from the base class */ return getMethod(clazz.getSuperclass(), name, parameterTypes); } catch (final NoSuchMethodException e2) { /* We don't want the exception to indicate that the * *base* class didn't contain a matching method, but * rather that the subclass didn't. */ final StringBuilder s = new StringBuilder(); s.append(clazz.getName()); s.append("("); boolean first = true; for (final Class<?> parameterType : parameterTypes) { if (!first) s.append(", "); first = false; s.append(parameterType); } s.append(")"); throw new NoSuchMethodException(s.toString()); } } else { /* No base class, complain */ throw e1; } } }