Use NetBeans to inspect live Java objects? - java

In Python, I'm used to being able to start a debugger at any point in the code, then poke around at live objects (call methods, that sort of thing). Is there any way, using NetBeans, to do this?
For example, I'd like to be able to break at the line foo = bar().baz().blamo() and run bar(), bar().baz() and bar().baz().blamo() to see what they do.
In Python, this is what I would do:
...
import pdb; pdb.set_trace()
foo = bar().baz().blamo()
Then it would give me a prompt where I could type things:
(pdb) bar()
... some objet ...
(pdb) bar() + 42
...

First, set a breakpoint at that line of the code (by clicking in the left margin). Then click "Debug Main Project" (or "Debug single file" in the Debug menu).
Once you hit the breakpoint, you can use the tools in the Debug menu. In particular, "Evaluate Expression" seems to be what you're looking for.

In the debugger use the "Watches" view. In there you can add things to "watch", such as variables or expressions.
So if you had a "foo" object you could do things like watch "foo.bar()" and have it call the method.
I am not aware of any Java debuggers that have a real "scratchpad" sort of thing, but there probably is one. The "Watches" is about as close as you can get in Netbeans that I have found.

if you are running in debug mode and has set the debug pointer, then
(top left) debug --> Evaluate expression [Put your expression and run]

Related

IntelliJ and SonarQube , Local Variable is redundant

IntelliJ is saying Local Variable is redundant. I placed the equation there to store it, so I can debug/see the variable before returning it. Is there any easy way to debug, or else, I would have to copy the whole variable equation in the debugger window to see its value.
IntelliJ IDEA offers several ways to evaluate expressions while in debug mode:
The obvious one, by hovering over a variable or by having a look at the automatically added local variable watches. This is what you are doing now, but forces you to change the code in such a way that you have such a variable and some static code analysis tools will complain.
Add a manual watch for the variable or expression you are interested in.
Select an expression or subexpression, right click and select Quick Evaluate Expression
Hover over a (sub-)expression, hold Alt and left click
Note that point 2–4 will re-evaluate any expression. If you have side effects or non-idempotent expressions, you might not want to do this. In that case, your only choice is a temporary and redundant variable.

Cannot find local variable 'ac'

Below my code but it is not working - refer to this screenshot of the error during debugging,
Cannot find local variable 'ac'
AccountManager am = AccountManager.get(this);
Account[] accounts = am.getAccounts();
for (final Account ac : accounts)
{
String acname = ac.name;
System.out.println("Accounts : " + acname);
}
From your screenshot I saw that you are using Android Studio and there is no problem with your code, but rather some debug settings. If you'll open Android Studio preferences and go to Build, Execution, Deployment > Debugger > Data Views you'll probably see the option Enable auto expressions in Variables view ticked, since it is ticked by default. It should look something like this:
Now, if you'll check the IntelliJ docs for that you'll find this (note that IntelliJ and Android Studio are running on the same engine in case that you are wondering why I said about IntelliJ docs):
Select this option if you want the IntelliJ IDEA debugger to automatically evaluate expressions and show the corresponding values in the Variables pane of the Debug tool window.
The debugger analyzes the context near the breakpoint (the current statement, one statement before, and one after). It does so to find various expressions in the source code (if available) such as, for example, myvar.myfield.
If such expressions don't contain explicit method invocations, the debugger evaluates them and shows the corresponding values in the Variables view.
Basically, they are saying that the IDE will check the code around a breakpoint to identify the variables and compute their values (method calls not included). That being said, when the control reaches the line where accounts variable is declared, the IDE will check the code for variables and will found the ac variable, but will fail to compute its values since at the execution point that variable isn't declared yet, hence the entire operation will end with the message that the variable cannot be found.
To fix this, you have to uncheck that option from settings or you can leave it just like this, since it will not affect your code (it is fully functional right now).

Eclipse: How to force execute a particular java statement while debugging?

I have searched and found that indeed Eclipse does not support this 'direct' feature. But, Did I stil miss something? and Is it present in other IDEs?
Let me elaborate my question more -
if a statement falls under execution flow based on an expression evaluation, then why can't we force execute it? (without the execution of the expression).
For example consider this -
... if(bool returnsABoolean) {
<execute some statement>;
}
...
Can the execution of 'if' be skipped and the statement be executed as a 'next statement'? (I obviously can control the value of 'returnAsBoolean' in the Variables view; but can I not skip (in a controlloed manner) all the statements until a particular statement in the execution?)
Highlight the code you want to run and right-click/Execute or press Ctrl+U.
Alternatively to "Execute", use "Display" (Ctrl+Shift+D) or "Inspect" (Ctrl+Shift+I) to see the result.
Looks like you want the 'Display' view - in the Debug perspective, do :
Window -> ShowView -> Display.
You can enter Java statements to execute there (you have to select the bit of text each time that you want to execute)
http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fviews%2Fdisplay%2Fref-display_view.htm
Debugging allows you to run a program interactively while watching the source code and the variables during the execution.
So debugging is nothing but executing program but inspecting the elements during execution but you can not jump into something which is not there in execution.
You can use keyboard buttons F5,F6 ,F8 etc. (For Eclipse) and other Shortcuts during debugging for your convinience but you can't jump to something directly which is not in the execution sequence.
Debugging Shortcuts:
F5 Step into
F6 Step over
F8 Resume and will take you to the break point
Ctrl+Shift+B Toggle breakpoint
Ctrl+Shift+D Display Info of current statement
Ctrl+Shift+I Inspect the selected element
You can Skip some code by the use of breakpoint you can directly jump to specific point and avoid debugging of code which you believe works fine.Or you can jump out code snippet if you want to.
The question really was to set the Instruction pointer at will. This has been discussed and through url's i pasted on the comments above - this is not an eclipse feature (yet).

