1

Eclipse JDT を使用して ICompilationUnit に注釈が存在するかどうかを確認する簡単な方法はありますか?

以下のコードを実行しようとしましたが、スーパー クラスについても同じことを行う必要があります。

IResource resource = ...;

ICompilationUnit cu = (ICompilationUnit) JavaCore.create(resource);

// consider only the first class of the compilation unit
IType firstClass = cu.getTypes()[0];

// first check if the annotation is pressent by its full id
if (firstClass.getAnnotation("java.lang.Deprecated").exists()) {
    return true;
}

// then, try to find the annotation by the simple name and confirms if the full name is in the imports 
if (firstClass.getAnnotation("Deprecated").exists() && //
    cu.getImport("java.lang.Deprecated").exists()) {
    return true;
}

ASTParser を使用してバインドを解決できることはわかっていますが、注釈が存在するかどうかを確認する方法が見つかりませんでした。そのようなことを行うための簡単な API はありますか?

4

1 に答える 1

2

はい、ASTVisitor必要なメソッドを使用およびオーバーライドできます。MarkerAnnotation注釈にはNormalAnnotation、 などの種類があります。

ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setSource(charArray);
parser.setKind(ASTParser.K_COMPILATION_UNIT);

final CompilationUnit cu = (CompilationUnit) 
parser.createAST(null);
cu.accept(new ASTVisitor(){..methods..});

たとえば、通常の注釈:

@Override
public boolean visit(NormalAnnotation node) {
    ...
}

ところで、以下の差分に注意してください。

import java.lang.Deprecated;
...
@Deprecated

@java.lang.Deprecated
于 2014-01-21T07:38:19.687 に答える