Advanced coding with the JDT

Posté le dans Blog

With the last messages, you know how to create and configure properly your Java projects and how to use Eclipse to code faster than ever. Today, it is time to review advanced features of Eclipse to go even faster! We have seen how Eclipse help us when we ask for help with the proper shortcut, today we will mainly see how we can configure Eclipse to help us even more and sometime even automatically.

3.1 - Imports order

image

If you remember correctly, last time we have seen various ways to make Eclipse compute the types to import for you, thanks to the use of code completion, the quick fix or the “Ctrl+Shift+o” shortcut. With all those import declarations automatically computed, you may want to change the way the JDT orders the type imported.

To configure most of the settings in Eclipse, you can navigate to the properties of your project (right click -> Properties) or you can go to the properties of Eclipse itself (Window -> Preferences). The difference between the two solutions is that with project settings, the changes will most of the time be saved in a file under the folder “.settings” and as such, you can share it on your favorite source code management. I’ve talked about this “.settings” folder in the “1.3” section before. We will use project-specific settings as much as possible since we want to share our awesome settings with the rest of our team.

In the “Java Code Style” section, you can find a part dedicated to “Organize Imports”. In this preference panel, you can activate a project specific setting for the order of the imports in the Java source code of your project. Since I like to have my imports ordered alphabetically with the static import after, I’ve changed the default settings.

3.2 - Formatter

image

From the very same window, you can also change the settings of the formatter. By default, when using the automatic formatting, Eclipse will rely on a built in formatter profile. By switching to a project-specific one, the formatter used can not only be customized but it can also be shared with your other team members. You have access to a very wide range of settings to customize the formatter that you wish to have.

3.3 - Type filtering

When you are using the code completion in Eclipse, you have access to an enormous amount of information and you may want to filter some of those information. In the Eclipse preference window (Window -> Preferences), you can use the “Java -> Appearance -> Type Filters” panel to filter some useless type from the “Open Type” dialog, the code completion and the quick fix proposals. For example, if you are manipulating user interface types, you may want to get rid of all the “java.awt.*” types since you may not use them.

3.4 - Favorite for the imports and code completion

On the other side, have you ever had any problems with the static imports for example with “assertEquals” or “sqrt”? Eclipse never seems to find them properly the first time. In Eclipse, not only you can remove some types from the code completion, you can add favorites one too to ensure that they will be found more easily. In the same Eclipse preferences window, you have access to the list of your favorite static members and types. You just need to go to the “Java -> Editor -> Content Assit -> Favorites” section to add new favorites. When you will use the code completion again, your favorites are now accessible. I would recommended using this to add JUnit static methods but not much more as, outside of those static methods, I don’t really like having just the name of the static method. I prefer “TypeName.staticMethod()” to “staticMethod()”.

3.5 - Code conventions

In the settings of your Java project, you can also define project-specific code style to help you maintain a set conventions in your projects. You can easily set up a specific prefix for all your fields to prevent a parameter from hiding one of your fields and much more. I personally don’t use it to set up any prefix or suffix for the name of my variables, fields or parameters but it is useful to have a better name for exceptions for example (“e”, the default one, being a very bad name).

3.6 - Errors and warnings

The project specific settings of your Java project also give you the ability to choose the severity of the Java problems that can be detected by the JDT. You have access to a very wide range of problem that you can choose to ignore, treat as warnings or consider as errors. You can even make the JDT treat those problems as fatal errors preventing the compilation of the code. Some of the problems detected can be very helpful to improve the quality of your code or to prevent future issues like:

non externalized strings undocumented empty blocks boxing and unboxing conversions potential null pointer access redundant null check local variable declaration hides another field or variable classes that override equals() but not hashCode()

3.7 - Task tags

When you are working on a sizable Java project, you will have pieces of code that may not be working completely as expected. As a result, you can use a comment like “// TODO do something” or “// FIXME huge bug”. Not only Eclipse will detect those tags and create markers and tasks for them that you can use to track the remaining issue that you need to resolve but the JDT also let you define your own task tags. Those project-specific task tags can let you specify tags that match more clearly the domain of the problem. The tasks created can be seen in the dedicated “Tasks” view.

3.8 - Line delimiters and encoding

In order to minimize problems, you and your coworkers should select a specific line delimiter and encoding for all your textual artifacts. In the project-specific settings, you can choose which line delimiter and which encoding you want to use on the project.

3.9 - Javadoc errors and warning

image

We have seen how you can tell the JDT to look for additional errors and warnings in the code of your Java project but you can also check for problems in the javadoc too.

3.10 - Custom code templates

In the previous post, we have seen how we can use code templates like “foreach” or “sysout” to speed up our development. The JDT not only provides code templates but it also allows you to define new ones to help you go even faster.

There are two kinds of code templates, code style templates and editor templates. The first ones can be saved only as Eclipse-specific settings while the second ones can be also saved as project-specific settings. The code style templates, that can be saved at a project level, let you define the code generated when you are creating a new file, a new class, a new field or even a new method. Those templates can be helpful to specify a copyright that should be added to every Java files.

On the other hand, the editor templates, that can only be saved at an Eclipse level, let you define templates mostly used within the code or the Javadoc itself. You can easily customize or create very complex code templates in a matter of seconds with a powerful templates language. You can use those templates later just like regular ones.

3.11 - Escape text when pasting in a string literal

One day, you will want to paste something in a string literal that need to be escaped like the absolute path of a file in windows for example. You can let the JDT do the job for you automatically with the Eclipse-specific Java editor typing options. It’s one of those tips that is useful only once in a while but which makes you so happy when it’s there to help you.

3.12 - Save actions

Advanced JDT users will notice that I am keeping one of the most convenient option of the JDT for the end with “save actions”. Save actions are just a set of actions that will be triggered by the JDT once you save your Java file. You can define save actions as project specific settings and it will change your life. Writing imports manually was boring, using the code completion was better but sometimes we still had to use “Ctrl+o” to force the automatic resolution of the missing imports, now it can be done each time we save our file. We can also make sure that unused imports are cleared all the time. The amount of options available in the save actions is mind-blowing. Save actions are awesome, use them!

3.13 - Overriding with the code completion

While writing a Java program, you will have to override a method one day or another. You could write the signature of the overriding method by yourself but you can also use the code completion to do it in a couple clicks. You just need to use the code completion where you want to write the overriding operation. As usual, you can leverage camel case to choose more quickly the method to override.

3.14 - Copy qualified name

If you want to have the qualified name of a Java element like a class, a method or a field, you just need to use the contextual action “Copy Qualified Name”. One day, you will need it and you will love it.

3.15 - Go to line

image

An exception has been raised in your application and a very long stack trace has been printed in the console. You can click on the first line of the stack to go to the source of the problem but if you need to navigate to another position referenced in the stack trace, you can just open the type mentioned and then with “Ctrl+l”, you can go to a specific line in the currently selected editor.

Now we have seen a lot of tips and tricks to speed up Java development in Eclipse. As I have presented it in the introduction, next time we will see how we can configure the launch of the Java application and the different refactoring options available.

Auteur d'origine: sbegaudeau