Background


A consistent formatting is good regardless of specific project. There are lots of resources that explain why. Popular code style and code conventions documents often give a justification for each particular formatting decision.

ONAP


ONAP Java Code Style is based on Google Style with two modifications
 Line length  Indentation size

Link to onap code style: Java code style


Challenges


The challenge is to find a tool that would take care of code formatting automatically. Unfortunately, a generic tool never covers all possible cases of a particular style (e.g. Google’s).
We have tried the tools often mentioned in various forums, including the ONAP Wiki page above.
 The IntelliJ formatting for Google Java Style is for an old version of IntelliJ, and affects also the formatting of other languages (Python, JavaScript, etc.), which we want to avoid.  The Google Java Format plugin has the tab size and line length hard-coded, so that we had to fix and re-package it. Surprisingly, it also seems to contradict the Google Style Guide in some cases.

Solution


The home-built solution consists of two tools:
1. IntelliJ Checkstyle plugin coupled with a modified version of Checkstyle configuration for Google Style, which was made to comply with the ONAP requirements. 2. Thoroughly reviewed IntelliJ code formatting configuration for Java – aligned with the ONAP requirements as much as possible.
Setup
1. Save the attached onap_checkstyle_configuration.xml and onap_intellij_java_formatting.xml on your local disk.

2. Install https://plugins.jetbrains.com/plugin/1065-checkstyle-idea.

3. Go to File > Settings > Other Settings > Checkstyle.

4. Add a new Configuration File (green plus sign on the right).

5. Give it a name (e.g. “ONAP Checks”) and browse for the saved onap_checkstyle_configuration.xml.

6. Check the Active checkbox on the left.

7. Make sure “Only Java sources (including tests)” is selected for “Scan Scope” (top right).

8. Go to File > Settings > Editor > Code Style > Java.

9. Click the cogwheel to the right of “Scheme”, and select Import Scheme > IntelliJ IDEA code style XML.

10. Browse for the saved locally onap_intellij_java_formatting.xml; give the new configuration a new name, or leave it as is.
The base Checkstyle configuration used in the solution must merged once in a while with the original one, which may be updated: https://github.com/checkstyle/checkstyle/blob/master/src/main/resources/google_checks.xml

onap_intellij_java_formatting.xmlonap_checkstyle_8_23_configuration.xmlonap_checkstyle_8_24_configuration.xml
Usage

  •  You will see a new Checkstyle toolbar around the edge of the IntelliJ window. 
  • Results of running Analyze > Inspect Code… will now include a Checkstyle section. 
  • Checkstyle violation will be highlighted in the editor.
  •  Running Code > Reformat Code will apply the ONAP formatting.
  • It can be run on a single file or an entire directory.
    Keep in mind that it is impossible to configure the IntelliJ formatting to cover 100% of cases, so sometimes you’ll have to format manually. Our goal was to make IntelliJ at least accept the manual formatting – if it complies with the ONAP style (and the Checkstyle configuration). Please contact me if you have encountered a situation when IntelliJ forces formatting that is not accepted by Checkstyle.
  • No labels

4 Comments

  1. Hi Oren Kleks ,

    Are we missing the xml attachments from Step 1 above?

    1. Files were added to the document.

      Thanks

  2. It seems that current attached Checkstyle configuration is not compatible anymore with Checkstyle 8.24 because of

    com.puppycrawl.tools.checkstyle.api.CheckstyleException: cannot initialize module TreeWalker - 
    TreeWalker is not allowed as a parent of LineLength Please review 'Parent Module' section for this Check in web documentation if Check is standard.

    Still works with checkstyle 8.23.

    See breaking backward compatibility related to issue 2116 from releases notes.

    Oren Kleks I fixed the configuration for 8.24, attached a new onap_checkstyle_8_24_configuration.xml and renamed previous one onap_checkstyle_8_23_configuration.xml.

  3. Hi Oren Haim Zvi Kleks 

    I am getting the error, when using the check style configuration of 8.23 and 8.24 versions.

    Please find the error in below screen shot. Can anyone help me on this?