3

我正在尝试制作一个自定义注释处理器并且它没有被调用。虽然它是.init,但它似乎从未调用过.process。任何帮助将不胜感激!

版本:

  • 科特林(1.3.72)
  • JRE(11.0.7)
  • Maven(3.8.0)

这是我从控制台的输出:

[INFO] --- maven-compiler-plugin:3.8.0:compile (java-compile) @ mesh ---
[INFO] Changes detected - recompiling the module!
Processor is being init!!
Processor is done init!!

注解:

package my.path.annotations

@Target(AnnotationTarget.CLASS)
annotation class MyAnnotation()

注释处理器:

package my.path.annotations

import <...>

@SupportedAnnotationTypes("my.path.annotations.MyAnnotation")
class MyAnnotationProcessor : AbstractProcessor() {
    private var elementUtils: Elements? = null
    private var messager: Messager? = null

    @Synchronized
    override fun init(env: ProcessingEnvironment?) {
        println("Processor is being init!!")
        super.init(env);
        elementUtils = env!!.elementUtils;
        messager = env.messager;
        println("Processor is done init!!")
    }

    override fun process(annotations: MutableSet<out TypeElement>?, roundEnv: RoundEnvironment): Boolean {
        println("Processor is being run!!!")
        return true
    }

    override fun getSupportedSourceVersion(): SourceVersion? = SourceVersion.latestSupported()
}

来自 pom.xml 的片段

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                    <annotationProcessors>
                        <annotationProcessor> my.path.annotations.MyAnnotationProcessor</annotationProcessor>
                    </annotationProcessors>
                </configuration>

META-INF/services/javax.annotation.processing.Processor 文件:

my.path.annotations.MyAnnotationProcessor

用于类:

@MyAnnotation
class SomeClass @Inject constructor(private val objectMapper: ObjectMapper) {
4

0 回答 0