永远不会调用注释处理

时间:2013-11-18 20:22:16

标签: java eclipse maven annotations annotation-processing

我正在开发一个应该从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);

有什么建议可能出错吗?

2 个答案:

答案 0 :(得分:1)

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

如果你想看一个有效的例子,请看这里。 https://github.com/jsaurav/Code-Generation

答案 1 :(得分:1)

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

  1. 包含AP完全限定名称的javax.annotation.processing.ProcessorMETA-INF/services下注册
  2. 您覆盖函数getSupportedAnnotationTypes(),该函数应返回要处理的所需Annotations的列表。
  3. 您的classPath中至少有一个班级正在使用所需的注释之一。这是由于Java compiler优化所致,即仅在且仅在被调用的情况下才调用注释处理器,因此用所需的注释之一对类进行注释。

祝您编程愉快!

相关问题