我正在为 Eclipse 中的 Java 编写注释处理器。我创建了一个注解@MyAnnotation,任何类,比如说A,用注解的,@MyAnnotation都会有一个自动生成的“伙伴类”,A_buddy它与A.
所以,我正在创建注释处理器来完成这项工作。以下是代码。
@SupportedAnnotationTypes("MyAnnotation")
@SupportedSourceVersion(SourceVersion.RELEASE_7)
public class MyAnnotationProcessor extends AbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
//find and process the annotated class
...
String annotatedClassName = ...; //the name of the annotated class.
PackageElement pkgElement = ...; //the package of the annotated class.
//create the source file
String buddyClassName = annotatedClassName + "_buddy";
JavaFileObject jfo = processingEnv.getFiler().createSourceFile(buddyClassName, pkgElement);
Writer writer = jfo.openWriter();
writer.write("Hello, buddy");
writer.close();
在 Eclipse 中保存并构建代码,并将好友文件放在.apt-generated文件夹中,这不是我想要的。
如何将生成的源文件与带注释的类放在同一个包中,以便我可以像手动创建它一样引用它?例如,如果带注释的类是mypackage.A,我希望当我在 Eclipse 中保存代码时,我会得到自动生成的类mypackage.A_buddy,以便我可以立即在代码的其他部分引用它。