【问题标题】:Annotation processing never be invoked永远不会调用注释处理
【发布时间】:2013-11-18 20:22:16
【问题描述】:

我正在开发一个应该从 Annotations 生成 java 代码的小库。

public class MyAnnotationProcessor extends AbstractProcessor {

/**
 * This suffix will be appended on every {@link OrmAble}
 */
public static final String CLASS_SUFFIX = "Helper";

private Elements elementUtils;
private Types typeUtils;
private Filer filer;

@Override
public synchronized void init(ProcessingEnvironment env) {
    super.init(env);

    elementUtils = env.getElementUtils();
    typeUtils = env.getTypeUtils();
    filer = env.getFiler();
}

@Override
public boolean process(Set<? extends TypeElement> annotations,
        RoundEnvironment roundEnv) {

    System.out.println("Start AnnotationProcessing");

    for (Element elem : roundEnv
            .getElementsAnnotatedWith(MyAnnotation.class)) {

        if (elem instanceof TypeElement)
            createCode((TypeElement) elem);

    }

    // no further processing of this annotation type
        return true;
    }

private void createCode(TypeElement typeElement) {

        // Write the view injector class.
        try {

            JavaFileObject jfo = filer.createSourceFile(
                    getPackageName(typeElement) + typeElement.getSimpleName()
                            + CLASS_SUFFIX, typeElement);

            Writer writer = jfo.openWriter();
            brewJavaCode(writer, typeElement);
            writer.flush();
            writer.close();

        } catch (IOException e) {
            error(typeElement, "Unable to write injector for type %s: %s",
                    typeElement, e.getMessage());
        } catch (ClassNotFoundException e) {
            error(typeElement, "Class "
                    + typeElement.getQualifiedName().toString() + " not found");
        }
    }
}

我使用 maven 构建它,但注释并编写了一些单元测试,其中包含一些用 MyAnnotation 注释的类。

我的 pom.xml 文件如下所示:

<plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>

                 <executions>
                      <execution>
                        <id>default-compile</id>
                        <goals><goal>compile</goal></goals>
                        <configuration>
                          <compilerArgument>-proc:none</compilerArgument>
                        </configuration>
                      </execution>
                      <execution>
                        <id>default-test-compile</id>
                        <goals><goal>testCompile</goal></goals>
                        <configuration>
                          <annotationProcessors>
                            <annotationProcessor>com.example.MyAnnotationProcessor</annotationProcessor>
                          </annotationProcessors>
                        </configuration>
                      </execution>
                 </executions>
            </plugin>

我还尝试在 Eclipse 中使用带注释的类运行测试。我从我的库中生成了一个 jar,并将其设置为 Eclipse 中的 AnnotationProcessor。但是注解处理永远不会被执行。

据我所知,生成的类文件应该放在target/right或者生成的java文件会存放在哪里?

JavaFileObject jfo = filer.createSourceFile(
                    getPackageName(typeElement) + typeElement.getSimpleName()
                            + CLASS_SUFFIX, typeElement);

有什么建议吗?

【问题讨论】:

  • 您的注释处理器是否使用@SupportedAnnotationTypes 和@SupportedSourceVersion 进行了注释?
  • 是:@SupportedSourceVersion(SourceVersion.RELEASE_6) @SupportedAnnotationTypes("com.example.MyAnnotation")

标签: java eclipse maven annotations annotation-processing


【解决方案1】:

我知道这是旧线程,但对于未来的读者,请确保您已设置这些项目:

  1. 包含 AP 完全限定名称的javax.annotation.processing.Processor 注册在META-INF/services
  2. 您重写了函数getSupportedAnnotationTypes(),它应该返回一个需要处理的Annotations 列表。
  3. classPath 中的至少一个类正在使用所需的注释之一。这是由于 Java compiler 优化的一种方式,即仅在 AND ONLY IF 应该调用它们时才调用注释处理器,因此使用所需注释之一对类进行注释。

编码愉快!

【讨论】:

    【解决方案2】:

    您必须将所有处理器定义到一个名称以 javax.annotation.processing.Processor 开头的文本文件中。

    如果您想查看一个工作示例,请查看此处。 https://github.com/jsaurav/Code-Generation

    【讨论】:

    • 是的,我找到了一些描述:“在 Jar 文件中包含一个目录 META-INF/services。在目录中包含一个名为 javax.annotation.processing.Processor 的文件”。但是它仍然不起作用。生成的 jar 文件包含 META-INF/service/javax.annotation.processing.Processor 文件。即使我编译了一个使用 javac 注释的 Test 类,我的注释处理器也不会启动。
    • 你确定你有任何用 MyAnnotation 注释的类吗?
    猜你喜欢
    • 2019-12-14
    • 2014-04-17
    • 1970-01-01
    • 2016-10-22
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 2014-11-01
    • 2016-07-22
    相关资源
    最近更新 更多