-
Notifications
You must be signed in to change notification settings - Fork 38
Support for Java 21 #242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yknz
wants to merge
3
commits into
master
Choose a base branch
from
support-java-21
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Support for Java 21 #242
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ author: Future Enterprise Coding Standards | |
head: | ||
- - meta | ||
- name: keywords | ||
content: Javaコーディング規約,Java17,コーディング規約,Java | ||
content: Javaコーディング規約,Java21,コーディング規約,Java | ||
--- | ||
|
||
<page-title/> | ||
|
@@ -520,6 +520,30 @@ head: | |
ソースコード管理システム、バグトラッキングシステムで管理している内容はソースコードにコメントで記載する必要はない。 | ||
- コメントアウトされたコード | ||
ソースコード管理システムで管理されている | ||
- サンプルコードを記載する場合は、`{@snippet}`タグを利用する。 | ||
|
||
外部ファイルから引用する例: | ||
|
||
```java | ||
/** | ||
* ユーザー登録処理の例 | ||
* {@snippet file="UserRegistrationExample.java" region="registration"} | ||
*/ | ||
``` | ||
|
||
インラインでサンプルコードを記載する例: | ||
|
||
```java | ||
/** | ||
* ユーザー登録処理の例 | ||
* {@snippet : | ||
* User user = new User(); | ||
* user.setName("田中太郎"); | ||
* user.setEmail("[email protected]"); | ||
* userRepository.save(user); | ||
* } | ||
*/ | ||
``` | ||
|
||
## インポート | ||
|
||
|
@@ -1002,6 +1026,8 @@ head: | |
(o instanceof Collection && ((Collection)o).isEmpty()); | ||
``` | ||
|
||
- パターンマッチングについては[switch文・式で使用する](#switchでのパターンマッチング)ことも可能。 | ||
|
||
## 制御構造 | ||
|
||
- 制御文( `if` , `else` , `while` , `for` , `do while` )の `{ }` は省略しない | ||
|
@@ -1518,8 +1544,8 @@ head: | |
boolean off = false; | ||
switch (day) { | ||
case SUNDAY, SATURDAY: | ||
off = true; | ||
break; | ||
off = true; | ||
break; | ||
}; | ||
``` | ||
|
||
|
@@ -1531,11 +1557,47 @@ head: | |
switch (day) { | ||
case SUNDAY: | ||
case SATURDAY: | ||
off = true; | ||
break; | ||
off = true; | ||
break; | ||
}; | ||
``` | ||
|
||
## switchでのパターンマッチング | ||
|
||
- `instanceof`ではなく`switch`文や式に拡張されたパターンマッチングで記載する。 | ||
|
||
良い例: | ||
|
||
```java | ||
static String formatterPatternSwitch(Object obj) { | ||
return switch (obj) { | ||
case Integer i -> String.format("int %d", i); | ||
case Long l -> String.format("long %d", l); | ||
case Double d -> String.format("double %f", d); | ||
case String s -> String.format("String %s", s); | ||
default -> Objects.toString(obj); | ||
}; | ||
} | ||
``` | ||
|
||
悪い例: | ||
|
||
```java | ||
static String formatter(Object obj) { | ||
String formatted = "unknown"; | ||
if (obj instanceof Integer i) { | ||
formatted = String.format("int %d", i); | ||
} else if (obj instanceof Long l) { | ||
formatted = String.format("long %d", l); | ||
} else if (obj instanceof Double d) { | ||
formatted = String.format("double %f", d); | ||
} else if (obj instanceof String s) { | ||
formatted = String.format("String %s", s); | ||
} | ||
return formatted; | ||
} | ||
``` | ||
|
||
## コレクション | ||
|
||
- Java2 以降のコレクションクラスを利用する | ||
|
@@ -1602,6 +1664,44 @@ head: | |
`Arrays.asList()`で生成した`List`は、サイズのみ不変で、`set`等による値の操作が可能な`List`です。 | ||
また、`set`を行った場合、`Arrays.asList()`に与えられた配列インスタンスにも影響します。 | ||
|
||
## 順序を保持するコレクション | ||
|
||
- 要素の順序に関する操作(最初の要素や最後の要素へのアクセス、追加、削除、逆順処理など)には、`SequencedCollection`、`SequencedSet`、`SequencedMap` インターフェースで定義された**専用のメソッド**(`getFirst()`、`getLast()`、`addFirst()`、`addLast()`、`removeFirst()`、`removeLast()`、`reversed()`、`putFirst()`、`putLast()`、`firstEntry()`、`lastEntry()` )の使用を推奨する。従来の記述と専用メソッドで性能面の違いはないので、どちらを使用するか各プロジェクトで揺れがないように統一する。 | ||
|
||
良い例: | ||
|
||
```java | ||
import java.util.SequencedCollection; | ||
import java.util.ArrayList; | ||
|
||
SequencedCollection<String> items = new ArrayList<>(); | ||
items.addFirst("A"); | ||
items.addLast("B"); | ||
String firstItem = items.getFirst(); | ||
String lastItem = items.getLast(); | ||
SequencedCollection<String> reversedItems = items.reversed(); | ||
``` | ||
|
||
悪い例: | ||
|
||
```java | ||
import java.util.List; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
|
||
List<String> items = new ArrayList<>(); | ||
items.add(0, "A"); | ||
items.add("B"); | ||
String firstItem = items.get(0); | ||
String lastItem = items.get(items.size() - 1); | ||
List<String> reversedItems = new ArrayList<>(items); | ||
Collections.reverse(reversedItems); | ||
``` | ||
|
||
※ `reversed()`で取得されるのは元のインスタンスの参照であるため、要素を変更すると元のインスタンスに反映されることに注意。 | ||
|
||
- 大量の要素に対する先頭や末尾への頻繁な追加・削除操作は、`ArrayList` のような実装では要素のシフトが発生するため、パフォーマンスに影響を与える可能性があります。このような場合は、`LinkedList` や `ArrayDeque` など、**両端の操作が効率的な実装**を選択することを検討してください。 | ||
|
||
## ラムダ式・メソッド参照・コンストラクタ参照 | ||
|
||
- ラムダ式が利用できる箇所はラムダ式を利用してよい | ||
|
@@ -1994,6 +2094,50 @@ head: | |
} | ||
``` | ||
|
||
## レコードパターン | ||
|
||
- レコードパターンは、データの分解と型チェックを同時に行えるため、冗長なコードの削減、型安全性の向上、IDEによる補完やリファクタリング支援などのメリットがあります。 | ||
これにより、コードの可読性・保守性・安全性が高まるため、レコードを使用する場合はレコードパターンを用いて記述することを推奨します。 | ||
|
||
良い例: | ||
|
||
```java | ||
static void execute(Object obj) { | ||
if (obj instanceof Point(int x, int y)) { | ||
System.out.println(x + y); | ||
} | ||
} | ||
``` | ||
|
||
悪い例: | ||
|
||
```java | ||
if (obj instanceof Point p) { | ||
int x = p.x(); | ||
int y = p.y(); | ||
System.out.println(x+y); | ||
} | ||
``` | ||
|
||
## シールクラス | ||
|
||
- 明確な方針で、利用する・利用しないを統一すること | ||
shout-star marked this conversation as resolved.
Show resolved
Hide resolved
|
||
方針無く、`sealed`を利用するとコードの保守性や柔軟性が悪くなります。 | ||
各プロジェクトで、`sealed`を利用しないか、`sealed`を利用しても良い箇所について方針を決めた上で使用するようにしてください。 | ||
|
||
方針例: | ||
- プロジェクト内で使用する共通機能ライブラリに限定して使用する | ||
- 外部公開APIでは禁止し、内部ユーティリティやドメイン層のみ許可する | ||
- サードパーティ連携部分では利用しない | ||
- 全体で利用しない | ||
など、用途や公開範囲に応じて具体的な方針を決めてください。 | ||
|
||
**【補足:シールクラス(sealed classes)とは】** | ||
Javaのsealedクラスは、継承できるサブクラスを明示的に制限する仕組みです。 | ||
これにより、ドメインモデルの制約強化やパターンマッチングの網羅性チェックが可能となり、意図しない拡張や誤用を防ぐことができます。 | ||
典型的な利用例としては、状態や種類が限定されるドメイン(例:イベント種別、計算式のノード型など)の表現や、パターンマッチング(switch文・式)で全ケースを網羅的に扱いたい場合などが挙げられます。 | ||
メリットは安全性・可読性の向上ですが、柔軟な拡張が難しくなるデメリットもあるため、利用方針を明確に定めてください。 | ||
|
||
## テキストブロック | ||
|
||
次のリンクも参考にしてください。 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.