Catch the current executed method Eclipse Java - java

I have an application that was written by another guy and I have to add function there. The code is pretty complex to simply look for a method I need to change. So I wanted to ask if there is a possibility to debug the application and catch on the run what method is currently called,concrete: I want to open a graphical editor in application, add an object in this editor and get the trace of methods that are being executed during adding the object without knowing anything about the application code. Is there any possibility for that? Practically I want to know what methods are executed by JVM
Thank for your help,
Jeff

you can use a profiler to get into the code but this won't be easy. Nevertheless, it's crucial to understand the code before working on it. Eclipse offers a broad range of static code analysis tools for this, like call hierarchy, ...

Related

Eclipse Debugging Run Methods

it's probably really easy but I still don't know how to do this.
Is it possible to run a method while a program in Java is running in debug-mode in Eclipse?
While debugging, open the "Display view" to run any code you want at the position you're currently at.
See:
https://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fviews%2Fdisplay%2Fref-display_view.htm
In debug mode, it will open a new perspective and it's going to show the workflow of your Main method, including all the methods invocations during it's execution, which variable's values has changed, everything that happen to the beginning until the end of your Main class code.
May you check this link, got a lot of usefull information.

How can I get all the called method in JAVA

I have to extract algorithms from an open source software in order to include these on another software.
As a consequence, I want to know how can I get all the method which are called when I launch one of the algorithm...
I tried to run it in debug mode and navigate step by step and note all the called methods on my board but it's pretty long... I'm using eclipse and I want to know if it can be done automatically.
a quick and dirty way to do it, if it's open source, would be to throw an exception in the "deepest" function that's in the call chain, then just catch it and use printStackTrace; or make it a RuntimeException and you don't even have to catch it, just run the program once and the callstack will be printed for you.
Dont know if can match your needs, but you can use a profiler like VisualVm or JProfiler to trace all the method call executed in you application, here you can find an example with visualVm and JasperReport, another way may be define an aspect (aspectJ or spring) to intercept the method call and log it were you want look here

Eclipse - showing full call stack (like when it hits breakpoint in debugger) without putting in breakpoints?

I'm working with a legacy Java app that is new to me so one way to figure out how it works and find things easier, I have thought would be to be able to get the full stack trace after I perform actions, so as to be able to see which classes are being used based on a particular UI action. I had thought this was possible in the debugger but it seems that it only works if I insert a breakpoint and in this case part of the purpose of this is so that I don't have to know what's being called to be able to insert the breakpoint first (as this would help tell me that).
I apologize if this is a basic question, I have searched on this but I'm not finding the correct answer.
This doesn't directly answer your question, but maybe it will solve your problem better. Take a look at BTrace. It lets you instrument a running Java app and insert some basic code of your own. You could, for instance, have it write out entire method call chains to help you find your way through the app. It's somewhat similar to AspectJ, but with an entirely different purpose and requiring no change in the project source:
"BTrace is a safe, dynamic tracing tool for Java. BTrace works by dynamically (bytecode) instrumenting classes of a running Java program. BTrace inserts tracing actions into the classes of a running Java program and hotswaps the traced program classes."
A few suggestions:
Some profilers will allow you to walk from any particular method up (and sometimes down) to see what's calling and being called. I've found this surprising informative about flow, even in apps I thought I knew well.
For understanding the mainline flow, I don't think there's a better substitute for working interactively with a debugger. It will lead you into learning other important things. Not what you wanted to hear, I know. This presumes that you can rapidly restart the app when you miss a key off-ramp.
Reverse-designing large legacy apps is the one place where I use UML fairly regularly. There's too much to keep in my head to form a good big picture. If you have a UML tool that will do reverse-engineering, load it up with the app, then probably prune down hard on the classes you don't care about, because they are trivial or obvious. Arrange the diagrams in a way that helps you understand. I've used Together, Magic Draw, and Visual Paradigm in this way. Together worked the best - but it was a decade ago.
When you are in the debugger perspective, you will see a view showing the launched processes. In that view you can tell it to pause all threads of a process. Once stopped, you will be able to browse through threads to see what they are all doing. To try to catch what a particular action is doing, you would have to start the action and then quickly pause all threads.
You could always run the application with the VM arg of -verbose:class. You could then watch the console output and see what classes the VM is loading when you perform a particular action. This could possibly give you a starting place for where to place breakpoints. This won't always work depending on the scenario, but may be helpful.
Another trick you can use is to figure what classes you know that have to be involved in the code path you are trying to trap. For instance, you mentioned that it's a Java EE web app and therefore the action is likely some kind of a servlet interaction (at some level). I don't have the API in front of me, but you can place a breakpoint on the method in the response object where the output stream is retrieved. Once that breaks, you will know the code that's trying to service the request.
You can always see where a method is called by clicking "Open Call Hierarchy" from eclipse (left click on the selected method or CTRL+ALT+H ). Also, you always can inspect where a method/class is defined by clicking "Open Declaration" (left click on the selected method/class or F3).