Moving the instruction pointer while debugging Java in Eclipse

Can I move the instruction pointer directly to a line of my choice (within the current method) while debugging a Java program in Eclipse (Galileo)?
It's straightforward to drag the instruction pointer to the desired line within a method in Visual Studio, but I don't see a way to do that in Eclipse (and don't find anything about it in the docs or on google).
This is possible...
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jdt.doc.user/tips/jdt_tips.html
Drop to frame - When stepping through your code, you might occasionally step too far, or step over a line you meant to step into.
Rather than restarting your debug session, you can use the Drop to
Frame action to quickly go back to the beginning of a method. Select
the stack frame corresponding to the Java method you wish to restart,
and select Drop to Frame from Debug view toolbar or the stack frame's
context menu. The current instruction pointer will be reset to the
first executable statement in the method. This works for non-top stack
frames as well.
Note that Drop to frame is only available when debugging with a 1.4 or
higher VM, or the J9 VM. There are some situations where a JVM may be
unable to pop the desired frames from the stack. For example, it is
generally impossible to drop to the bottom frame of the stack or to
any frame below a native method.
This is not possible.
If you simply want to execute some code at the current place you can use the Expressions view and enter your code as an expression. The methods called by the expression evaluation will run in the current debugging context.
Moving the pointer like in Visual Studio is not possible, however workarounds are:
Going backwards to the beginning of the currently executed method:
Select the method from the debug call stack, right click -> "Drop to frame"
et voila you're back at the beginning of the method.
Now to reach your desired line select the line by clicking in it and hit ctrl+r or right click the line and select "Run to line".
These techniques are hugely helpful and reduce debugging efforts massively, enjoy!
A trick I use is to type a space in your class, somewhere safe such as in the comment line; immediately delete it and save the class. This forces the execution point to jump to the beginning of your current method. Not ideal, I admit, but it can sometimes be used as a workaround to achieve what you want.
Although in the default installation of eclipse it is not possible to do directly move the execution point like in Visual Studio, there may exist an eclipse plugin which provides that functionality somewhere. Have a search around.
I like ankon's answer best, but another option (that will only work for your specific instance -- if that) is to stop at a breakpoint on your if and modify the variable(s) evaluated in the conditional such that it returns false (from the "Variables" view, right click on a variable and click "Change Value...")
I thought that this was totally possible in older versions of eclipse, I thought I had the memory of doing it, but I guess I just implanted that memory when I worked in Visual Studio. From what I'm reading it might come to the jvm and not eclipse itself, there are pages where it's suggested that the jvm cannot handle that.
In my opinion Eclipse is many many times better than VS, I worked extensively in both and since I discovered Eclipse I was always in pain when I had to work in VS. But not having this feature is definitely hurting right now hehe.
You can jump directly to any other method call inside of the currently debugged method. Select some method call below your current instruction pointer and use "Step into selection" from the context menu.
unfortunately not possible to step forward with instruction pointer (program counter), so what you need to do instead is to introduce your own "debugging" variables that you can test on - lets say you want to step around a loop that takes too long, then add a variable and test on its increased value and then encapsulate the loop in an if with that variable. I know this is ugly, but it gets it done - or you could just develop in C++ :-)
Just right click on desired line and choose run to line.That's it...
Put the cursor on the line of your choice and either hit ctrl-R ("Run to line") or right-click and select "Run to line" from the context menu.

Dependent Breakpoints

Is it possible to add Dependent Breakpoints(Not a Conditional Bp) ( Breakpoint1 is enabled if B2 is enabled etc..) in Eclipse...
I know it is possible in Intellij IDea ..but havent found a way to get this working in Eclipse.
Thanks,
Pavan
Eclipse thus far only supports conditional breakpoints, where the execution will suspend if a condition supplied evaluates to true. Thus, you could set the conditional of your breakpoint to be
objectReference == null
and eclipse will hit that break point if and only if that condition evaluates to true. These expressions can be as complex as you would like, but they can only reference values from your source code, not values from the eclipse environment (thus, you can't hit a second break point if a first one was hit, like you originally asked for). This conditional logic can be accessed by right clicking on a breakpoint and selecting "Breakpoint Properties...".
Instead, what I've found helpful is the "Run to Line" ability ([ctrl]+R, in the Run menu). When you're insertion point is on any later line while the execution is suspended, you can use the "Run to line" command to continue execution to that point in the source as if there was a break point set there. In essence, it basically sets and unsets a temporary breakpoint on the line of code your insertion point is at. The greatest part of the command is that it works across files, so that you can hit a break point in one file, go to the file where you would place the dependent break point, [ctrl]+R to that line in the second file. Its a great way to "breakpoint" to a specific line in a file without setting a full breakpoint that will be hit everytime.
Do you mean a conditional breakpoint.
If you mean a conditional breakpoint, it is possible in Eclipse. Right click on the break point select breakpoint properties.
Then you can add the condition.
If you want to toggle a set of breakpoints at once, you can put them in a "Breakpoint Working Set". Group the Breakpoints view by "Breakpoint Working Sets" (view menu > Group By > Breakpoint Working Sets) and you can toggle the whole working set at once.

Categories