views:

114

answers:

1

Hello everyone!

I have an own annotation processor (let's call it MyProcessor) and a project (let's call it MyProject) which uses the processor by passing -processor to javac.

Now I need MyProcessor to produce some output and make it available for MyProject.

I have following options (and problems):

  • Let MyProcessor write a file to the path, specified by the property user.dir.
    Problem: from the point of view of MyProcessor, user.dir is always my home dir, not the path of MyProject.

  • Pass the current directory of MyProject to MyProcessor using javac's -A option.
    Problem: It's an ugly hard-coded path: /some/path/to/MyProject/.

  • Let MyProcessor generate some source files, which then would be compiled by javac together with MyProject, so that MyProject can refer to this compiled class and retrieve data from it.
    Problem: It's too complex for such an easy (?) task.

  • What other options are there?

Can someone please suggest, how to proceed?

+3  A: 

Processor.init() method (which you've implemented) is invoked with ProcessingEnrionment as parameter which, in turn, has a getFiler() method returning Filer instance.

You should be using createResource() method of the Filer (assuming the output being generated is neither class nor source; otherwise use appropriate create method for that) and write your output to either class or source locations (former is probably preferable, but it depends on what you're doing). Both are overridable via command-line switches if need be, but are well-defined as they are to be used in a build process.

ChssPly76
Thanks! You've saved my day ;) I create a FileObject using Filer and write some properties using `myProperties.store(fileObject.openOutputStream,"");`
java.is.for.desktop