アノテーションのインスタンスはDynamic Proxyです。
確認してみる
こんな感じのアノテーションを定義して、
package com.example;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface FooBar {
}
こんな感じでアノテーションのインスタンスからClassを書き出すコードを書いて、
package com.example;
@FooBar
public class Main {
public static void main(final String[] args) {
final FooBar a = Main.class.getAnnotation(FooBar.class);
System.out.println(a.getClass());
}
}
実行すると次の出力を得られました。
class com.sun.proxy.$Proxy1
ご覧の通り(?)Dynamic Proxyですね。
Dynamic Proxyはどこで作られているのか
Class.getAnnotationを辿るとAnnotationParser.annotationForMapへ行き着きます。
コードを見るとアノテーションのClassを使ってDynamic Proxyが作られていることが分かります。
/**
* Returns an annotation of the given type backed by the given
* member {@literal ->} value map.
*/
public static Annotation annotationForMap(final Class<? extends Annotation> type,
final Map<String, Object> memberValues)
{
return AccessController.doPrivileged(new PrivilegedAction<Annotation>() {
public Annotation run() {
return (Annotation) Proxy.newProxyInstance(
type.getClassLoader(), new Class<?>[] { type },
new AnnotationInvocationHandler(type, memberValues));
}});
}
Dynamic Proxyを作るときに渡されるInvocationHandler実装はAnnotationInvocationHandlerです。
アノテーションに対するメソッド呼び出しは全てAnnotationInvocationHandlerによって処理されます。
アノテーションのインスタンスからAnnotationInvocationHandlerを取得したくなったらProxy.getInvocationHandlerを使います。
final FooBar a = ...
final InvocationHandler h = Proxy.getInvocationHandler(a);
System.out.println(h.getClass());
class sun.reflect.annotation.AnnotationInvocationHandler
補足情報
そもそもDynamic Proxyってなんや?という方はProxyとInvocationHandlerのJavadocをご参照ください。