
# ビルド時のチェック

ジェネレーターはどのみちアプリケーションのアノテーション付きの宣言をすべて読むので、その間にチェックできるものをチェックします。Spring ならアプリケーションの起動時に報告する、あるいはまったく報告しない間違いが、ここではビルドを失敗させます。メッセージはソースの要素を指し、修正方法を伝えます。

## 使う理由

ビルド時に見つかった設定の間違いのコストは数秒です。起動時に見つかればアプリの一回の実行分、本番で見つかればはるかに大きなコストになります。そして、まったく失敗しない間違いもあります。何にも一致しないタイプミスのあるポイントカット、チェックできない型に付いた制約、バンドルに含まれていないアセットです。ビルドはこれらをエラーにします。

実行中のアプリケーション全体に依存するものは起動時に任されます。足りない Bean や曖昧な Bean、依存の循環は、コンテキストの起動時、自分のオブジェクトが一つも生成される前に報告されます。

## すべての問題を一度に

スキャナーは最初の不正な宣言で止まりません。ビルドの独立した問題を集め、まとめて報告します。

```text
2 problems in this build:

1. Bean class Broken must not be abstract (an abstract class is a bean only
   when it has @Lookup methods for the container to implement).
   package:app/src/broken.dart:3:16
   ...

2. Injected field clock must be settable: declare it `late final` or
   non-final.
   package:app/src/report.dart:5:15
   ...
```

各エントリーには、メッセージ、ライブラリの URI、場所があります。問題が一つだけのビルドは、その問題だけを報告します。

## AOP

すべてのアドバイスの式、すべての `@Pointcut`、すべての `@Aspect('perthis(...)')` の句は、ランタイム自身のパーサー (`spring_core_dart` の `ExpressionPointcut`) で解析されます。ビルドが受け付けたポイントカットは、起動時にも受け付けられます。構文エラーは、アスペクト、アドバイス、文字の位置を示します。

```dart
@Aspect()
@Component()
class Audit {
  @Before('execution(* *(..)) &&')
  void log() {}
}
```

```text
The pointcut of before advice Audit.log is invalid: Invalid pointcut
(unexpected end of expression) at character 21 of 'execution(* *(..)) &&'
```

未知の指定子も同じように報告され (`unknown designator 'frobnicate'`)、不正な `@Pointcut` も同様です。

```text
The @Pointcut Audit.bad is invalid: ...
```

`execution` の型パターンもチェックされます (括弧なしの `String||int`、`Animal+`)。ビルドから見えないクラス (別のパッケージのホルダークラス) の名前付きポイントカットへの参照は、ランタイムに任されます。

## バリデーションの制約

チェックできない型に付いた制約は、Hibernate Validator がバリデーション時に失敗するのと同じように、ビルドを失敗させます。

```dart
class Form {
  @NotBlank()
  int age = 0;
}
```

```text
@NotBlank on Form.age: it validates String values, not int (like Hibernate
Validator's UnexpectedTypeException). Remove it or change the type.
```

不正な属性も同様です。

| 宣言 | メッセージ |
|---|---|
| `@Size(min: -1)` | `Invalid @Size on Form.name: the min parameter cannot be negative (Hibernate Validator rejects it when validating).` |
| `@Size(min: 5, max: 2)` | `... min (5) is greater than max (2) ...` |
| `@Digits(integer: -1, fraction: 2)` | `... the length of the integer part cannot be negative ...` |
| `@DecimalMin('abc')` | `... "abc" is not a decimal number ...` |
| `@Matches('[a-')` | `... invalid regular expression "[a-" ...` |
| 要素を持たない型に付けた `@Each(...)` | `@Each on Form.tags: it constrains the elements of an Iterable or the values of a Map, not String. Remove it or change the type.` |

[バリデーションの制約](../spring-core-dart/validation/constraints.md) を参照してください。

## JSON

`@JsonValue` はクラスを一つの値として書かせるので、クラスに付けられるのは一つだけで、それが働けるメンバーに付ける必要があります。

```dart
class Isbn {
  @JsonValue()
  String get a => '';

  @JsonValue()
  String get b => '';
}
```

```text
Isbn has more than one @JsonValue (a, b); like Jackson, a class can be
written as one value only.
```

その他のケースは、それぞれ `@JsonValue on <Class>.<member> ...` として報告されます。セッター (`is a setter; mark the field or getter`)、enum のメソッド、必須パラメーターを持つメソッド、private なメンバー (`is private, so generated code cannot read it`)、static なメンバーです。`@JsonValue` と、定数に `@JsonProperty` の名前の両方を持つ enum も失敗します。[JSON](../spring-core-dart/json/index.md) を参照してください。

## Flutter のアセット

リテラルの `@Value('asset:<key>')` (`spring_boot_starter_flutter` のもの) はパッケージと照合されます。ファイルが存在し、かつ `pubspec.yaml` が `flutter: assets:` の下でそれを宣言していなければなりません。そうでなければ、アプリにそのファイルは含まれません。

