カスタムコメント(一)
以前はいろいろな注釈を使っていましたが、最近のプロジェクトでは、ユーザー定義の注釈が使われています.ここにメモしてください.
カスタムコメントは簡単です.@interfaceを使えばいいです.
@Target:注釈が有効になる対象範囲を限定し、ElementTypeを使って列挙する.
カスタムコメントの場合は、パラメータを指定せずにパラメータを指定することもできます.
1.パラメータがない
使用できます
PS:@Inheritedの注釈を使う時、サブクラスと言ってこの注釈を引き継ぐことができますが、自分で試してみたら、役に立たないようです.研究が必要です.
は、ブログを見つけました.話しました.見てもいいです.
子類は親類のannotation-Inheitedを継承しますか?
カスタムコメントシリーズ:
カスタムコメント(一)
カスタムコメント(二)
カスタムコメントは簡単です.@interfaceを使えばいいです.
package org.ygy.demo.annotation;
public @interface Hello{
String value();
String info();
}
以下では、ユーザー定義の注釈に使用されるものを紹介します.@Target:注釈が有効になる対象範囲を限定し、ElementTypeを使って列挙する.
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,
/** Parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE
}
@Retens:構造を維持し、戦略を維持するためにRUNTIME、CLASS、SOURCEがあります.package java.lang.annotation;
public enum RetentionPolicy {
/**
* 。
*/
SOURCE,
/**
* , VM 。
*/
CLASS,
/**
* class , VM , 。
*/
RUNTIME
}
@Inherited:サブクラスはこの注釈を引き継ぐことができるという意味です.カスタムコメントの場合は、パラメータを指定せずにパラメータを指定することもできます.
1.パラメータがない
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}
2.パラメータが一つしかない場合は、value=を指定してもいいし、デフォルトを維持してもいいです.@Inherited
@Target({ElementType.FIELD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Hello{
String value();
String info();
}
@Inherited
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Hello{
String value();
String info();
}
3.パラメータ時配列使用できます
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
@Target({ElementType.FIELD})
4.標準値を使う@Inherited
@Target({ElementType.FIELD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Hello{
String value();
String info() default "haha";
}
defaultを使用すればいいです.標準値を使用した後、この注釈を呼び出した時には、その属性には値を付けません.PS:@Inheritedの注釈を使う時、サブクラスと言ってこの注釈を引き継ぐことができますが、自分で試してみたら、役に立たないようです.研究が必要です.
は、ブログを見つけました.話しました.見てもいいです.
子類は親類のannotation-Inheitedを継承しますか?
カスタムコメントシリーズ:
カスタムコメント(一)
カスタムコメント(二)