, , permutation . JavaDoc Class.getDeclaredMethods(). :
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class SO {
public void test1() {
System.out.println("Running test1");
}
public void test2() {
System.out.println("Running test2");
}
public void test3() {
System.out.println("Running test3");
}
public void notATest() {
System.err.println("THIS IS NOT A TEST");
}
public static void main(String ... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class c = SO.class;
SO that = new SO();
Method[] methods = filter(c.getDeclaredMethods(), "test");
PermuteMethod permutations = new PermuteMethod(methods);
while(permutations.hasNext()) {
for(Method permutation: permutations.next()) {
permutation.invoke(that, null);
}
System.out.println("----");
}
}
private static Method[] filter(Method[] declaredMethods, String startsWith) {
List<Method> filtered = new ArrayList<>();
for(Method method : declaredMethods) {
if(method.getName().startsWith(startsWith)) {
filtered.add(method);
}
}
return filtered.toArray(new Method[filtered.size()]);
}
}
follwoung ( , notATest() method:
Running test1
Running test2
Running test3
----
Running test1
Running test3
Running test2
----
Running test2
Running test1
Running test3
----
Running test2
Running test3
Running test1
----
Running test3
Running test1
Running test2
----
Running test3
Running test2
Running test1
----
() :
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class PermuteMethod implements Iterator<Method[]> {
private final int size;
private final Method[] elements;
private final Method[] ar;
private final int[] permutation;
private boolean next = true;
public PermuteMethod(Method[] e) {
size = e.length;
elements = new Method[size];
System.arraycopy(e, 0, elements, 0, size);
ar = new Method[size];
System.arraycopy(e, 0, ar, 0, size);
permutation = new int[size + 1];
for (int i = 0; i < size + 1; i++) {
permutation[i] = i;
}
}
private void formNextPermutation() {
for (int i = 0; i < size; i++) {
Array.set(ar, i, elements[permutation[i + 1] - 1]);
}
}
public boolean hasNext() {
return next;
}
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
private void swap(final int i, final int j) {
final int x = permutation[i];
permutation[i] = permutation[j];
permutation[j] = x;
}
public Method[] next() throws NoSuchElementException {
formNextPermutation();
int i = size - 1;
while (permutation[i] > permutation[i + 1])
i--;
if (i == 0) {
next = false;
for (int j = 0; j < size + 1; j++) {
permutation[j] = j;
}
return ar;
}
int j = size;
while (permutation[i] > permutation[j])
j--;
swap(i, j);
int r = size;
int s = i + 1;
while (r > s) {
swap(r, s);
r--;
s++;
}
return ar;
}
}