0

Is there a way to get a list of all executed methods of a program with a certain input? For example:

public static void main(String[] args) 
{
   Car c1 = new Car("c1");
   c1.drive();

   if(args.length > 2)
   {
       Car c2 = new Car("c2");
       c2.drive();
   }
}

public class Car()
{
   String name;

   public Car(String name)
   {
      this.name = name;
   }

   public void drive();
   {
      System.out.println(name + " is driving!");
   }
}

Now I'd like to get the executed methods with the input "1 2 3" (c1 and c2 will be created). So a list that looks like this:

void main(String[] args);
public Car(String name); 
void drive(); 
void println(String input); 
public Car(String name);
 void drive(); 
void println(String input);

Maybe even more methods will be listed because of the "+" in println or something else. Does anyone know how to create such a list automatically?

And if that is too easy for someone, it would also be great to filter the listed methods in such a way that only methods will be listed that were created by the programer and not methods that were used but not created, like println (I hope you know what I mean). The list would then look like this:

void main(String[] args);
public Car(String name); 
void drive(); 
public Car(String name); 
void drive();
dsolimano
  • 8,870
  • 3
  • 48
  • 63
  • Do you want to get stacktrace? – Mufanu May 05 '14 at 09:29
  • possible duplicate http://stackoverflow.com/questions/1069066/get-current-stack-trace-in-java – vlady May 05 '14 at 09:39
  • I am not sure if I want the stacktrace, but if I try to print the stacktrace at the end of main, I just get the methods that are currently on the stack: java.lang.Thread.getStackTrace(Thread.java:1588) Start.main(Start.java:20). But it does not show the methods that were called before (constructor of car, drive and println). Maybe I am just using it wrong, but how do I get all executed methods? – user3057508 May 05 '14 at 09:42
  • You can achieve this with [VisualVM](http://visualvm.java.net/) – Absurd-Mind May 05 '14 at 09:56
  • I think you should use debug mode to get all methods that have called before. – Mufanu May 05 '14 at 09:57
  • This sounds like you want code coverage. There is a number of tools such as EMMA, Jacaco, Covertura, Clover and some of these are supported in IDEs. I would see which tools your IDE supports. – Peter Lawrey May 05 '14 at 10:10
  • Thank you for the different answers, I think the suggestion of Peter Lawrey was exactly what I needed. With the help of EMMA I can create a code coverage report in Eclipse and export it as a .xml file. If I now look for methods with coverage > 0 the executed ones will be shown. Additionally it seems like only custom created methods (like I asked for in the initial post) will be presented in the report. So thank you again Mr. Lawrey. – user3057508 May 06 '14 at 07:51

2 Answers2

0

Reflection API can help you. Is this what you are looking?

Here is link for Java Reflect API :

http://docs.oracle.com/javase/tutorial/reflect/

Alexis Pigeon
  • 7,423
  • 11
  • 39
  • 44
h4k3r
  • 91
  • 5
0

You can have Aspects configured on your code for tracking purpose. But I dont see any way you can count methods from other library until you compile those library with your aspects applied.

Vinay Lodha
  • 2,185
  • 20
  • 29