public static Method getPublicMethod(Class<?> cls, String methodName, Class<?>... parameterTypes)
            throws SecurityException, NoSuchMethodException {

        Method declaredMethod = cls.getMethod(methodName, parameterTypes);
        if (Modifier.isPublic(declaredMethod.getDeclaringClass().getModifiers())) {
            return declaredMethod;
        }

        List<Class<?>> candidateClasses = new ArrayList<Class<?>>();
        candidateClasses.addAll(getAllInterfaces(cls));
        candidateClasses.addAll(getAllSuperclasses(cls));

        for (Class<?> candidateClass : candidateClasses) {
            if (!Modifier.isPublic(candidateClass.getModifiers())) {
                continue;
            }
            Method candidateMethod;
            try {
                candidateMethod = candidateClass.getMethod(methodName, parameterTypes);
            } catch (NoSuchMethodException ex) {
                continue;
            }
            if (Modifier.isPublic(candidateMethod.getDeclaringClass().getModifiers())) {
                return candidateMethod;
            }
        }

        throw new NoSuchMethodException("Can't find a public method for " +
                methodName + " " + ArrayUtils.toString(parameterTypes));
    }
<p>Returns the desired Method much like {@code Class.getMethod}, however
it ensures that the returned Method is from a public class or interface and not
from an anonymous inner class. This means that the Method is invokable and
doesn't fall foul of Java bug
<a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4071957">4071957</a>).

 <code><pre>Set set = Collections.unmodifiableSet(...);
 Method method = ClassUtils.getPublicMethod(set.getClass(), "isEmpty",  new Class[0]);
 Object result = method.invoke(set, new Object[]);</pre></code>
</p>

@param cls  the class to check, not null
@param methodName  the name of the method
@param parameterTypes  the list of parameters
@return the method
@throws NullPointerException if the class is null
@throws SecurityException if a a security violation occured
@throws NoSuchMethodException if the method is not found in the given class
 or if the metothod doen't conform with the requirements
android/android_framework/base/src/main/java/lang3/ClassUtils.java