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.