@Test public void shouldFindMethod() { class A { void methodFromA() {}; } class B extends A { void methodFromB() {}; } Method methodFromA = ReflectionUtils.findMethod(B.class, "methodFromA"); Method methodFromB = ReflectionUtils.findMethod(B.class, "methodFromB"); Method notExistingMethod = ReflectionUtils.findMethod(B.class, "notExistingMethod"); assertThat(methodFromA.getDeclaringClass()).isEqualTo(A.class); assertThat(methodFromB.getDeclaringClass()).isEqualTo(B.class); assertThat(notExistingMethod).isNull(); }
public static <T> Method findMethod(Class<? extends T> klass, String methodName) { Class<?> superClass = klass; while (superClass != Object.class) { try { return superClass.getDeclaredMethod(methodName); } catch (NoSuchMethodException e) { } superClass = superClass.getSuperclass(); } return null; }
ReflectionUtils { public static <T> Method findMethod(Class<? extends T> klass, String methodName) { Class<?> superClass = klass; while (superClass != Object.class) { try { return superClass.getDeclaredMethod(methodName); } catch (NoSuchMethodException e) { } superClass = superClass.getSuperclass(); } return null; } }
ReflectionUtils { public static <T> Method findMethod(Class<? extends T> klass, String methodName) { Class<?> superClass = klass; while (superClass != Object.class) { try { return superClass.getDeclaredMethod(methodName); } catch (NoSuchMethodException e) { } superClass = superClass.getSuperclass(); } return null; } private  ReflectionUtils(); }
ReflectionUtils { public static <T> Method findMethod(Class<? extends T> klass, String methodName) { Class<?> superClass = klass; while (superClass != Object.class) { try { return superClass.getDeclaredMethod(methodName); } catch (NoSuchMethodException e) { } superClass = superClass.getSuperclass(); } return null; } private  ReflectionUtils(); static Iterable<Class<? super T>> getAncestors(Class<T> klass); static boolean isMethodOverriden(Class<? extends T> klass, Class<T> parentClass, String methodName); static Method findMethod(Class<? extends T> klass, String methodName); }
ReflectionUtils { public static <T> Method findMethod(Class<? extends T> klass, String methodName) { Class<?> superClass = klass; while (superClass != Object.class) { try { return superClass.getDeclaredMethod(methodName); } catch (NoSuchMethodException e) { } superClass = superClass.getSuperclass(); } return null; } private  ReflectionUtils(); static Iterable<Class<? super T>> getAncestors(Class<T> klass); static boolean isMethodOverriden(Class<? extends T> klass, Class<T> parentClass, String methodName); static Method findMethod(Class<? extends T> klass, String methodName);  }