Eclipse Optimize Imports to Include Static Imports - java

Is there anyway to get Eclipse to automatically look for static imports? For example, now that I've finally upgraded to Junit 4, I'd like to be able to write:
assertEquals(expectedValue, actualValue);
hit Ctrl + Shift + O and have Eclipse add:
import static org.junit.Assert.assertEquals;
Maybe I'm asking too much.

I'm using Eclipse Europa, which also has the Favorite preference section:
Window > Preferences > Java > Editor > Content Assist > Favorites
In mine, I have the following entries (when adding, use "New Type" and omit the .*):
org.hamcrest.Matchers.*
org.hamcrest.CoreMatchers.*
org.junit.*
org.junit.Assert.*
org.junit.Assume.*
org.junit.matchers.JUnitMatchers.*
All but the third of those are static imports. By having those as favorites, if I type "assertT" and hit Ctrl+Space, Eclipse offers up assertThat as a suggestion, and if I pick it, it will add the proper static import to the file.

If you highlight the method Assert.assertEquals(val1, val2) and hit Ctrl + Shift + M (Add Import), it will add it as a static import, at least in Eclipse 3.4.

Eclipse 3.4 has a Favourites section under Window->Preferences->Java->Editor->Content Assist
If you use org.junit.Assert a lot, you might find some value to adding it there.

Not exactly what I wanted, but I found a workaround. In Eclipse 3.4 (Ganymede), go to
Window->Preferences->Java->Editor->Content Assist
and check the checkbox for Use static imports (only 1.5 or higher).
This will not bring in the import on an Optimize Imports, but if you do a Quick Fix (CTRL + 1) on the line it will give you the option to add the static import which is good enough.

From Content assist for static imports
To get content assist proposals for static members configure your list of favorite static members on the Opens the Favorites preference page Java > Editor > Content Assist > Favorites preference page.
For example, if you have added java.util.Arrays.* or org.junit.Assert.* to this list, then all static methods of this type matching the completion prefix will be added to the proposals list.
Open Window » Preferences » Java » Editor » Content Assist » Favorites

For SpringFramework Tests, I would recommend to add the below as well
org.springframework.test.web.servlet.request.MockMvcRequestBuilders
org.springframework.test.web.servlet.request.MockMvcResponseBuilders
org.springframework.test.web.servlet.result.MockMvcResultHandlers
org.springframework.test.web.servlet.result.MockMvcResultMatchers
org.springframework.test.web.servlet.setup.MockMvcBuilders
org.mockito.Mockito
When you add above as new Type it automatically add .* to the package.

Shortcut for static import:
CTRL + SHIFT + M

Select the constant, type
Ctrl + 1 (quick fix)
Select "Convert to static import." from the drop down.
"Quick fix" has options even though it is not an error.

In Eclipse 4.9, you can static import existing invocations using a quick fix.
A new quick fix has been implemented that allows the user to convert static field accesses and static methods to use a static import. It's also possible to replace all occurrences at the same time.
More details here

Related

Stop Eclipse from cutting code lines on clean up

In Eclipse I want (for example) that code like this
public Foo bar() {
}
gets formatted to this
public Foo bar()
{
}
via the clean up function.
But to do that I have to check "Format source code" in the clean up profile.
But that also formats code like this
alert.setHeaderText("blablablablablablablablablablablablablablablabla");
to this
alert.setHeaderText(
"blablablablablablablablablablablablablablablabla");
which I absolutely do not want. Is there any possible way to stop Eclipse from cutting lines like that?
Go to Window->Preferences->Java->Code Style->Formatter. Create new formatter. Click on edit and then pick tab Line Wrapping and set Line Wrapping policy to Do not wrap.
For more clarification refer the below Link :-
http://eclipsesource.com/blogs/2013/07/09/invisible-chaos-mastering-white-spaces-in-eclipse/
You can configure the style to which code is formatted. Under
Preferences: Java -> CodeStyle -> Formatter
Then look for "Line wrapping".

How to do a specific completion action without adding some unnecessary comment ?

I have the following warning
The serializable class myClassD4 does not declare a static final
serialVersionUID field of type long
From this interesting discussion https://stackoverflow.com/a/285809/813853 I know what to do to handle this warning.
BUT my problem is kind of different, it is related to Eclipse parametrization. When I get this warning, I do right click. And choose completion action :
Add generated serial version id
the problem is that this add a comment block , something like /* ** */. That I don't want. Please, how to get rid of that ? I have looked on the configuration of Eclipse but not found it YET.
Found it :)
Window -> Preferences -> Java -> Code Style -> Code Templates -> Comments -> Fields
All I have to do is to edit the default string.
The only way that I know is to turn off comment-generation for ALL fields (=instance vairables). This is how to do it:
Windows -> Preferences -> Java -> Code Style -> Code Template .
Open Comments tab, click on Fields, click on Edit button and remove the text

IntelliJ Idea: code completion for creating method like in Eclipse

I am using Eclipse for about 5 years. Now I'm begging with IntelliJ Idea 13. I can not get used to code completion :-(
How can I create new public method?
In Eclipse i press:
pub CTRL+SPACE ENTER int CTRL+SPACE test ENTER
and get
public int test() {
}
how can I do it in Idea?
IF this scenario is important to you,
You can use IntelliJ's Live Templates
There are no built-in templates for that. You can create your own, like other answers advise. But in general, just typing the method with autopopup completion seems to require almost the same number of key presses:
pub < autopopup appears with "public" selected, hit ENTER > int test( < CTRL+SHIFT+ENTER >
You can add a 'Live Template' in IntelliJ. Go to Preferences -> Live Templates -> Select your language (if Java is not there, you can select Other), select the "+" symbol and add a new template. e.g. Abbreviation = test, and put your method in the Template text.
IntelliJ will display a message 'No applicaable contexts yet'. Define' just below the box. If you click on 'Define' you can select 'Java' in the list that shows up.
In your editor, type the template abbreviation and press the tab key.

Eclipse Advanced code template

I have been researching about how to create my own Eclipse's editor template or customize code template to fit my need but to no avail.
Here is my requirement:
I need to have to this kind of script.
${class} ${classname:newName(class)} = new ${class}();
${classname}.toString();
So when I try to use use this. I can automatically generate the code below just by typing "MyClass" to the ${class} part of the code
MyClass myClass = new MyClass();
myClass.toString();
But just by using that script. "myClass" cannot be automatically generated when I input the class name "MyClass"
So for example, I will have
MyClass classname = new MyClass();
classname.toString();
I have found these resources, but it didn't help me. Can anyone point me to the right direction as I can't seem to find a solution for this problem.
http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fpreferences%2Fjava%2Fcodestyle%2Fref-preferences-code-templates.htm
Can you define your own template variables in Eclipse
Is there a Eclipse template variable for short version of enclosing type name
OR, is there a way to create my own Eclipse's "Generate Getters and Setters..",
which can be achieved my right clicking on any eclipse source file -> Source -> "Generate Getters and Setters"
This is the best I've found so far: https://marketplace.eclipse.org/content/fast-code-eclipse-plugin
It does exactly what I want, but it lacks polish and documentation. I've also had it crash on me to the point where I needed to reinstall it.
But it's still the best I've found so far.

Rename multiple files in eclipse

How can I rename multiple files in eclipse and update their references as well ?
For ex. I got these classes in many packages in my project :
com.a.b.c.Fooclass1
com.a.b.c.Fooclass2
com.a.b.c.Fooclass3
com.a.b.c.BarFooclass1
com.a.b.c.Dontworryclass
I want to replace Foo with BAR.
How can I replace Foo with BAR in the class names of all those classes in my project, which have Foo in their name.
Edit : I really know how to use refactor in eclipse for a single file ! I wanted to know a solution for multiple files.
This might help some other people crashing in here. If you wanna replace multiple file names use powershell:
Get-ChildItem -Filter “*currentfilename*” -Recurse | Rename-Item -NewName {$_.name -replace ‘currentfilename’,’newfilename’ }
Right click on file name -> Refactor -> Rename -> Use the new name then press enter. Confirm any prompts. This will change any references. But I am afraid there is no way to do that for multiple files.
I can tell you in windows explorer, Select your first folder from the list, hit F2, paste or append to the file name, then hit tab, paste or append to the second file name, hit tab and repeat....

Categories

Resources