[Java] Google Java Style Guide を IntelliJ に設定して整形する
Google のコード規約に沿ってチェックを行い、フォーマットを実行する設定。
## スタイルガイド
Java スタイルガイドはこちらに。
> – [Google Java Style Guide](https://google.github.io/styleguide/javaguide.html)
非公式和訳。
> – [Google Java Style Guide (非公式和訳)](https://kazurof.github.io/GoogleJavaStyle-ja/)
## スタイルガイドの入手とインポート
「設定 / 環境設定ダイアログ ⌘,」から「エディター | コードスタイル」を選択。
> – [コードスタイルの構成 | IntelliJ IDEA](https://pleiades.io/help/idea/configuring-code-style.html)
英語 UI の場合は、「Preferences -> Editor -> Code Style」。
> Macintosh Download it and go into Preferences -> Editor -> Code Style. Click on Manage and import the downloaded Style Setting file. Select GoogleStyle as new coding style.
>
> [Installing the google styleguide settings in intellij and eclipse · HPI-Information-Systems/Metanome Wiki](https://github.com/HPI-Information-Systems/Metanome/wiki/Installing-the-google-styleguide-settings-in-intellij-and-eclipse#macintosh)
Google が intellij-java-google-style.xml を用意してくれている。
下記をダウンロードし、IntelliJ にインポートする。
> – [styleguide/intellij-java-google-style.xml at gh-pages · google/styleguide](https://github.com/google/styleguide/blob/gh-pages/intellij-java-google-style.xml)
以上。
## フォーマット
メニュー「コード > ファイルの整形…」から選択。
macOS では `cmd + opt + l(エル)` ショートカットでフォーマットされる。
## 例
こちらで書き殴っていたコードを整形。
> – [コレクションの操作(ジェネリクス構文) | deadwood](https://www.d-wood.com/blog/2021/04/29_12275.html)
“`java
package com.example.arrayloop;
import java.util.Arrays;
import java.util.LinkedList;
public class ArrayLoop {
public static void main(String[] args) {
var start = System.currentTimeMillis();
var msg = new StringBuilder();
var array = new LinkedList<>(Arrays.asList(50, 60, 70, 80));
for (var mps : array) {
var kmh = mps * 3600 / 1000;
msg.append(“最大瞬間風速”).append(mps).append(“m = 時速”).append(kmh).append(“km”).append(“\n”);
}
System.out.println(msg);
var end = System.currentTimeMillis();
System.out.println((end – start) + “ミリ秒”);
}
}
“`
## 補遺
プラグインのインストールは不要だった。
> – [CheckStyle-IDEA – plugin for IntelliJ IDEA and Android Studio | JetBrains](https://plugins.jetbrains.com/plugin/1065-checkstyle-idea)
> – [jshiell/checkstyle-idea: CheckStyle plug-in for IntelliJ IDEA](https://github.com/jshiell/checkstyle-idea)
checkstyle のインストールも不要だった。
> – [checkstyleのインストール手順 – IT入門書籍 スッキリシリーズ](https://sukkiri.jp/technologies/devtools/checkstyle/checkstyle_install.html)
> – [checkstyle – Importing and debugging in IntelliJ IDE](https://checkstyle.org/idea.html)