#author("2026-03-17T05:42:47+00:00","","")
#author("2026-03-17T05:43:17+00:00","","")
// 下階層用テンプレート
#topicpath
----
//ここにコンテンツを記述します。
#contents

AbstractTypeDeclaration は型(クラスやインタフェース)やEnum,Annotationを表現する抽象クラスです。

#ref(abstract.png)
 List<AbstractTypeDeclaration> CompilationUnit#types();
を呼び出すことでCompilationUnitに記述されている型情報を取得することができます。戻り値がListになっているのは、一つのUnit((=ファイル?))に複数のクラスが書いてある場合があるからですね。ちなみに、[[あるソースコードに書かれているクラス名を全て取得する>Eclipse/プラグイン開発のTIPS集/ソースコードを解析するパーサASTParser/TypeDeclaration#f465918b]] これを使ってもソース内のクラス名などは取得可能です。あちらは
 ICompilationUnit =element; <-パッケージエクスプローラなどから取得する
 IType[] types = element.getTypes();
といってITypeインタフェースを経由して型情報にアクセスできますね((そもそも、なんで2通りのやり方があるのかはよくわからんです))。。

**Annotationにアクセスしてみる [#s6a7f75d]
対象のCompilationUnitがAnnotationだった場合に、正しくアクセスできるか試してみました。

対象のコードはこんな感じ
-nu.mine.kino.annotations.Service
 package nu.mine.kino.annotations;
 
 @Inherited
 @Target(ElementType.TYPE)
 @Retention(RetentionPolicy.SOURCE)
 public @interface Service {
   boolean singleton() default false;
 }

上のコードを解析するソースはこんな感じです
 ASTParser parser = ASTParser.newParser(AST.JLS3);
 // parser.setResolveBindings(true);
 parser.setSource(unit);
 parser.setKind(ASTParser.K_COMPILATION_UNIT);
 
 // parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);// やったら↓これはNG
 CompilationUnit root = (CompilationUnit) parser
         .createAST(new NullProgressMonitor());
 List<AbstractTypeDeclaration> types = (List<AbstractTypeDeclaration>) root
         .types();
 
 for (AbstractTypeDeclaration type : types) {
     System.out.println("タイプのクラス名: " + type.getClass().getName());
     System.out.println("クラス名: " + type.getName());
     // System.out.println("TypeのValue: " + type);
     System.out.println("---------");
 
     // そのクラス自体がAnnotationかどうか。
     if (type instanceof AnnotationTypeDeclaration) {
         System.out.println("Annotationです");
     }
 }

実行結果です
 タイプのクラス名: org.eclipse.jdt.core.dom.AnnotationTypeDeclaration
 クラス名: Service
 ---------
 Annotationです

まだAnnotationらしいデータにはアクセスしてないですが、Annotationだってこと自体は確認できました。

次はその型のModifierやAnnotationにアクセスしてみます。Modifierとはいわゆる
 public
 protected
 private
 static
 abstract
 final
 native
 synchronized
 transient
 volatile
 strictfp
などの修飾子のことですね。AnnotationはまさにAnnotationですね。先の型@Serviceはそれ自体もAnnotationですが、
 @Inherited
 @Target(ElementType.TYPE)
 @Retention(RetentionPolicy.SOURCE)
 public @interface Service {
 ...
などとAnnotationが付与されているため、それらの値も取得できそうです。

実際にModiferやAnnotationにアクセスするメソッドは
 List<IExtendedModifier> modifiers = type.modifiers();
です。実際にコードは以下の通りです。

 // http://dev.eclipse.org/newslists/news.eclipse.tools.jdt/msg19931.html
 List<IExtendedModifier> modifiers = type.modifiers();
 // ただし↑は、 @inheritedされて継承されたAnnotationはModifiersとしては返却されないようだ。。
 for (IExtendedModifier modifier : modifiers) {
   System.out.println("modifier: " + modifier);
   if (modifier.isAnnotation()) {
     System.out.println("isAnnotation");
     Annotation annoModifier = (Annotation) modifier;
     if (annoModifier.isSingleMemberAnnotation()) {
       System.out.println("isSingleMemberAnnotation");
       Expression value = ((SingleMemberAnnotation) annoModifier)
           .getValue(); // このクラスにAnnoされている値。
       System.out.println(value);
     } else if (annoModifier.isMarkerAnnotation()) {
       System.out.println("isMarkerAnnotation");
     } else if (annoModifier.isNormalAnnotation()) {
       System.out.println("isNormalAnnotation");
       List<MemberValuePair> list = ((NormalAnnotation) annoModifier)
           .values();
       for (MemberValuePair pair : list) {
         System.out.println("Annotationされている情報: "
             + pair.getName() + "=" + pair.getValue());
       }
     }
   } else {
     Modifier normalModifier = (Modifier) modifier;
     System.out.println(normalModifier.getKeyword());
   }
 }

結果は以下の通り
 modifier: @Inherited
 isAnnotation
 isMarkerAnnotation <- マーカー
 
 modifier: @Target(ElementType.TYPE)
 isAnnotation
 isSingleMemberAnnotation <-SingleMember
 ElementType.TYPE <-そのValue
 
 modifier: @Retention(RetentionPolicy.SOURCE)
 isAnnotation
 isSingleMemberAnnotation <-SingleMember
 RetentionPolicy.SOURCE <-そのValue
 
 modifier: public
 public

SingleMemberじゃないAnnotationはなかったので表示されませんでしたが、NormalAnnotationの場合は
 List<MemberValuePair> list = ((NormalAnnotation) annoModifier).values();
 for (MemberValuePair pair : list) {
   System.out.println("Annotationされている情報: "
     + pair.getName() + "=" + pair.getValue());
 }
などとKey/Value形式でアクセスができそうです。


----
この記事は
#vote(おもしろかった[2],そうでもない[0])
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 13:49:57};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 13:50:27};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:22:46};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:23:16};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:23:46};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:24:30};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:24:32};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:25:00};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:25:02};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:25:30};
- response.write(9758583*9674496) -- [[RDFYjolf]] &new{2026-03-17 (火) 14:27:16};
- response.write(9758583*9674496) -- [[RDFYjolf]] &new{2026-03-17 (火) 14:27:46};
- response.write(9758583*9674496) -- [[RDFYjolf]] &new{2026-03-17 (火) 14:28:16};
- 1 -- [[response.write(9953692*9668795)]] &new{2026-03-17 (火) 14:28:46};
- 1 -- [[)]] &new{2026-03-17 (火) 14:28:51};
- '" -- [[RDFYjolf]] &new{2026-03-17 (火) 14:29:02};
- ;assert(base64_decode('cHJpbnQobWQ1KDMxMzM3KSk7')); -- [[RDFYjolf]] &new{2026-03-17 (火) 14:29:02};
- 1 -- [[response.write(9953692*9668795)]] &new{2026-03-17 (火) 14:29:16};
- 1 -- [[)]] &new{2026-03-17 (火) 14:29:21};
- '" -- [[RDFYjolf]] &new{2026-03-17 (火) 14:29:32};
- 1 -- [[response.write(9953692*9668795)]] &new{2026-03-17 (火) 14:29:46};
- 1 -- [[)]] &new{2026-03-17 (火) 14:29:51};
- ;assert(base64_decode('cHJpbnQobWQ1KDMxMzM3KSk7')); -- [[RDFYjolf]] &new{2026-03-17 (火) 14:30:02};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:30:16};
- 1 -- [['"]] &new{2026-03-17 (火) 14:30:32};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:30:46};
- 1 -- [['"]] &new{2026-03-17 (火) 14:31:02};
- 1 -- [[;assert(base64_decode('cHJpbnQobWQ1KDMxMzM3KSk7'));]] &new{2026-03-17 (火) 14:31:02};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:31:16};
- 1 -- [[;assert(base64_decode('cHJpbnQobWQ1KDMxMzM3KSk7'));]] &new{2026-03-17 (火) 14:31:32};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:32:11};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:32:32};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:33:02};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:33:15};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:33:45};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:34:15};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:42:15};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:42:47};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:43:17};

#comment
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:28:15};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:27:45};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:27:15};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:26:45};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:26:15};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:25:45};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:25:15};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:24:45};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:24:15};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:21:29};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:20:50};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:20:46};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:20:20};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:20:16};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:19:50};
- 1 -- [[RDFYjolf]] &new{2026-03-17 (火) 14:19:45};
#topicpath


SIZE(10){現在のアクセス:&counter;}

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS