2

Suppose

public class A{
    public void doSomethingInMethodA(){
        int a =0;
        System.out.println("value of a :"+a);  
    }
    public static void main(String[] args) {
        new A().doSomethingInMethodA();
        new B().doSomethingInMethodB();
    }
}
class B{
    public void doSomethingInMethodB(){
        int b =0;
        System.out.println("value of b :"+b);
        C c = new C();
        c.doSomethingInMethodC();
    }
}
class C{
    public void doSomethingInMethodC(){
        int c =0;
        System.out.println("value of c :"+c);
    }
}

Now how can I know the flow of the program programatically.

Like If I provide the name of class A(class with main method) to some parser class then the parser class should tell me the flow of the program like

Edit:

A(class with main)
             -> doSomethingInMethodA()(Class A)
             -> doSomethingInMethodB()(Class B) ->doSomethingInMethodC()(Class C)
program flow ends
where -> means it calls

I guess through reflection it is not possible because you can't ask a java.lang.reflect.Method which methods do you call?

Is it possible with StackTraceElement? I even heard of AspectJ doing something like this

So how to achieve it?

Sarbong
  • 111
  • 1
  • 1
  • 8
  • Do you want to analyze the classes and inspect all possible code paths or run the program and see step by step what is happening while it's being executed? In other words should this be a static or dynamic analysis? – Piotr Praszmo Aug 24 '14 at 14:19
  • 2
    How would that be more helpful than the source code? The source code, at least, is not ambiguous. – JB Nizet Aug 24 '14 at 14:19
  • 2
    You want to create English prose which describes a program? For that you'll need to build the equivalent of a Java compiler, whose target is English prose instead of bytecode. Your result will be flimsy at best. – Marko Topolnik Aug 24 '14 at 14:22
  • @MarkoTopolnik: Well, the op could make an attempt to compile it into the shakespeare language, nothing flimsy about that ;-) http://en.wikipedia.org/wiki/Shakespeare_(programming_language) – Patrick Aug 24 '14 at 14:40
  • @MarkoTopolnik ok ignore the essay below the code just want to know any library available to find the program flow from the main class that is the bottom line – Sarbong Aug 24 '14 at 14:43
  • @Sarbong: But you can read the program flow from the source code, right? What are you trying to solve? – Patrick Aug 24 '14 at 14:44
  • 1
    What, in your mind, does "find the program flow" mean? The complexity of program flow is on the same order as the complexity of the source code, so to get any benefit beyond rewording source code you would need to introduce some lossy transformation rules which extract one aspect or another from the it. – Marko Topolnik Aug 24 '14 at 14:48
  • @Patrick I think I'm not much clear can there be a parser which will read a main class(class with main method) analyze the classname and method it calls from the main and print the name of the className and the methods and the variables the method contains. suppose class A's main method calls someMethod() of class B so the parser must give the output like `someMethod() of class B` is called and it contains these many variable – Sarbong Aug 24 '14 at 14:53
  • @MarkoTopolnik please check the edit part may be it will clear things – Sarbong Aug 24 '14 at 15:11
  • See [this question](http://stackoverflow.com/questions/930289/how-can-i-find-all-the-methods-that-call-a-given-method-in-java). Especially first answer about [ASM](http://asm.ow2.org/). – Piotr Praszmo Aug 24 '14 at 15:16
  • Smells like the Halting Problem. – chrylis -cautiouslyoptimistic- Aug 24 '14 at 17:24

1 Answers1

0

If you want to generically record the execution flow for any given program, you can use bytecode instrumentation. Consider using java.lang.instrument and ASM to achieve your goal. Take a look at this tutorial.

It's not easy by any means, but it's certainly doable.

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124