Value tracking in Eclipse - java

Do Eclipse's Java tools have any equivalent (built-in or plugin) to the Value Tracking inspection in ReSharper? This would be similar to a Call Hierarchy, but tracks all previous value assignments and argument passes for a particular variable.
To clarify, I'm looking for a tool that uses static analysis to trace the origin of a value, within the IDE; this is not debugging at runtime.

Use the debugger mode in eclipse is very powerful. (Add watches!)

Highlight the variable and then press Control+Shift+G or Right Click->References->Workspace will get you all of the references of a particular variable, and you can see where it was assigned or passed as an argument that way. It will show up in a window on the bottom, and a double click will take you directly to the reference.

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.

Intellij: move variables at method start

I'm using IntelliJ Ultimate for working with Java.
There are so many useful functions, but there is one that could be very good for me and that I can't find...
Sometimes I use variables without previously declaring them (at first time). Then, I use the ctrl+Enter functionality on the undeclared variables to open menu and select the option for automatically declaring. Sometimes it's just more rapid.
It works good, the problem is that the variable is automatically declared next the line where is used. Now, I want a function in IntelliJ that automatically moves all the declarations at the start of the methods where they are. Does it exists? Or how can I implement it?
There is no feature to move all declarations to the method start (and no plans to add it, because most coding guidelines recommend declaring variables as close to the usage as possible). For situations where you're trying to access a variable which is not visible because it's declared in a too narrow scope, there's a quickfix "Bring variable into scope" which will make this specific variable accessible for this specific usage.
You can of course write a plugin to move all variables to the top; plugin development documentation can be found here.
Don't know of a built-in option, though having just auto-declared the variable you can also split the declaration and shoot that up the method:
Start with the cursor on the variable name
Press Alt+Enter again to show the available auto-assists (also on the light bulb menu)
Select "Split into declaration and assignment"
This leaves the cursor on the split variable declaration, from where you can press Alt+Shift+Up repeatedly to move that line up the method
This isn't what you are looking for but maybe it will help you:
You can select some expression that you want extract to a variable, than press Ctrl + Alt + V combination, it will find all usages of this expression in your method and put it as high as it needed for all places where it is used.

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).

Finding variable value in Eclipse Debug Perspective

I am debugging a program, and once I step in an instruction, I get a list of variables in the Variables view, or if I hold the mouse on the variable, the its value is shown.
Now, I have an object that could possibly have many references to other objects, which, in turn, have their own attributes that contain other objects and so on. The search space could become very large. I would like to find where these values could be by searching the object attributes by value. Eclipse already searches these objects by attribute.
I tried EVars plugin, but it doesn't seem to be still compatible with Eclipse 4.4. Any other tools or recommendations on how to do it?
Thank you very much!
While debugging you can use the "Display" window where you can write pieces of code and "execute" them with inspect (highlight the code -> right click -> inspect).
In that window you have access to all variables of the breakpoint's context.
You could use some java 8 streams snippets to filter your objects.
https://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fviews%2Fdisplay%2Fref-display_view.htm
https://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-evaluating_expressions.htm

Is there a possibility to break on every object reference in eclipse debugger?

Suppose I have a class
public class MyClass {
private Set<String> set = new HashSet<String>();
//and many methods here
}
is there a possibility to make an eclipse debugger stop at every line where set member is used?
I haven't used Eclipse for a while, but from what I remember this was possible in the Callisto release at least. If you set a breakpoint on the line containing the declaration, and then go into the advanced properties for that breakpoint, I believe you can set options for modification and access of that variable.
Edit: I just checked with Eclipse Europa. It does work broadly as I thought; the breakpoint is called a watchpoint when you set it on a variable; and in the "Breakpoint Properties" page (accessible by right-clicking on the breakpoint's bauble in the margin, and possibly in other ways) you can determine whether the debugger should stop on "Field access" and "Field Modification". In your case, you want the first one selected.
This has been part of the eclipse debugger from very start. You just have to set a breakpoint at the line where the variable is declared. For more control you can right click on the breakpoint and select breakpoint properties where you can set if you want to stop only on Access or Modification.
Please keep in mind that modification is actually the change in the value for value types and change in reference for reference types. For example if you set a modification breakpoint (watchpoint) on a HashMap then the debugger wont stop if you add items into this hashmap since adding items doesn't change the address/reference of the variable.
Yes. You can put a breakpoint at the expression
private String propString;
The breakpoint gets another symbol and shows the tool tip "Watchpoint [Acess and modification] "
Whith Shift+Ctrl+I you can watch the value of a selected variable name when the debugger is in step mode.
You also can change variable values at runtime when the debugger is in step mode.
The eclipse debugger is a very useful and powerful tool.

Categories