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

时间:2014-03-12 18:01:23

标签: java annotations annotation-processing java-compiler-api

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

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

假设注释接口声明如下:

public @interface Something {
  int value();
}

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

@Something(Foo.CONSTANT_FIELD)

问题:在注释处理器中,如何在设置CONSTANT_FIELD的值时使用@Something的元素?


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

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

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

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

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

2 个答案:

答案 0 :(得分:3)

我能够使用Compiler Trees API实现此功能。

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

    <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>,用于获取分配给注释中TypeMirror属性的声明类型的rule

    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明确禁止...... {/ p>

    /**
     * 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. 更新代码,该代码收集有关应用于代码中特定RuleDependency实例的Element注释的信息,以便首先尝试访问recognizer属性,如果它&#39 ;未指定,请使用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));
    

答案 1 :(得分:1)

如果ruleint,则注释处理器无法找到int的定义位置。但是,您可以对enum字段使用int而不是rule,并在其中对您的规则进行分组,并引用其解析器。也许是这样的:

Parser界面:

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;
    }
}

使用枚举而不是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"
    }
}

希望能给你一些想法。