- Eclipse Google Java Style Guide
- Step 1: Download the Google Java Style guide definition for Eclipse
- Step 2: Add the Google Style formatter to Eclipse
- Step 3: Configure save actions to automatically format your code when saving
- Recent Posts
- Categories
- Java Code Style and Static Analysis
- 1.1 Checkstyle Eclipse Plugin
- 1.2 Checkstyle Maven Plugin
- 1.3 Maven Eclipse Plugin
- 2 PMD
- 2.1 PMD Eclipse Plugin
- 2.2 Maven PMD Plugin
- 3. FindBugs
- 3.1 FindBugs Eclipse Plugin
- 3.2 Maven FindBugs Plugin
- 4. What’s next?
- Saved searches
- Use saved searches to filter your results more quickly
- License
- google/google-java-format
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
Eclipse Google Java Style Guide
This article shows how to use the Google Java style guide with Eclipse IDE. The benefit of doing this is that it will be easier for you to adhere to your organisation’s style guide by automatically highlighting deviations from it as you write code.
Step 1: Download the Google Java Style guide definition for Eclipse
Step 2: Add the Google Style formatter to Eclipse
- Opening Eclipse settings varies per OS. For example, on macOS open Eclipse -> Preferences.
- In the search bar on the left, type “formatter”, and select the Java -> Code Style -> Formatter menu item.
- Click “Import” and browse to the XML file you downloaded in Step 1.
- Ensure the “GoogleStyle” item is selected in the “Active profile” section.
- Click “OK”.
Step 3: Configure save actions to automatically format your code when saving
This is a game-changer!
- In the Preferences menu (same as in Step 2), type “save actions”, and select Java -> Editor -> Save Actions.
- Select “Perform the selected actions on save”.
- Select “Format source code”.
- Click the “Formatter” link and ensure the “GoogleStyle” formatter is selected as active.
- Click OK.
Now, whenever you make changes to your code, you can use the format source code menu item, or just save the file and formatting will be applied automatically.
Recent Posts
Categories
Java Code Style and Static Analysis
CheckStyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java code to spare humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard.
Which Java code style to choose? Google has published a few coding standards, include Google Java Style. Aditionally, there are xml configuration files for Eclipse and IntelliJ in the SVN repository, https://code.google.com/p/google-styleguide/source/browse/trunk.
Checkstyle Eclipse plugin has already used Google Java style by default, checkt it by clicking “Window->Preferences->Checkstyle->Global Check Configurations”, or at https://github.com/checkstyle/checkstyle/blob/master/google_checks.xml.
1.1 Checkstyle Eclipse Plugin
To check your coding style automatically in Eclipse, just install the Checkstyle Eclpise plugin. Launch Eclipse, click Menu “Help –> Install New Software”, input http://eclipse-cs.sf.net/update/, then click “Next” to install the plugin.
1.2 Checkstyle Maven Plugin
Add the following lines to pom.xml to enable Checkstyle:
org.apache.maven.plugins maven-checkstyle-plugin 2.13 com.puppycrawl.tools checkstyle 6.1.1 checkstyle validate google_checks.xml UTF-8 true true true checkstyle org.apache.maven.plugins maven-checkstyle-plugin 2.13 https://raw.githubusercontent.com/checkstyle/checkstyle/master/google_checks.xml org.apache.maven.plugins maven-jxr-plugin 2.5
1.3 Maven Eclipse Plugin
To format source code automatically according to Google Java style, import the file eclipse-java-google-style.xml into Eclipse by clicking menu “Window->Preferences->Java->Code Style->Formatter->Import”.
You can add the code style automatically via Maven, add the following lines to pom.xml:
org.apache.maven.plugins maven-eclipse-plugin 2.9 true true $ https://google-styleguide.googlecode.com/svn/trunk/eclipse-java-google-style.xml
2 PMD
Checking code style is not enough, there are many static anaylysis tools which can analyze your code and find potential bugs, for example PMD, findbugs, etc. The difference between them is http://www.sw-engineering-candies.com/blog-1/comparison-of-findbugs-pmd-and-checkstyle.
PMD is a source code analyzer. It finds common programming flaws like unused variables, empty catch blocks, unnecessary object creation, and so forth.
2.1 PMD Eclipse Plugin
2.2 Maven PMD Plugin
To enable automatically static analysis during compilation, add the following lines to pom.xml:
org.apache.maven.plugins maven-pmd-plugin 3.3 validate check cpd-check maven-pmd-plugin 3.3 pmd cpd
3. FindBugs
3.1 FindBugs Eclipse Plugin
3.2 Maven FindBugs Plugin
To configure your build to fail if any errors are found in the FindBugs report, add the following lines to pom.xml.
org.codehaus.mojo findbugs-maven-plugin 3.0.1-SNAPSHOT Max Low true check
4. What’s next?
Nowadays there are many tools that are more powerful and automatic, for example, SonarQube, Coverity. SonarQube is an open source quality management platform, dedicated to continuously analyze and measure source code quality, and has been used by many companies, I will try it in the next step.
Posted by soulmachine Dec 15 th , 2014 Java
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Reformats Java source code to comply with Google Java Style.
License
google/google-java-format
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
#950 PiperOrigin-RevId: 551370402
Git stats
Files
Failed to load latest commit information.
README.md
google-java-format is a program that reformats Java source code to comply with Google Java Style.
java -jar /path/to/google-java-format-$-all-deps.jar [files. ]
The formatter can act on whole files, on limited lines ( —lines ), on specific offsets ( —offset ), passing through to standard-out (default) or altered in-place ( —replace ).
To reformat changed lines in a specific patch, use google-java-format-diff.py .
Note: There is no configurability as to the formatter’s algorithm for formatting. This is a deliberate design decision to unify our code formatting on a single format.
IntelliJ, Android Studio, and other JetBrains IDEs
A google-java-format IntelliJ plugin is available from the plugin repository. To install it, go to your IDE’s settings and select the Plugins category. Click the Marketplace tab, search for the google-java-format plugin, and click the Install button.
The plugin will be disabled by default. To enable it in the current project, go to File→Settings. →google-java-format Settings (or IntelliJ IDEA→Preferences. →Other Settings→google-java-format Settings on macOS) and check the Enable google-java-format checkbox. (A notification will be presented when you first open a project offering to do this for you.)
To enable it by default in new projects, use File→Other Settings→Default Settings. .
When enabled, it will replace the normal Reformat Code and Optimize Imports actions.
The google-java-format plugin uses some internal classes that aren’t available without extra configuration. To use the plugin, go to Help→Edit Custom VM Options. and paste in these lines:
--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
Once you’ve done that, restart the IDE.
The latest version of the google-java-format Eclipse plugin can be downloaded from the releases page. Drop it into the Eclipse drop-ins folder to activate the plugin.
The plugin adds a google-java-format formatter implementation that can be configured in Window > Preferences > Java > Code Style > Formatter > Formatter Implementation .
- Gradle plugins
- spotless
- sherter/google-java-format-gradle-plugin
- spotless
- spotify/fmt-maven-plugin
- talios/googleformatter-maven-plugin
- Cosium/maven-git-code-format: A maven plugin that automatically deploys google-java-format as a pre-commit git hook.
- sbt/sbt-java-formatter
- googlejavaformat-action: Automatically format your Java files when you push on github
The formatter can be used in software which generates java to output more legible java code. Just include the library in your maven/gradle/etc. configuration.
google-java-format uses internal javac APIs for parsing Java source. The following JVM flags are required when running on JDK 16 and newer, due to JEP 396: Strongly Encapsulate JDK Internals by Default:
--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
dependency> groupId>com.google.googlejavaformatgroupId> artifactId>google-java-formatartifactId> version>$ version> dependency>
dependencies < implementation 'com.google.googlejavaformat:google-java-format:$googleJavaFormatVersion' >
You can then use the formatter through the formatSource methods. E.g.
String formattedSource = new Formatter().formatSource(sourceString);
CharSource source = . CharSink output = . new Formatter().formatSource(source, output);
Your starting point should be the instance methods of com.google.googlejavaformat.java.Formatter .
Copyright 2015 Google Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
About
Reformats Java source code to comply with Google Java Style.