```dart
@Component()
class Sounds {
  Sounds(@Value('asset:assets/sounds/tap.mp3') this.tap);
  final Resource tap;
}
```

```text
@Value('asset:assets/sounds/tap.mp3') in package:app/src/sounds.dart#Sounds:
assets/sounds/tap.mp3 is not declared in pubspec.yaml, so the app would not
contain it. Add it under `flutter: assets:`:

flutter:
  assets:
    - assets/sounds/
```

ファイルがない場合は `... there is no file assets/sounds/tap.mp3 in the package app.` になります。プレースホルダーを含むキーと他のパッケージのアセット (`packages/...`) はチェックされません。それらを知っているのは実行中のアプリだけです。

## スターター

スターターの `requires` の範囲は、使用中の `spring_boot_dart` のバージョンと照合されます。

```text
Incompatible starter: neon_cache requires spring_boot_dart ^0.8.0, but 0.7.1
is used.
Upgrade spring_boot_dart to a version in ^0.8.0, or use a version of
neon_cache made for spring_boot_dart 0.7.1 (see its CHANGELOG / pub.dev
versions).
```

不正な範囲や、`spring_boot_dart` に依存しないアプリケーションで `requires` を持つスターターも失敗します。[自動構成](auto-configurations.md#requires-のチェック) を参照してください。

## 宣言

| 間違い | メッセージ (冒頭) |
|---|---|
| mixin、enum、extension、extension type に付けた `@Component` (または `@Service` など) | `@Service on mixin Audit of package:app/src/audit.dart: only classes can be beans; ...` |
| トップレベル関数に付けた `@Bean` | `@Bean on top-level function clock of package:app/src/clock.dart: @Bean methods belong to a class with @Configuration or @Component; move it there (as an instance or static method).` |
| トップレベル変数以外に付けた `@ComponentScan` | `@ComponentScan on class AppBeans of package:app/src/beans.dart: put it on a top-level variable holding the generated module, e.g.` に続けて `@ComponentScan() final BeanModule appBeans = $appBeans;` |
| 一つのファイルに二つの `@ComponentScan` | `Only one @ComponentScan is allowed per file.` |
| 一つのディレクトリに二つの `@ComponentScan` ルート | `Two @ComponentScan roots in lib/src/: lib/src/a.dart and lib/src/b.dart. A directory has one scan root; remove one, or move it into a subdirectory (which the other then imports).` |
| private なスキャン変数、または `$` で始まるもの | `@ComponentScan variable must be public and must not start with $.` |
| 空、空白のみ、または前後に空白のある Bean 名やエイリアス | `... is empty: give a name, or remove it to use the default name 'clock'.`、または `... ' clock' starts or ends with whitespace: use 'clock'.` |
| 同じ名前の二つの無条件の Bean | `Bean name 'clock' is used by both SystemClock and FixedClock.` |
| 設定できない注入フィールド | ``Injected field clock must be settable: declare it `late final` or non-final.`` |
| nullable な `@Lazy` の依存 | `@Lazy on repo: a lazy dependency cannot be nullable; use ObjectProvider<Repository> for an optional lazy bean.` |
| extension type の注入や返却 | `Cannot inject extension type Id into ...: extension types are erased at runtime, so the container cannot ...` |
| `@Lookup` メソッドのない抽象 Bean クラス | `Bean class Broken must not be abstract ...` |

条件付きの Bean (`@Profile('dev')` と `@Profile('prod')` がどちらも `dataSource` を宣言する) は名前を共有できます。コンテナは条件に一致するものを登録します。

## private な宣言

生成コードは別のライブラリにあるので、private な宣言 (`_Foo`、`_default`) の名前を書けません。コンパイルできないファイルを書き出す代わりに、ビルドは宣言、そのライブラリ、修正方法を示すメッセージで失敗します。

```dart
@Service()
class _Greeter {}
```

```text
Bean class _Greeter must be public: the generated module cannot reference a
private class of package:app/src/greeter.dart. Make it public, or move it out
of the scanned directory.
```

private な `@Bean` の戻り値の型、注入される型 (型引数としても)、イベントの型、アドバイスのパラメーターの型、private な関数、クラス、コンストラクターであるデフォルト値も同じです。提案される修正は、public にする、スキャンの外に移す、public なスーパータイプを宣言する、デフォルトを外す、のいずれかです。

## データリポジトリ

データスターターのリポジトリ、派生クエリ、`@Query` のテキストも、「もしかして」の提案とともにチェックされます。[データ: エンティティ、リポジトリ、クエリ](data.md) を参照してください。

## クリーンな生成コード

生成コードには、アナライザーの警告も `package:lints` の推奨 lint の指摘もありません。それでも `type=lint` は無視します。自分の lint セットが異なるかもしれないからです。

## 関連項目

- [エラーと生成ファイル](errors.md)
- [データ: エンティティ、リポジトリ、クエリ](data.md)
- [自動構成](auto-configurations.md)
- [ポイントカット](../spring-core-dart/aop/pointcut.md)
- [バリデーションの制約](../spring-core-dart/validation/constraints.md)
