カスタムアノテーションを作成する場合は、APIExampleHereを使用Reflection
し てそれらを処理する必要があります。アノテーションの宣言方法を参照できます。
Javaでのアノテーション宣言の例は次のようになります。
import java.lang.annotation.*;
/**
* Indicates that the annotated method is a test method.
* This annotation should be used only on parameterless static methods.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Test { }
Retention
としてTarget
知られていmeta-annotations
ます。
RetentionPolicy.RUNTIME
実行時に注釈を保持し、実行時にアクセスできることを示します。
ElementType.METHOD
クラスレベル、メンバー変数レベルなどのアノテーションを構成できるのと同様に、メソッドでのみアノテーションを宣言できることを示します。
各Reflectionクラスには、宣言されたアノテーションを取得するためのメソッドがあります。
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
Returns this element's annotation for the specified type if such an annotation is present, else null.
public Annotation[] getDeclaredAnnotations()
Returns all annotations that are directly present on this element. Unlike the other methods in this interface, this method ignores inherited annotations. (Returns an array of length zero if no annotations are directly present on this element.) The caller of this method is free to modify the returned array; it will have no effect on the arrays returned to other callers.
これらのメソッドは、、、クラスに存在しField
ます。Method
Class
例:実行時に指定されたクラスに存在するアノテーションを取得するには
Annotation[] annos = ob.getClass().getAnnotations();