4

注釈から 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);

何が間違っている可能性がありますか?

4

2 に答える 2