1

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.

2
  • 1
    why not to write an eclipse plugin? Commented Oct 22, 2013 at 8:09
  • @nikpon actually, I am finding a way to do so, I am very thankful if you could suggest some good tutorials for a beginner. Commented Oct 22, 2013 at 8:31

1 Answer 1

2

Open the project properties dialog and go to Java Compiler -> Annotation Processor. If you check the Enable project specific settings you can edit the generated source directory to tell it where to put your generated classes.

Alternatively, if you just want to reference the classes in your other code, you can add the .apt_generated folder as a source folder in the project properties Java Build Path section.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.