@Test(groups = "fast") public void testAccessibleMethodBaseCase() throws Exception { Method m = Object.class.getMethod("getClass"); Method m2 = ReflectUtil.findAccessibleMethod(m); Assert.assertSame(m, m2); }
public static Method findAccessibleMethod(final Method m) { if (isPublic(m.getModifiers()) && isPublic(m.getDeclaringClass().getModifiers())) { return m; } if (m.isAccessible()) { return m; } final Class<?> clazz = m.getDeclaringClass(); final String name = m.getName(); final Class<?>[] ptypes = m.getParameterTypes(); for (Class<?> iface : clazz.getInterfaces()) { try { Method m2 = iface.getMethod(name, ptypes); if (m2.isAccessible()) { return m2; } if (isPublic(iface.getModifiers()) && isPublic(m2.getModifiers())) { return m2; } } catch (NoSuchMethodException nsme) { } } Class<?> c = clazz.getSuperclass(); while (c != null) { try { Method m2 = c.getMethod(name, ptypes); if (m2.isAccessible()) { return m2; } if (isPublic(c.getModifiers()) && isPublic(m2.getModifiers())) { return m2; } } catch (NoSuchMethodException nsme) { } c = c.getSuperclass(); } return m; }
ReflectUtil { public static Method findAccessibleMethod(final Method m) { if (isPublic(m.getModifiers()) && isPublic(m.getDeclaringClass().getModifiers())) { return m; } if (m.isAccessible()) { return m; } final Class<?> clazz = m.getDeclaringClass(); final String name = m.getName(); final Class<?>[] ptypes = m.getParameterTypes(); for (Class<?> iface : clazz.getInterfaces()) { try { Method m2 = iface.getMethod(name, ptypes); if (m2.isAccessible()) { return m2; } if (isPublic(iface.getModifiers()) && isPublic(m2.getModifiers())) { return m2; } } catch (NoSuchMethodException nsme) { } } Class<?> c = clazz.getSuperclass(); while (c != null) { try { Method m2 = c.getMethod(name, ptypes); if (m2.isAccessible()) { return m2; } if (isPublic(c.getModifiers()) && isPublic(m2.getModifiers())) { return m2; } } catch (NoSuchMethodException nsme) { } c = c.getSuperclass(); } return m; } }
ReflectUtil { public static Method findAccessibleMethod(final Method m) { if (isPublic(m.getModifiers()) && isPublic(m.getDeclaringClass().getModifiers())) { return m; } if (m.isAccessible()) { return m; } final Class<?> clazz = m.getDeclaringClass(); final String name = m.getName(); final Class<?>[] ptypes = m.getParameterTypes(); for (Class<?> iface : clazz.getInterfaces()) { try { Method m2 = iface.getMethod(name, ptypes); if (m2.isAccessible()) { return m2; } if (isPublic(iface.getModifiers()) && isPublic(m2.getModifiers())) { return m2; } } catch (NoSuchMethodException nsme) { } } Class<?> c = clazz.getSuperclass(); while (c != null) { try { Method m2 = c.getMethod(name, ptypes); if (m2.isAccessible()) { return m2; } if (isPublic(c.getModifiers()) && isPublic(m2.getModifiers())) { return m2; } } catch (NoSuchMethodException nsme) { } c = c.getSuperclass(); } return m; } private  ReflectUtil(); }
ReflectUtil { public static Method findAccessibleMethod(final Method m) { if (isPublic(m.getModifiers()) && isPublic(m.getDeclaringClass().getModifiers())) { return m; } if (m.isAccessible()) { return m; } final Class<?> clazz = m.getDeclaringClass(); final String name = m.getName(); final Class<?>[] ptypes = m.getParameterTypes(); for (Class<?> iface : clazz.getInterfaces()) { try { Method m2 = iface.getMethod(name, ptypes); if (m2.isAccessible()) { return m2; } if (isPublic(iface.getModifiers()) && isPublic(m2.getModifiers())) { return m2; } } catch (NoSuchMethodException nsme) { } } Class<?> c = clazz.getSuperclass(); while (c != null) { try { Method m2 = c.getMethod(name, ptypes); if (m2.isAccessible()) { return m2; } if (isPublic(c.getModifiers()) && isPublic(m2.getModifiers())) { return m2; } } catch (NoSuchMethodException nsme) { } c = c.getSuperclass(); } return m; } private  ReflectUtil(); static boolean isDefault(Method method); @SuppressWarnings("rawtypes") // this allows us to assign without casting static Class findClass(String name); static String toString(Annotation ann); static Collection<Method> getMethods(Class<?> clazz); static Collection<Field> getFields(Class<?> clazz); static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String property); static Method findAccessibleMethod(final Method m); static Field getField(Class<?> clazz, String property); static Object getDefaultValue(Class<?> clazz); static Set<Class<?>> getImplementedInterfaces(Class<?> clazz); static Type[] getActualTypeArguments(Class<?> clazz, Class<?> targetType); static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz); static Method resolveBridgedReadMethod(PropertyDescriptor pd); static Method resolveBridgedWriteMethod(PropertyDescriptor pd); static Class<?> resolvePropertyType(PropertyDescriptor pd); }
ReflectUtil { public static Method findAccessibleMethod(final Method m) { if (isPublic(m.getModifiers()) && isPublic(m.getDeclaringClass().getModifiers())) { return m; } if (m.isAccessible()) { return m; } final Class<?> clazz = m.getDeclaringClass(); final String name = m.getName(); final Class<?>[] ptypes = m.getParameterTypes(); for (Class<?> iface : clazz.getInterfaces()) { try { Method m2 = iface.getMethod(name, ptypes); if (m2.isAccessible()) { return m2; } if (isPublic(iface.getModifiers()) && isPublic(m2.getModifiers())) { return m2; } } catch (NoSuchMethodException nsme) { } } Class<?> c = clazz.getSuperclass(); while (c != null) { try { Method m2 = c.getMethod(name, ptypes); if (m2.isAccessible()) { return m2; } if (isPublic(c.getModifiers()) && isPublic(m2.getModifiers())) { return m2; } } catch (NoSuchMethodException nsme) { } c = c.getSuperclass(); } return m; } private  ReflectUtil(); static boolean isDefault(Method method); @SuppressWarnings("rawtypes") // this allows us to assign without casting static Class findClass(String name); static String toString(Annotation ann); static Collection<Method> getMethods(Class<?> clazz); static Collection<Field> getFields(Class<?> clazz); static PropertyDescriptor getPropertyDescriptor(Class<?> clazz, String property); static Method findAccessibleMethod(final Method m); static Field getField(Class<?> clazz, String property); static Object getDefaultValue(Class<?> clazz); static Set<Class<?>> getImplementedInterfaces(Class<?> clazz); static Type[] getActualTypeArguments(Class<?> clazz, Class<?> targetType); static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz); static Method resolveBridgedReadMethod(PropertyDescriptor pd); static Method resolveBridgedWriteMethod(PropertyDescriptor pd); static Class<?> resolvePropertyType(PropertyDescriptor pd);  }