0

I want to know the sequence of execution of methods. Suppose I have a class A with some methods

Class A{
    methodA(){
       line 1;
       line 2;
       .
       .
       .
       line n;
    }
    methodB(){
       line a;
       line b;
       .
       .
       .
       line n;
    }
}

Now i have a class to print messages

 Class B{
    printMessage(){
       System.out.println("method name and line number of callee");
    }
}

Now is there any way to execute printMessage() each time line 1, line 2,...line n get executed without writing printMessage() after each line in methodA() or methodB().

I already know how to debug, i just want to write some utility class for my convenience. Thank you.

9
  • 7
    Not really, no. You could look into aspect-oriented programming and AspectJ, or dynamic code rewriting, but really this isn't something you want to do; and if you think you want to do it you need to rethink how you write methods. Commented Nov 23, 2016 at 8:46
  • 2
    Seems something like debugging ... Commented Nov 23, 2016 at 8:49
  • 3
    May I ask what the purpose is for printing out each line of execution? Commented Nov 23, 2016 at 8:49
  • Thanks for your reply but if we can make this possible, i think programming will be a lot easier in terms of reverse engineering. Commented Nov 23, 2016 at 8:51
  • 2
    This looks like a XY problem. What are you really trying to do? Commented Nov 23, 2016 at 8:53

1 Answer 1

0

Yes, you can, but this is pretty complicated and it goes back to rewriting the debugger you have in eclipse (assuming your using it) and it's pretty restrictive. I made an equivalent answer in this question: Trace java bytecode stream where the OP wanted to write each bytecode line. There are a few link that might interest you.

In your case, Class B will be in another program than Class A. So you'll have an app called "myDirtyDebugger" with classB that start your other app with classA (compiled with -g) through jdb with something like the Process class. Set a breakpoint where you want (beginning of methodA()) and start doing step by step in "myDirtyDebugger", printing out each lines (you can get them with the Process class I guess).

Making a java application that starts jdb, which in turns start your app, sounds terrible, and it is. jdb is kinda the default implementation for debbuging of the JVM TI, so you can use the JVM TI to make your own (cleaner ?) debugger. I don't recommend doing it for professional purpose without someone that has knowledge in this, but it can be an interesting exercice for the curious ones.

And I doubt that developping this will be any faster than making unit tests, using a debugger or any logging framework for your prupose.


EDIT:

I thought there wouldn't be an easier way to do what you wanted. So that's why I wrote the hard way to do it. But then I thought: there are code generation frameworks, maybe there are code analysis/rewriting framewrok tools too ? Well, there is: Editing/Modifying a .java file programmatically? (not the .class file)

The Spoon framework for Java allows you to search, read and modify or generate classes, methods, and expressions in your source code. So, adding a log (or a call to another class) between every expression in one of your method doesn't sound so difficult with this. There are a few examples on their site. I didn't read thoroughly, so I can't really explain you how to do it, but it is a good start.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but i don't need such a complex approach to accomplish something like this.
@Nazim I do think so too, I edited the question with something that might be more of your interest. Honestly it is still complicated, but a lot a easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.