访问注释处理器中的常量字段



假设一个类定义了一个常量字段:

public class Foo {
  public static final int CONSTANT_FIELD = 3;
}

假设注释接口声明如下:

public @interface Something {
  int value();
}

最后,假设注释按如下方式使用:

@Something(Foo.CONSTANT_FIELD)

问题:在注释处理器中,我如何从设置@Something的值中获得CONSTANT_FIELD的元素?


编辑:在问题本身中包含一个具体的例子

我有一个注释是这样使用的:

@RuleDependency(recognizer = BQLParser.class,
                rule = BQLParser.RULE_statement,
                version = 0)

注释处理器需要知道RULE_statement是在BQLParser类中定义的常量。如果我可以通过设置注释的rule属性直接访问BQLParser.RULE_statementElement,那么就不需要recognizer属性了。这个注释在实际应用程序中使用了数千次,recognizer总是只是rule常量的声明类型。解决这个问题可以将注释的使用简化为:

@RuleDependency(rule = BQLParser.RULE_statement, version = 0)

我可以使用Compiler Trees API实现这个特性。

  1. 更新pom.xml以包含以下配置文件,以确保tools.jar在默认未被引用的系统上被引用。

    <profiles>
        <profile>
            <!-- Java 6 and earlier have java.vendor set to "Sun Microsystems Inc." -->
            <id>default-tools-6.jar</id>
            <activation>
                <property>
                    <name>java.vendor</name>
                    <value>Sun Microsystems Inc.</value>
                </property>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>com.sun</groupId>
                    <artifactId>tools</artifactId>
                    <version>1.6</version>
                    <scope>system</scope>
                    <systemPath>${java.home}/../lib/tools.jar</systemPath>
                </dependency>
            </dependencies>
        </profile>
        <profile>
            <!-- Java 7 and later have java.vendor set to "Oracle Corporation" -->
            <id>default-tools.jar</id>
            <activation>
                <property>
                    <name>java.vendor</name>
                    <value>Oracle Corporation</value>
                </property>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>com.sun</groupId>
                    <artifactId>tools</artifactId>
                    <version>1.6</version>
                    <scope>system</scope>
                    <systemPath>${java.home}/../lib/tools.jar</systemPath>
                </dependency>
            </dependencies>
        </profile>
    </profiles>
    
  2. 覆盖Processor.init以获得Trees的实例。

    @Override
    public void init(ProcessingEnvironment processingEnv) {
        super.init(processingEnv);
        this.trees = Trees.instance(processingEnv);
    }
    
  3. 实现一个TreePathScanner<TypeMirror, Void>,用于获取在注释中分配给rule属性的声明类型的TypeMirror

    private class AnnotationVisitor extends TreePathScanner<TypeMirror, Void> {
        @Override
        public TypeMirror visitAnnotation(AnnotationTree node, Void p) {
            for (ExpressionTree expressionTree : node.getArguments()) {
                if (!(expressionTree instanceof AssignmentTree)) {
                    continue;
                }
                AssignmentTree assignmentTree = (AssignmentTree)expressionTree;
                ExpressionTree variable = assignmentTree.getVariable();
                if (!(variable instanceof IdentifierTree) || !((IdentifierTree)variable).getName().contentEquals("rule")) {
                    continue;
                }
                return scan(expressionTree, p);
            }
            return null;
        }
        @Override
        public TypeMirror visitAssignment(AssignmentTree at, Void p) {
            return scan(at.getExpression(), p);
        }
        @Override
        public TypeMirror visitMemberSelect(MemberSelectTree mst, Void p) {
            return scan(mst.getExpression(), p);
        }
        @Override
        public TypeMirror visitIdentifier(IdentifierTree it, Void p) {
            return trees.getTypeMirror(this.getCurrentPath());
        }
    }
    
  4. 提供recognizer属性的默认值。我希望这可能是null,但Java明确禁止…

    /**
     * Gets the recognizer class where the dependent parser rules are defined.
     * This may reference the generated parser class directly, or for simplicity
     * in certain cases, any class derived from it.
     * <p>
     * If this value is not specified, the default value {@link Parser}
     * indicates that the declaring type of the constant value specified for
     * {@link #rule} should be used as the recognizer type.
     * </p>
     */
    Class<? extends Recognizer<?, ?>> recognizer() default Parser.class;
    
  5. 更新收集有关应用于代码中特定Element实例的RuleDependency注释的信息的代码,以首先尝试访问recognizer属性,如果没有指定,则使用rule属性中常量的声明类型代替。为简洁起见,在此代码示例中省略了错误处理。

    RuleDependency dependency = element.getAnnotation(RuleDependency.class);
    // first try to get the parser type from the annotation
    TypeMirror recognizerType = getRecognizerType(dependency);
    if (recognizerType != null && !recognizerType.toString().equals(Parser.class.getName())) {
        result.add(new Triple<RuleDependency, TypeMirror, Element>(dependency, recognizerType, element));
        continue;
    }
    // fallback to compiler tree API
    AnnotationMirror annotationMirror = null;
    for (AnnotationMirror mirror : element.getAnnotationMirrors()) {
        if (processingEnv.getTypeUtils().isSameType(ruleDependencyTypeElement.asType(), mirror.getAnnotationType())) {
            annotationMirror = mirror;
            break;
        }
    }
    AnnotationValue annotationValue = null;
    for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotationMirror.getElementValues().entrySet()) {
        if (entry.getKey().getSimpleName().contentEquals("rule")) {
            annotationValue = entry.getValue();
            break;
        }
    }
    TreePath treePath = trees.getPath(element, annotationMirror, annotationValue);
    AnnotationVisitor visitor = new AnnotationVisitor();
    recognizerType = visitor.scan(treePath, null);
    result.add(new Triple<RuleDependency, TypeMirror, Element>(dependency, recognizerType, element));
    

如果rule是一个int,那么注释处理器就不可能找出在哪里定义了int。但是,对于rule字段,您可以使用enum而不是int,并在其中对规则进行分组,并引用它们的解析器。也许像这样:

解析器接口:

public interface RuleParser {
}

示例实现:

public class RuleParserImpl implements RuleParser {
}

规则枚举:

public enum Rule {
    RULE_STATEMENT(RuleParserImpl.class);
    private final Class<? extends RuleParser> ruleParserClass;
    private Rule(Class<? extends RuleParser> ruleParser) {
        this.ruleParserClass = ruleParser;
    }
    public Class<? extends RuleParser> getRuleParserClass() {
        return ruleParserClass;
    }
}

注释用enum代替int字段:

@Retention(RetentionPolicy.RUNTIME)
public @interface RuleDependency {
    Rule rule();
}

使用例子:

@RuleDependency(rule = Rule.RULE_STATEMENT)
public class RuleProcessor {
    public static void main(String[] args) {
        RuleDependency ruleDependency = RuleProcessor.class.getAnnotation(RuleDependency.class);
        Rule rule = ruleDependency.rule();
        Class<? extends RuleParser> ruleParserClass = rule.getRuleParserClass();
        System.out.println(ruleParserClass); //Prints "class RuleParserImpl"
    }
}

相关内容

  • 没有找到相关文章

最新更新