0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ネストアノテーションとは

Posted at

ネストアノテーション Nested Annotation

Javaのアノテーションを他のアノテーションの要素に適用する
通常は、アノテーションはクラスやメソッド、フィールドなどの宣言に対して直接適用される

定義の例

public @interface OuterAnnotation {
    InnerAnnotation[] value();
}
  • OuterAnnotation が定義されている

OuterAnnotation は InnerAnnotation というアノテーションの要素を配列として持つ
→アノテーション要素の型を他のアノテーションにする(ネストアノテーションになる)

使用例

@OuterAnnotation({
    @InnerAnnotation(value = "Value 1"),
    @InnerAnnotation(value = "Value 2")
})
public class MyClass {
    // クラスの定義
}

MyClass クラスに対して OuterAnnotation を適用

OuterAnnotation の要素には、InnerAnnotation を要素とする配列が指定されている


アノテーションを柔軟に組み合わせて使用する際に便利

活用例

  • 複数のアノテーションをまとめて適用する場合
  • アノテーションの設定をより詳細に指定する場合
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?