I don't have a library for you, but if you get a random generator, then perhaps this class may come in handy:
public class BeanMethodIterator implements Iterable<Method> { private static final int MODIFIER_FILTER = (Modifier.PUBLIC | Modifier.STATIC); private static final int MODIFIER_EXPECTED = Modifier.PUBLIC; public enum Filter { GETTERS(new Transform<Method, Boolean>(){ public Boolean transform(Method input) { return (input.getName().startsWith("get") || input.getName().startsWith("is")) && input.getParameterTypes().length == 0; }; }), SETTTERS(new Transform<Method, Boolean>(){ public Boolean transform(Method input) { return input.getName().startsWith("set") && input.getParameterTypes().length == 1; }; }), BOTH(new Transform<Method, Boolean>(){ public Boolean transform(Method input) { return Filter.SETTTERS.condition.transform(input) || Filter.GETTERS.condition.transform(input); }; }); private Transform<Method, Boolean> condition; private Filter(Transform<Method, Boolean> condition) { this.condition = condition; } }; public enum Scope { PARENTS_ALSO() { @Override protected Method[] getMethods(Class<?> c) { return c.getMethods(); }; }, THIS_CLASS_ONLY() { @Override protected Method[] getMethods(Class<?> c) { return c.getDeclaredMethods(); } }; protected abstract Method[] getMethods(Class<?> c); } private final Filter filter; private final Scope scope; private final Class<?> theClass; public BeanMethodIterator(Class<?> theClass, Filter what, Scope scope) { this.filter = what; this.theClass = theClass; this.scope = scope; } public BeanMethodIterator(Class<?> theClass) { this(theClass, Filter.BOTH, Scope.PARENTS_ALSO); } private static boolean isPublic(Method method) { return (method.getModifiers() & MODIFIER_FILTER) == MODIFIER_EXPECTED; } public Iterator<Method> iterator() { final Method[] methods = this.scope.getMethods(this.theClass); return new Iterator<Method>() { int index = 0; public boolean hasNext() { while (index < methods.length) { if (isPublic(methods[index]) && filter.condition.transform(methods[index])) return true; index++; } return false; } public Method next() { if (!hasNext()) throw new NoSuchElementException(); return methods[index++]; } public void remove() { throw new UnsupportedOperationException(); } }; } public static void main(String[] args) { for (Method m: new BeanMethodIterator(Date.class, Filter.GETTERS, Scope.THIS_CLASS_ONLY)) { System.out.println(m.getName()); } } } interface Transform<I, O> { public O transform(I input); }
source share