コメントにもマークされているように、注釈キーowners
のデフォルト値""
は、空の文字列の要素で配列を作成します。また、必要であれば、空の配列のために単に値のみ「デフォルト」
OR
{}
を持つ配列を作成することにつながることになるだけでなく、そこに単一の文字列"default"
を置くことができます。
@Unfinished("Just articleware")
@Retention(RetentionPolicy.RUNTIME) // mark with Runtime retention policy
public @interface Unfinished {
enum Priority {LOW, MEDIUM, HIGH}
String value();
String[] owners() default "";
Priority priority() default Priority.MEDIUM;
}
この
@Unfinished(value = "")
public class AnnotatedClass {
public static void main(String[] args) {
System.out.println("");
}
}
でクラスに注釈を付け、その後の値を取得するためにリフレクションを使用する:あなたは、ランタイムの保持ポリシーで注釈をマークすることができ、これをテストするには
Unfinished
のキー:
public static void main(String[] args) throws Exception {
System.out.println("Testing...");
Class<AnnotatedClass> obj = AnnotatedClass.class;
if (obj.isAnnotationPresent(Unfinished.class)) {
Annotation annotation = obj.getAnnotation(Unfinished.class);
Unfinished unfinished = (Unfinished) annotation;
System.out.println(Arrays.toString(unfinished.owners()));
}
}
両方のバージョンを使用できます。 'default ''' 'は' default {""} 'と似ていますので、デフォルト値は* one *要素の配列:空文字列です。 'default {}'を設定すると、デフォルト値は* empty *配列になります。 – Pshemo
空文字列の配列です。また、空の配列の場合には、単一の文字列 "default"を '{}'に入れることもできます。 – nullpointer