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.

Nazim
  • 209
  • 5
  • 17
  • 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. – Boris the Spider Nov 23 '16 at 08:46
  • 2
    Seems something like debugging ... – passion Nov 23 '16 at 08:49
  • 3
    May I ask what the purpose is for printing out each line of execution? – swinkler Nov 23 '16 at 08: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. – Nazim Nov 23 '16 at 08:51
  • 2
    This looks like a [XY problem](http://xyproblem.info/). What are you *really* trying to do? –  Nov 23 '16 at 08:53
  • I agree with @LutzHorn. This looks like [XY Problem ](http://xyproblem.info/) problem. Do you have a use case? If yes, What is it? Ask the actual problem. – nanosoft Nov 23 '16 at 08:58
  • I don't have any XY problem, i mentioned it clearly i just want to write a convenience class to know the sequence of execution. If we write a large application at some point we got stuck finding out what's going on in process, i just want to overcome that hazard. – Nazim Nov 23 '16 at 09:19
  • @Nazim reverse engineering Java is trivial - it decompiles back to the original source code. The only thing missing are the variable names. If you need to log processing on every line to reverse engineer a Java program you may simply need to improve your core Java skills. – Boris the Spider Nov 23 '16 at 10:02
  • Thanks @BoristheSpider , i am not doing any reverse engineering, i was just wondering, the time we spent to analyze something very complex using debugger, it may be easier this way. – Nazim Nov 23 '16 at 20:27

1 Answers1

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.

Community
  • 1
  • 1
Asoub
  • 2,273
  • 1
  • 20
  • 33
  • Thanks but i don't need such a complex approach to accomplish something like this. – Nazim Nov 23 '16 at 20:31
  • @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. – Asoub Nov 24 '16 at 08:38