java代码格式化

Java source formatting

You are probably familiar with the Eclipse hotkeys to automatically format your source code:

  • Correct Indentation: Ctrl + I
  • Format: Ctrl + Shift + F

These are quite helpful and can save a great deal of time. However, with Eclipse you can take the whole idea of automatically formatting the source code to the next level.

Formatter configuration

At first, one can configure the formatting rules in Eclipse preferences > Java > Code Style > Formatter. You can create profiles and customise these to your needs:

Formatter

Source formatting rules are mostly a matter of taste. I usually stick to the default profiles because that is what most Java programmers are accustomed to. Purists can also choose the pre-configured Java Code Conventions. As negligible as the exact formatting rules are, what matters is that every programmer in the project adheres to the exact same set of formatting rules. Otherwise they will re-format everybody’s code all the time. Seeing the actual changes in the code will consequently be like looking for a needle in a haystack.

So, how can one make sure that everybody uses the same formatting rules? It is easy: You can configure a project-specific formatter in the project properties under Java Code Style > Formatter:

Project specific formatter

These settings are saved in a .settings folder in the project folder. You can add this folder to your version control system to make everybody use the exact same formatter settings:

project
`-- .settings
	|-- org.eclipse.core.resources.prefs
	|-- org.eclipse.core.runtime.prefs
	|-- org.eclipse.jdt.core.prefs
	`-- org.eclipse.jdt.ui.prefs

Also, it’s highly recommended to configure the text file encoding and the line delimiter for the project under Resources. This is especially important for projects with developers using different operating systems with different default encodings. I generally recommend UTF-8 and Unix line endings, but again, that is a matter of taste. As long as everybody uses the same settings, everything will work out fine:

Eclipse resource settings

Save Actions

So, what’s next? It’s time for save actions! Eclipse can automatically apply source code formatting and additional code cleanups whenever you save a source file. You can activate this feature in the project properties under Java Editor > Save Actions:

Save actions

This way, whenever you save a source code file the following steps are performed:

  • the code is formatted
  • imports are organized
  • the final modifier is added to fields that are only set in the constructor
  • fields that overwrite a subclass method are annotated with @Override: When a subclass changes and doesn’t offer an overwritten method anymore, you get a compiler warning. This is a highly recommended cleanup as it makes your code more robust against changes in library APIs.
  • unnecessary casts are removed

The most apparent advantage is that everything is consistently formatted. In my experience, consistent formatting makes it much easier to read existing source code.

Again, the save action configuration is saved in the .settings folder and applies to all users if you add these files in your projects version control system (you can also configure these just for your workspace in Eclipse preferences > Java > Editor > Save Actions).

Trailing whitespaces

To perfect source code formatting when it comes to versioning the files, we need to take care of one last detail: trailing whitespaces. I never cared about this until I had to merge a large amount of code for a project with many parallel branches. Here is the problem: An empty line is different from an “empty line with a tab character”. This is also true for white spaces after source lines. Especially if developers use different editors with varying policies regarding to white space handling (most indentation is automatically added while typing), this can cause a lot of trouble when merging code:

Trailing whitespace problem

The general solution for this problem is to strip all trailing/unnecessary whitespaces. In Eclipse you can do this with the Any Edit plug-in. This removes all trailing whitespace automatically:

Remove trailing whitespaces with AnyEdit

Summary

All this might sound negligible, however in my experience, the effort to figure this out and to properly configure it pays off perpetually whenever you write or change a line of code. Happy formatting!


本文转自:http://www.ralfebert.de/archive/java/source_formatting/

原文地址:https://www.cnblogs.com/nizuimeiabc1/p/4254122.html