将参数传递给注释处理器

时间:2017-08-16 22:16:12

标签: java maven annotations

我使用注释处理来生成一些类...我有两个模块,处理器本身和"客户端"使用它的模块。我想通过客户端向处理器传递一个参数,这是我喜欢的东西

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                    <generatedSourcesDirectory>${project.build.directory}/generated-sources/</generatedSourcesDirectory>
                    <annotationProcessors>
                        <annotationProcessor>org.rapster.xxx.xxx.xxComponentProcessor</annotationProcessor>
                    </annotationProcessors>
                    <compilerArgs>
                       <arg>-Awidget=something</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>

如何在处理器端检索此参数?

2 个答案:

答案 0 :(得分:1)

您可以按如下方式获得此参数 -

processingEnvironment.getOptions().get("widget")

请注意,只有以&#39; -A&#39;开头的参数将通过这种方式提供。

答案 1 :(得分:1)

AbstractProcessor的实现中,您可以使用以下方法检索参数:

processingEnv.getOptions().get("widget");

请注意,您需要通过使用注释@SupportedOptions({"widget"})或通过覆盖方法public Set<String> getSupportedOptions()来声明处理器支持的选项。