Results 1 to 4 of 4
  1. #1
    PataRican is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Counting Method Execution, Testing Tool

    I am currently writing a testing tool which displays any selected class's methods. This tool can also execute any .java class with a "static void Main(String[] args)" method.

    I would also like to be able to count how many times a method is invoked(used) during run time. Although I haven't the slightest Idea how i would even go about approaching the task..

    Can anyone shed some light?

  2. #2
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    This tool can also execute any .java class with a "static void Main(String[] args)"
    I presume you mean "public static void main(String[] args)".
    And I guess you thought of private static int's to keep count? Or do you mean something else? (I can imagine that it is a bit more difficult to foresee the needed ints if you get the methods by introspection.)
    I think you have to clarify your problem a bit more.
    (And give a SSCEE, short self contained compiling example of what you have until now, with only code refering to the problem. Using codetags.)
    Last edited by Jodokus; 04-30-2011 at 02:09 AM. Reason: Asking SSCEE

  3. #3
    PataRican is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    Does this help? This is the function where i plan on handling the count..


    Java Code:
    public void updateNumbers() {
             int idx = jList1.getSelectedIndex();
             try {
                   int i = farray[idx].getName().indexOf(".class");
                   String cName = farray[idx].getName().substring(0,i);
                   Class c = Class.forName(dir.getName() + "." + cName, true, ucl);
     
                   // handle method counts
                   // :
                   // : 
     
                   // jColHeader.setText(...);    (Used to show the count of each method)
             }
             catch (Exception ex) {
                  System.out.println(ex);
             }
     
    }

    Also this is a snipit of the "run button action performed"

    Java Code:
      // Run button clicked
      void jButton4_actionPerformed(ActionEvent e) {
        LaunchingConnector lc = Bootstrap.virtualMachineManager().defaultConnector();
        Map map = lc.defaultArguments();
        Connector.Argument ca = (Connector.Argument) map.get("main");
        int idx = jList1.getSelectedIndex();
        try {
          int i = farray[idx].getName().indexOf(".class");
          String cName = dir.getName() + "." + farray[idx].getName().substring(0, i);
          ca.setValue("-cp \"" + dir.getParentFile()+ "\" " + cName);
          vm = lc.launch(map);
          Process process = vm.process();
          vm.setDebugTraceMode(VirtualMachine.TRACE_NONE);
          displayRemoteOutput(process.getInputStream());
          mt = new MyThread(vm, false, dir.getName(), farray.length, this);
        }
        catch (Exception ex) {
          System.out.println(e);
        }
      }
    }
    Last edited by PataRican; 04-30-2011 at 10:56 PM.

  4. #4
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    I would make some counterClass:
    Java Code:
    package countmethods;
    import java.util.ArrayList;
    
    public class RunningClasses{ 
    private ArrayList<ClassCounter> counters = new ArrayList<ClassCounter>();
    
    public void addRunningClass( String theClassName ){
    	//add a counter, or increase if it exists
    }
    public void removeRunningClass( String theClassName ){
    	//decrease counter, or remove counter if numberLaunched==1
                 //errorcode if it doesn't exists
    }
    public int getNumberLaunched( String theClassName ){
    	int result = 0;
    	//get the result
    	return result;
    }
    
    	class ClassCounter{
    		private String className;
    		private int numberLauched;
    		//some methods to check for name and adapt the count
    	}
    }
    Then you make a static instance of this class, and increase/decrease it as needed:
    Java Code:
    package countmethods;
    
    //import java.awt.event.ActionEvent;//added
    import java.io.File;//added
    //import java.util.Map;//added
    import javax.swing.JButton;//added
    import javax.swing.JList;//added
    
    public class SomeClass {
    	private JButton jButton4;//added
    	private JList jList1;//added
    	private SomeArrayItem[] farray;//added
    	private File dir;//added
    	private ClassLoader ucl;
    	private SomeVirtualMachine vm;
    	private static RunningClasses runningCounts = new RunningClasses();
    	
    	public void updateNumbers() {
            int idx = jList1.getSelectedIndex();
            try {
                  int i = farray[idx].getName().indexOf(".class");
                  String cName = farray[idx].getName().substring(0,i);
                  Class c = Class.forName(dir.getName() + "." + cName, true, ucl);
    
                  // handle method counts
                  // :
                  // : 
    
                  // jColHeader.setText(...);    (Used to show the count of each method)
            }
            catch (Exception ex) {
                 System.out.println(ex);
            }
    
    }
    	public void simulateButtonClicked(){
    		String cName = "TestClass";
    		vm.launch( cName );
    		runningCounts.addRunningClass( cName );
    	}
    }

Similar Threads

  1. Deterministic execution of Java Threads, tool support
    By pulkittomar in forum Threads and Synchronization
    Replies: 0
    Last Post: 03-16-2011, 06:30 AM
  2. double execution of a method
    By matteo in forum AWT / Swing
    Replies: 0
    Last Post: 01-13-2011, 03:11 PM
  3. java UI web testing tool
    By roanmv in forum Forum Lobby
    Replies: 1
    Last Post: 10-15-2009, 12:20 PM
  4. Timeout for a method execution
    By pjmorce in forum Advanced Java
    Replies: 2
    Last Post: 12-22-2008, 07:51 PM
  5. Method execution time
    By javaplus in forum Advanced Java
    Replies: 3
    Last Post: 11-26-2007, 10:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •