Eclipse is the free IDE of choice for most Java developers. Here are some tips and tricks on customizing your Eclipse installation. These are generic and pertinent to any type of Java project.
Warnings and Errors
Open "Preferences" under the "Window" menu.
There in Java -> Compiler -> Errors/Warnings, switch all ignore to warning. We don't want to ignore any possible source of errors. Here's a shortlist of exceptions. These are mostly personal:
- Serializable class without serialVersionUID. I prefer to ignore this. The reason for the exception will be easy to identify. We shouldn't serialize and deserialize between versions anyway.
- Unqualified access to instance field. Adding "this." contributes to make the code more difficult to read. Since any variable shadowing is already marked as warning and my methods are very short with few variables, there's no point in using it.
ignore, info, warning and error. I still set the minimum level to warning for all except a few and it's among the first things I do after new Eclipse installation. There is little downside to setting all possible problems at the warning level, so why not take advantage of it and get rid of a whole class of errors at compile time?
Installed JRE
Use a JRE that's under your JDK installation. You will be able to access the Java libraries' source. Additionally, tools.jar will be in the classpath.
Findbugs
This static code analyzer extension is a must in any project.
The following tip has no relation to Eclipse, but on the topic of findbugs and if you use maven, I would use the "findbugs-maven-plugin" for the build. It will fail a build unless no bugs are detected (you can still set an exclusion filter for some of them if you want). I see no reason not to use it.
The following tip has no relation to Eclipse, but on the topic of findbugs and if you use maven, I would use the "findbugs-maven-plugin" for the build. It will fail a build unless no bugs are detected (you can still set an exclusion filter for some of them if you want). I see no reason not to use it.
Other generic plugins
Any developer has to use a source control system and a code coverage tool.
For me it makes more sense to use source control outside of the IDE (so I don't need an extension for it) and to use a tool for code coverage within the IDE. Others may want to integrate source control in the IDE too. For code coverage I use Atlassian's "Clover". EclEmma is another popular choice. In Eclipse Oxygen (and, presumably, any newer version) EclEmma is already included.
When you run Clover, the code will be marked up in red and green. To switch the coloring off, go to the "Coverage Explorer" view and click on "Hide coverage information" on the top right.
Their definition can be found in Preferences in Java -> Editor -> Templates.
Common usage templates already defined in eclipse are sysout, syserr and foreach.
This stackoverflow post shows several useful templates that we can add to our installation. I would include most of them in my editor. The large multiline ones should always be utility functions though.
Some of the useful ones are already defined in Eclipse but not activated and / or partially wrong. Toarray is an example of that. Activate it and edit it to look like this.
Learn and Create Useful Editor Templates
Using editor templates will save you time. It is well worth learning a few of them. They work by typing their alias and pressing ctrl+space to expand them.Their definition can be found in Preferences in Java -> Editor -> Templates.
Common usage templates already defined in eclipse are sysout, syserr and foreach.
This stackoverflow post shows several useful templates that we can add to our installation. I would include most of them in my editor. The large multiline ones should always be utility functions though.
Some of the useful ones are already defined in Eclipse but not activated and / or partially wrong. Toarray is an example of that. Activate it and edit it to look like this.
${collection}.toArray(new ${type:elemType}[${collection}.size()]);
Go ahead and include semicolons in the templates too, we don't want to write monster lines anyway.
In general we shouldn't work with arrays at all in Java, but if we are using third party libraries we will be converting arrays into lists and viceversa at the boundaries of our code.
Add then, the reverse of the previous: arraytolist
List<${type}> ${name:newName(java.util.List)} = Arrays.asList(${array});${:import(java.util.List, java.util.Arrays)}
In the description you should indicate that this doesn't work, specifically, for Java 1.5.
We will then create arraytolist8. With this definition, in Java 8, you can also convert from arrays of primitives (which is not possible with the previous one).
List<${type}> ${name:newName(java.util.List)} = Arrays.stream(${array}).boxed().collect(Collectors.toList());${:import(java.util.List, java.util.Arrays, java.util.stream.Collectors)}
Miscellaneous Tips
- Uncheck "Always show Welcome at start up" on the welcome page. Some versions of Eclipse have a weird bug that cause block selection and movement with control, shift and the arrow keys not to work. Since these are pretty common actions, make sure to do this.
- If you add or remove views or otherwise customize one of your perspectives, remember to save it by right-clicking on the perspective icon on the top right. In fact after you have installed all your desired plugins, you probably want to have some of the views in your window (eg. findbug's bug explorer or the coverage view) so most of the time you'll want to save a customized perspective.
- In Eclipse Neon and Oxygen, the two latest versions as of this post, the default tab size and number of spaces of indentation is four. This seems to be too much for most programmers. You can change it by browsing to "Java", "Code Style", "Formatter" and create a custom-defined profile. I personally create a custom one based on "Java Conventions" and set the number of indentation spaces to three.
- Export the preferences to a file for future reference. Go to File -> Export and type Preferences in the box. Save them to your company's wiki website, email or any other place related to your work.
- Keep a sample project with the configuration you normally use in your projects. For example a sample pom.xml file, a mercurial ignore file, etc. This is one of mine, take a look at the README. It allows me to get up to speed with the common configuration that I tend to use in a matter of seconds. On top of that, add these lines in mercurial.ini to save a few keystrokes.
[paths] startingpoint=https://bitbucket.org/jubbat/startingpointproject [alias] ;new java project with default stack: eclipse, maven, libraries, etc newproj=!hg clone startingpoint
- I find the default font size to be too small and my screen big enough. You might also want to adjust it in Preferences.
- Enable assertions globally. You can disable them when you run the code in production, but you should be using them anywhere else, in particular any time you run something in eclipse. Go to Preferences -> Java -> Installed JREs, Edit.. button and in the Default VM arguments type in -ea.
- Show .* files in the package view. This is especially useful to edit the ignore patterns of the version control tool (eg. .hgignore or .gitignore).
Shortcuts
If you are new to Eclipse, also take the time to learn the most useful shortcuts that will allow you to keep your hands on the keyboard most of the time.At the very least any developer should know these, which they will be using all the time:
Ctrl + 1 To display a list of fixes for a problem in the code
Ctrl+Shift+O To organize imports: add necessary imports in a class and remove the unused ones.
Ctrl+Shift+F To format the code
Alt+Shift+M To extract the selected lines of code as a method
Alt+Shift+R To rename a method or variable
Alt+Shift+X J To execute the program
F3 Go to definition
F5 "Step into" in debugging
F6 "Step over" in debugging
Comments
Post a Comment