开发者

Java: How to communicate between an annotation processor and another project?

开发者 https://www.devze.com 2022-12-09 06:25 出处:网络
I have an own annotation processor (let\'s call it MyProcessor) and a project (let\'s call it MyProject) which uses the processor by

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 -pr开发者_运维知识库ocessor 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?


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

You should be using the 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消