Eclipse plugin "Operation not enabled"

I'm struggling with Java Refactoring - I need to write an Eclipse plug-in which will refactor some code. It's really difficult for me and it would be way easier if I could follow working code step - by - step. The only alike example I could find is a sample Introduce Indirection.
Unfortunately I cannot make it actually work. I get "chosen operation is not currently available" information anytime I try it.
I've imported code, "Run As -> Eclipse App" and than tried to use Introduce Ind. from Eclipse Articles. What am I doing wrong?
I'll be happy with any help :).
Check your handler class for isEnabled() method. I suppose when creating new handler the default value for this method returns false. That can be the issue.
You should be able to get that example (as written) working, or perhaps you need to open a bug. When you start their example in Run As, is there any errors in your error log?
Something else you can try is to look at some of the workbench refactorings. See org.eclipse.ltk.internal.ui.refactoring.actions.MoveResourcesHandler which sets up the move resources refactoring, and http://wiki.eclipse.org/FAQ_How_do_I_find_a_particular_class_from_an_Eclipse_plug-in%3F if you need help tracking down the SDK classes using CTRL+SHIFT+T
I've managed to run it. For others who'll face the same problem:
Check the basics:
Remember that you can chose only one method and:
It must exist in the model (http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/IJavaElement.html)
It must not have any errors
Must not be a constructor
Must not be connected to annotation declaration
And:Select a Java method in the editor outline or the Package Explorer
And it gets obvious :)
The action handler class should be impediments with related interfaces (Check for error console for identify what are the missing interfaces, most probably this would be 'IActionDelegate').

Is there a program that can tell you which class/method is being invoked

I'm working on a very big java/servlets/web project and i find it hard finding which classes and and methods is being called. sometimes it takes hours to find the right class. if there an application or plugin or technique that helps a little? im using eclipse.
edit: I'm using apache and tomcat
Regarding your comment to Bozhos answer: use a profiler on your server instance. You start profiling right before you click on a link in your client application ("the browser") and stop right after you have the correct response. Then just examine the profiler logs/views to find out, what actually happend on the server.
The Eclipse Test & Performance Tools Platform Project is worth a try.
CTRL + ALT + H, or right-click > open call hierarchy (when on a method declaration) will give you all callers, with their callers, and so on. You can also reverse the hierarchy
Right click > references > project will give you where a given class is used.
From your comment on Bhozo's answer I conclude that you do NOT mean at development time, but at runtime.
I suggest you connect a debugger to your application and pause it. You can then inspect the callstack at that time, which will usually give you an idea where to look.
To do that, run your java app with the following settings:
-Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n
Then in eclipse, add a debug configuration for 'remote' to port 8787 and execute it. Your debugger is now linked to your application. Put eclipse into the debug perspective.
Now click a link in your application, and immediately click the pause button in the debugger. You can now see the callstack. Usually, once you have a clue, you can quickly find good spots to put breakpoints. But this technique helps you get a clue :)
You might want to consider the use of AOP to add a tracing/logging aspect to certain parts of your code. This way you don't have to update your code and can simply write an aspect that logs a line for every method that is called with in example the name of the method, the class and the parameters. This aspect can of course be 'turned off' when building your production version to prevent the trace logging on production machines. If you're familiar with AOP, you can easily tweak the aspect and the pointcuts a bit to for instance only log calls to certain methods in your controller classes or something like that.
If you need more information on this solution, feel free to comment on this answer requesting more specific information or simply google for AOP and logging.

Categories