Intellij: move variables at method start - java

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.

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.

Autocomplete in eclipse like in visual studio

I know there is a Ctrl+Space dialog box that show the methods options that I have.
But if i have a method that I not fully remember the name of it and I remember only a part of the name.
For example: There is a big static class which contains tons of methods starting with "add"
add1_Buffer, add2_Render, add7_Function and so on..
now I don't remember the fully name but i remember only "Buffer".
so I want to type "buffer" press Ctrl+Space and get the "add1_Buffer" method and all the names
that contain the name "buffer" in it.
Edit:
By the way, i need it on the OpenGL api where there are tons of functions and i am not familiar with all of them i know only keywords, I was searching for something like in visual studio that is really fast and easy.
If you start typing the a in add1_Buffer and then an upper B and then press Ctrl + Space you will find the correct method.
You can then continue writing the word Buffer if there are more methods starting with add and then having an upper B.
This means that you'll have to remember at least the first part of your method but hopefully you do.
Add the CodeRecommenders plugin to your installation. That has a subwords completion, which the normal JDT code completion does not have.
Use search. From the Search menu at the top of the window, select "Java Search". You can limit your search to methods and use wildcards to search for *Buffer* if you know that Buffer is in the method name.
The shortcut Ctrl + O gives an outline of the current source. So you can view and search all your methods in your current class. Use wildcards when needed.
This merely meets you req: alt+/, just a replacement for ctrl+space
Currently there is no direct way to do that in eclipse. But i found these are helpful. here
This post resembles your's look at it. Similar one here

Is there a way to make eclipse show the defined methods in a variables in class without typing a period?

I know when typing something like "this." Eclipse will show a list of all the methods and variables of this but is their a way to show the list without having to enter a period before?
Thanks
You just need to type Ctrl + Space or look into the outline view.
No, because without the period it's not clear that you want to access a method or member or other variable. However with CTRL + Space of course you will get a selection of possibilities you might want to type (e.g. class names).

Eclipse - how to debug input parameter change

I have the following problem, we might even call it a classic one:
public void myMethod(Map<Object, Object> parameter){
someOtherObject.method(parameter);
.
.
.
someOtherThirdPartyObject.method(parameter);
}
And suddenly, in the end some method touched the input parameter Map, and I don't know where and how. Now, I know it would be desirable to make the parameter immutable, but it is not and that is the root of the problem. For instance, the methods inside myMethod are intended to perform some validations, but they do some more as well, which is wrong by design.
So, the question is how to create a breakpoint in this method where the execution pauses if an attribute of this parameter Map changes? It might be a good idea to put a conditional breakpoint after each method call, but if you have 20-odd methods, it's rather painful.
How can I debug when this input parameter is changing?
What you want appears to be called a "watchpoint". I actually didn't know this functionality existed and I used to work on the Eclipse Project!
http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Ftasks%2Fcdt_t_add_watch.htm
It looks like you'll have to figure out what fields are being editted and then set a "Write" watchpoint using the help document above.
Additionally, Eclipse highlights variables which are modified, so if you step over your method calls one by one you will be able to see which one is modifying the value (and which field is being modified) because it will be highlighted (bright yellow, by default) in the "variables" tab in the "debug" perspective. Once you know which method if modifying the data you can run debug again, but this time debug the method that changes the value and just keep repeating until you find the problem.
This is a classic problem solving scenario where you start with a very large search space and systematically and methodologically narrow it down until the search space is small enough for you to locate the problem.
If you're trying to locate a spot where your map is being modified incorrectly, you might want to first start at the higher levels of the myMethod. Put breakpoints around the methods called inside the myMethod method. At each breakpoint, look at the contents of the Map. Eclipse has a variable watch panel where you can see the contents of every variable at a specific moment in time.
When you hit the breakpoint where you notice something is wrong. Stop. You now know to dig into someOtherObject.method(parameter); assuming the data was changed at it's breakpoint.
Now, someotherObject.method will likely have other methods inside it. Put your breakpoints inside this method around all of it's function calls and repeat the process. Continue repeating until there are no more methods left. Eventually, you will narrow down the problem and have the answer.
Unfortunately, there is no magic "fix my code" button for these types of problems. It just takes good, old fashioned Sherlock Holmes style investigative skills and reasoning to eliminate areas of the code that you know aren't the problem until you're left with a smaller section that allows you to get at the root cause.
If no code modification is allowed, you can
use the watchpoints method described by acattle to watch changes at this specific map instance or
have breakpoints in the Map methods modifying its state (if you want to do that for multiple instances). It does not matter that the Map code is binary only, you can still open it using Ctrl-Shift-T (Open Type), select the methods like put(...) or remove(...) in the outline view and add breakpoints using the context menu in the outline view.

Renaming a lot of variables in many Java classes at once

I work in a pretty large Java-project (2500+ classes) that uses an old code standard where all
member variables are prefixed with "m_" (e.g m_temperature). There is really no reason for this any longer and I'd like to get rid of them but:
In order to make the change I must do all variables at once.
It must not generate any bugs.
The first naive approach of simply renaming all the "m_variable" to just "variable" will not be sufficient as that could produce name collisions between an already existing variable named "variable", and the newly renamed one.
So, to sum up:
How do I rename all these pesky member variablest without getting into trouble and are there any more problems than the one mentioned above?
Yes, I'm aware of the refactoring/renaming features within IDEs, please bear in mind that I want to do the changes to all variables matching the criteria at once and not by right-clicking on variables and renaming them one-by-one.
How about below from : mass renaming of java variables
by Simulant
click on the variable name.
1:press [alt] + [shift] + [R]
2:enter the new name.
3:press [enter] to confirm.
-->all instances of this variable will be renamed.
Theres a question on SC which is about a massive refactoring in java too. The best answer is using japvaparser and implementing a visitor to do the actual refactoring. This shouldn't be that much work for a simple rename.
To answer your second question (are there any more problems)
I would avoid global variables. Favour encapsulation such that you can localise functionality.
I would implement some unit tests such that you can verify future changes. At the moment it appears your only available check is that your project compiles properly.
I realise both of these are potentially sizable issues in an existing project. But it's worth lookingto identify possible pain points and work on those first of all.
Refactoring tool will help in this case. When you rename a variable, it will rename it in all dependent places including if it is called in different class file. While doing this, it will alert you in case if your renamed variable matches with already existing name.
However we have to do it manually for each variable.
I don't know if this can work.
But I find this link to find and replace all occurence in a project using IntelliJ.
http://www.jetbrains.com/idea/webhelp/finding-and-replacing-text-in-project.html
If you are using eclispe IDE, you can do it in easy manner.
Steps:
select the variable
right click on it
click on refactor
click on rename
even in netbeans you can follow same steps.
The IDE finds the reference and usages and replace it all.

Categories