如何使用 JDT AST 获取注释的类型?

How to get the type of an annotation using the JDT AST?

我有一个 IAnnotation 实例,如果 IAnnotation#getElementName() returns 简单名称,我如何获得注释的完全限定类型名称?

    IAnnotation ruleAnnotation = field.getAnnotation("Rule");
    String fullQualifiedName = ???
    if (fullQualifiedName.equals("org.junit.Rule")){
        ...
    }

我找到了这个答案,但这是一个不同的用例: How to determine if a class has an annotation using JDT, considering the type hierarchy

假设你谈论 org.eclipse.jdt.core.IFieldorg.eclipse.jdt.core.IAnnotation("Java Model" 的一部分 - 不是 AST),你应该做 s.t。按照这些思路:

IAnnotation ruleAnnotation = field.getAnnotation("Rule");
IType declaringType = field.getDeclaringType();
String elementName = ruleAnnotation.getElementName();
String[][] fullyQualifiedName = declaringType.resolveType(elementName);

请参考 IType.resolveType(String) 的 javadoc 以从 2-dim 数组中提取限定名称。