Save Actions (Eclipse)

Tuesday, 31 December 2013, 18:20

Tags: Eclipse

The Java editor save actions are a very useful feature in Eclipse which will help you have more uniform and clean-looking code. You can configure a set of actions that Eclipse will automatically perform on a Java file when you save it. This way you can configure (part of) your coding conventions as save actions and Eclipse will correct the code if it is not compliant with these coding conventions.

My save action configuration is as follows. (Window -> Preferences -> Java -> Editor -> Save Actions)

  • Format source code is enabled to format all lines. My formatter settings are described in the blog post about the Eclipse Java Formatter.
  • Organize imports is also enabled to group similar imports and to remove unused imports. Imports can be organized without saving by clicking right -> Source -> Organize Imports or by using the shortcut Ctrl + Shift + O.
  • Additional actions
    • Remove trailing white spaces on all lines
    • Correct indentation
    • Change indirect accesses to static members to direct accesses (accesses through subtypes)
    • Change non static accesses to static members using declaring type
    • Add missing '@Override' annotations
    • Add missing '@Override' annotations to implementations of interface methods
    • Add missing '@Deprecated' annotations
    • Remove unused imports
    • Remove unnecessary casts