-AnnotationMirror
http://d.hatena.ne.jp/masataka_k/20060701/1151765462
実はAnnotationMirrorでやらにゃならんかったのですね。ちっ。以下、JavaDocより。
The annotation returned by this method could contain an element whose value is of type Class.
This value cannot be returned directly: information necessary to locate and load a class (such as the class loader to use) is not available, and the class might not be loadable at all.
Attempting to read a Class object by invoking the relevant method on the returned annotation
will result in a MirroredTypeException, from which the corresponding TypeMirror may be extracted. Similarly, attempting to read a Class[]-valued element will result in a MirroredTypesException.
ということで、MirroredTypesExceptionを原因としたAnnotationFormatErrorにやられていますw。Class型およびClass[]型がなけりゃいいのでしょうが、Ikushipeはあいにくそればっかりなのでした。うかつにも前に今は亡きJAVA PRESSで記事書いたときより、今に至るまで知らなかった。。。
じゃあAnnotationMirrorでというところから手数の多いことになります。Fooアノテーションのクラス型の値、barをとりたい場合。。。
TypeDeclaration td = ... for(AnnotationMirror am: typeDecl.getAnnotationMirrors()) { AnnotationType at = am.getAnnotationType(); AnnotationTypeDeclaration atd = at.getDeclaration(); if(Foo.class.getName().equals(atd.getQualifiedName())) { Map<AnnotationTypeElementDeclaration, AnnotationValue> map = am.getElementValues(); for(AnnotationTypeElementDeclaration ated: map.keySet()) { if("bar".equals(ated.getSimpleName())) { AnnotationValue av = map.get(ated); Object value = av.getValue(); if(value instanceof DeclaredType) { DeclaredType dt = (DeclaredType)value; return dt.getDeclaration().getQualifiedName(); } else { throw new IllegalStateException(); } } } } }
たりーな。