/* Begin RandomRunner.java file */ import java.lang.reflect.Method; import java.util.Random; /* This class runs its own methods in random order, by precedence group */ public class RandomRunner { /* This attribute stores the number of the highest precedence group , which are assumed to go from 0 to this number - 1 */ int mg = 3; /* The random number generator */ Random r = new Random(); /* The main method */ public static void main(String[] args) { try { RandomRunner RR = new RandomRunner(); RR.executeMethods(); } catch (Exception e) { e.printStackTrace(); } } /* The following are some * random * methods ... */ public int method_01(Boolean getGroup) { if (getGroup.booleanValue()) { return 1; } else { System.out.println("method_01 fired"); return 0; } } // method_01() public int method_02(Boolean getGroup) { if (getGroup.booleanValue()) { return 1; } else { System.out.println("method_02 fired"); return 0; } } // method_02() public int method_03(Boolean getGroup) { if (getGroup.booleanValue()) { return 2; } else { System.out.println("method_03 fired"); return 0; } } // method_03() public int method_04(Boolean getGroup) { if (getGroup.booleanValue()) { return 2; } else { System.out.println("method_04 fired"); return 0; } } // method_04() public int method_05(Boolean getGroup) { if (getGroup.booleanValue()) { return 2; } else { System.out.println("method_05 fired"); return 0; } } // method_05() /* The proceding are some * random * methods */ /* This method creates an array with the integers 0 to l - 1 in random order */ public int[] getRandomOrder(int l) { int[] x = new int[l]; for (int i = 0; i < l; i++) { x[i] = i; } int tmp = 0; int p = 0; int q = 0; for (int i = 0; i < l; i++) { p = r.nextInt(l); q = r.nextInt(l); if (p != q) { tmp = x[p]; x[p] = x[q]; x[q] = tmp; } } return x; } /*This method executes the class methods in (pseudo) random order */ public void executeMethods() throws Exception { Method[] m = this.getClass().getDeclareciMethods(); int xg[] = new int[m.length]; Object[] o = new Object[1]; Object[] O = new Object[1] o[0] = new Boolean(true); O[0] = new Boolean(false); // get distinct precedence group for each method for (int i = 0; i < m.length; i++) { if (m[i].getReturnType() != int.class) { xg[i] = 0; } else { xg[i] = ((Integer)(m[i].invoke(this, o))).intValue(); } } // count number of methods per distinct precedence group short[] cg = new short[mg]; for (int i = 0; i < m.length; i++) { cg[xg[i]] = (short)(cg[xg[i]] + 1); } // divide methods into precedence groups // first create precdence groups Method[][] z = new Method[mg][]; for (int i = 0; i < mg; i++) { z[i] = new Method[cg[i]]; } // now initialize references to first free locations in each group for (int i = 0; i < mg; i++) { cg[i] = 0; } // divide up method references for (int i = 0; i < m.length; i++) { z[xg[i]][(cg[xg[i]]) ++] = m[i]; } // execute methods in random order; "0" methods do not get executed randomly int[] y = null; for (int i = 1; i < mg; i++) { y = new int[z[i].length]; y = getRandomOrder(y.length); for (int j = 0; j < y.length; j++) { z[i][y[j]].invoke(this, O); } } } // executeMethods } // RandomRunner /* End RandomRunner.java file */