
Eclipse and Liferay coding conventions
Eclipse can help you comply with Liferay coding conventions.
Liferay IDE formatter profile for Eclipse #
See main article: Liferay IDE Java Code Style Formatter profile
Run the Source Formatter check from within Eclipse #
See main article: Source Formatter
80 character limit visible at all times #
- Menu: Preferences
- Tree: General > Editors > Text Editors
- Check the box: Show print margin
- Field: Print margin column, value: 80 (default already)
Basic cleanup everytime you save a source file #
- Menu: Project > Properties
- Tree: Java Editor > Save Actions
- Check the box: Perform the selected actions on save
- Uncheck the box: Organize imports
- Check the box: Additional actions
- Button: Configure…
- Tab: Code Style
- Uncheck the box: Use modifier 'final' where possible
Remove trailing whitespace automatically #
- Tab: Code Organizing
- Check the box: Remove trailing whitespace
Remove "this" automatically #
- Tab: Member Accesses
- Check the box: Use 'this' qualifier for field accesses
- Select: Only if necessary
- Check the box: Use 'this' qualifier for method accesses
- Select: Only if necessary
Organize imports automatically #
Download eclipse-liferay.importorder
- Menu: Project > Properties
- Tree: Java Code Style > Organize Imports
- Button: Import…
- Open: eclipse-liferay.importorder
This list is just a starting point. Add more packages to the list as they come up at source files.
Organize the source file being edited #
- Menu: Source > Organize Imports
Organize imports on save #
- Menu: Project > Properties
- Tree: Java Editor > Save Actions
- Check the box: Organize imports
Remove unused imports on save #
- Menu: Project > Properties
- Tree: Java Editor > Save Actions
- Button: Configure...
- Tab: Unnecessary Code
- Check box: Remove unused imports
Extra Formatter settings that help improve compliance #
- Menu: Project > Properties
- Tree: Java Code Style > Formatter
- Field: Active profile, value should be Liferay [plug-in]
- Button: Edit...
No new line at end of file #
- Tab: New Lines
- Uncheck the box: at end of file
No blank line at beginning of method body #
- Tab: Blank Lines
- Field: At beginning of method body, value: 0
No forced split on throws clause #
- Tab: Line Wrapping
- Tree: Method Declarations > 'throws' clause
- Uncheck box: Force split, even if line shorter than maximum line width
- Tree: Constructor declarations > 'throws' clause
- Uncheck box: Force split, even if line shorter than maximum line width
No white space after type cast or generics closing bracket #
- Tab: White Space
- Tree: Expressions > Type casts
- Uncheck box: after closing parenthesis
- Tree: Parameterized types > Type arguments
- Uncheck box: after closing angle bracket
Preserve manually inserted line breaks #
This will prevent Eclipse from rearranging line breaks on already compliant source files.
- Tab: Line Wrapping
- Check the box: Never join already wrapped lines
Limitations #
The Eclipse Java Code Formatter has several glitches that impact compliance with Liferay coding standards. If you'd like to help see it improved, please vote for this Eclipse bug. The majority of line wrapping glitches can be worked around by checking "Never join already wrapped lines" as explained earlier. With this on, you can fix line breaks manually and move on.
There are also a few rules that are simply not implemented by the Eclipse Formatter at all, so there's no configuration option to enforce them. Because of that, "Source > Format" on a whole file will usually result in several differences against sources already processed by Liferay's Source Formatter. Therefore if you want to work with format on save at all times, it's recommended to enable it for edited lines of code only.
Enabling format on save for edited lines only #
- Menu: Project > Properties
- Tree: Java Editor > Save Actions
- Check: Format source code
- Select: Format edited lines