I am writing an annotation processor for Java in Eclipse. I have created an annotation @MyAnnotation intended that any class, say A, annoteted with @MyAnnotation will have an auto-generated "buddy class", A_buddy, which resides in the same package as A.
So, I am creating the annotation processor to do the work. Following is the code.
@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(); Saved and Built the code in Eclipse and the buddy file was placed in the .apt-generated folder, which is not what I wish.
How to place the generated source file in the same package as the annotated class so that I can refer to it as if I have created it by hand? For example if the annotated class is mypackage.A, I wish that when I save the code in Eclipse, I would get the auto-generated class mypackage.A_buddy such that I can refer to it in other part of code immediately.