/* The following static fields will be used by multiple fragments */ public static Object methodInvocationObject = null; public static Object methodExecutionObject = null; public static Method includeMethodBarChart; public static Method includeMethodPieChart; public static Method getCountMethod; public static Method updateMethodCount; public static Hashtable methodStartTime = new Hashtable(); static { try { /* Use reflection to get the required classes */ ClassLoader myClassLoader = ClassLoader.getSystemClassLoader(); Class methodInvocationBarChart = myClassLoader.loadClass("MethodInvocationBarChart"); Class methodTimePieChart = myClassLoader.loadClass("MethodTimePieChart"); Class refineryUitilities = myClassLoader.loadClass("org.jfree.ui.RefineryUtilities"); Class window = myClassLoader.loadClass("java.awt.Window"); /* Create new instances of the bar and pie chart classes */ methodInvocationObject = methodInvocationBarChart.newInstance(); methodExecutionObject = methodTimePieChart.newInstance(); /* Get the methods that will be used by other fragment(s) */ Class[] includeMethodBarArgTypes = new Class[] { Integer.TYPE, String.class}; includeMethodBarChart = methodInvocationBarChart.getMethod("includeMethod", includeMethodBarArgTypes); /* MethodInvocationBarChart.includeMethod() */ Class[] getCountMethodArgTypes = new Class[] {String.class}; getCountMethod = methodInvocationBarChart.getMethod("getMethodCount", getCountMethodArgTypes); /* MethodInvocationBarChart.getMethodCount() */ Class[] includeMethodPieArgTypes = new Class[] { Double.TYPE, String.class}; includeMethodPieChart = methodTimePieChart.getMethod("includeMethod", includeMethodPieArgTypes); /* MethodTimePieChart.includeMethod() */ Class[] updateMethodCountArgTypes = new Class[] { String.class, Integer.TYPE}; updateMethodCount = methodInvocationBarChart.getMethod("updateMethodCount", updateMethodCountArgTypes); /* MethodInvocationBarChart.updateMethodCount() */ /* Invoke MethodInvocationBarChart.pack() */ Method packMethod = methodInvocationBarChart.getMethod("pack", null); packMethod.invoke(methodInvocationObject, null); /* Invoke MethodTimePieChart.pack() */ packMethod = methodTimePieChart.getMethod("pack", null); packMethod.invoke(methodExecutionObject, null); /* Invoke RefineryUtilities.positionFrameOnScreen() */ Class[] positionFrameOnScreenArgTypes = new Class[] {window, Double.TYPE, Double.TYPE}; Method positionFrameOnScreenMethod = refineryUitilities.getMethod("positionFrameOnScreen", positionFrameOnScreenArgTypes); positionFrameOnScreenMethod.invoke(null, new Object[] {methodExecutionObject, new Double(0), new Double(0.84)}); /* Invoke MethodInvocationBarChart.setVisible(true) */ Class[] setVisibleArgTypes = new Class[] {Boolean.TYPE}; Method setVisibleMethod = methodInvocationBarChart.getMethod("setVisible", setVisibleArgTypes); setVisibleMethod.invoke(methodInvocationObject, new Object[] {new Boolean(true)}); /* Invoke MethodTimePieChart.setVisible(true) */ setVisibleMethod = methodTimePieChart.getMethod("setVisible", setVisibleArgTypes); setVisibleMethod.invoke(methodExecutionObject, new Object[] {new Boolean(true)}); } catch (Exception e) { System.err.println("An exception has occured: " + e.getMessage()); e.printStackTrace(); } } /* Make sure our object is not NULL before continuing */ if (methodInvocationObject != null) { try { /* Get the number of times this method has been executed before */ Object[] getCountMethodArgs = new Object[] {new String(methodName)}; int previousValue = ((Integer)getCountMethod.invoke(methodInvocationObject, getCountMethodArgs)).intValue(); previousValue++; /* Update method counts */ Object[] updateMethodCountArgs = new Object[] {new String(methodName), new Integer(previousValue)}; updateMethodCount.invoke(methodInvocationObject, updateMethodCountArgs); /* Graph the method only if it has been invoked more than 2500 times */ if (previousValue >= 2500) { Object[] includeMethodArgs = new Object[] {new Integer(++previousValue), new String(methodName)}; includeMethodBarChart.invoke(methodInvocationObject, includeMethodArgs); } /* Store the start time of the method */ methodStartTime.put(methodName, new Long(new Date().getTime())); } catch (Exception e) { System.err.println("An exception occured while trying to execute the methods of MethodInvocationBarChart: " + e.getMessage()); e.printStackTrace(); } } /* Get the start time of the method */ Long methodStartTimeObject = (Long)methodStartTime.get(methodName); if (methodStartTimeObject == null) return; long startTime = methodStartTimeObject.longValue(); /* Remove this entry from our hashtable. Makes things a little more optimum */ methodStartTime.remove(methodName); /* Calculate the execution time of the method (in ms)*/ double executionTime = (double)((new Date().getTime()) - startTime) / (double)1000; /* We're only interested if the execution time is > than 3 second */ if (executionTime >= 3) { try { DecimalFormat df = new DecimalFormat("0.##"); /* Graph the execution time of this method */ Object[] includeMethodArgs = new Object[] {new Double(Double.parseDouble(df.format(executionTime))), new String(methodName)}; includeMethodPieChart.invoke(methodExecutionObject, includeMethodArgs); } catch (Exception e) { System.err.println("An exception occured while trying to execute the methods of methodInvocationObject"); e.printStackTrace(); } }