简体   繁体   English

如何让java注释处理器从projectA读取和处理注释,并为projectB生成java源文件

[英]How to have a java annotation processor read and process annotations from projectA and generate java source files for projectB

I have created a java annotation that marks some of my 'service' classes as services and then written an annotation processor that generates corresponding servicerequest and servicehandler classes. 我创建了一个java注释,它将我的一些“服务”类标记为服务,然后编写一个注释处理器,生成相应的servicerequest和servicehandler类。 This is for a GWT maven project where service requests are sent down to the server to a central dispatcher to be handle by the various services. 这适用于GWT maven项目,其中服务请求被发送到服务器到中央调度程序以由各种服务处理。

My maven project is composed of a parent project and 5 sub module projects. 我的maven项目由一个父项目和5个子模块项目组成。

The project structure is something like this: 项目结构是这样的:

  • MoodleMobile ( parent maven project ) MoodleMobile (父母maven项目)
    • gwtMoodleWS ( GWT layer ) gwtMoodleWS (GWT层)
    • moodleBuildSupport (contains annotation processor) moodleBuildSupport (包含注释处理器)
    • moodleGeneratedServiceHandlers (desired location of generated classes) moodleGeneratedServiceHandlers (生成的类的所需位置)
    • moodleWS ( server side layer ) moodleWS (服务器端层)
    • moodleWSAPI ( location of code where annotation processor will run ) moodleWSAPI (注释处理器将运行的代码的位置)

My question is this. 我的问题是这个。

What is the prescribed way to have an annotation processor read and process annotations from one project (moodleWSAPI) and produce java source files in another project (moodleGeneratedServiceHandlers)? 让注释处理器从一个项目(moodleWSAPI)读取和处理注释并在另一个项目中生成java源文件(moodleGeneratedServiceHandlers)的规定方法是什么?

I have seem many many examples of annotation processors produce java source files for the same project in which the annotation processor is running, but not for a different project. 我似乎有许多注释处理器的例子,它们为运行注释处理器的同一个项目生成java源文件,但不适用于不同的项目。

I have successfully built and tested my annotation processor but I can't figure how to get it to produce the files outside of the project that the processor is running in. At the moment its producing the java source files within moodleWSAPI (this is wrong). 我已经成功构建并测试了我的注释处理器,但我无法弄清楚如何让它生成处理器运行的项目之外的文件。目前它在moodleWSAPI中生成java源文件(这是错误的) 。 I want it to process the source files in moodleWSAPI and then output the new generated source files in moodleGeneratedServiceHandlers. 我希望它处理moodleWSAPI中的源文件,然后在moodleGeneratedServiceHandlers中输出新生成的源文件。

Here is the code snippet that I have to create the actual java source file .. 这是我必须创建实际java源文件的代码片段。

    public void createSourceFileFromTemplate(Template template, ClassSignature classSignature, VelocityContext context, Element element) {
        PrintWriter pw = null;
        try {
            JavaFileObject sourceFile =  processingEnv.getFiler().createSourceFile(classSignature.getFullName(), classSignature.getElement());
            OutputStream fileStream = sourceFile.openOutputStream();
            pw = new PrintWriter(fileStream);
            template.merge(context, pw);
        } catch (Exception e) {
            e.printStackTrace();
            if (element!=null) {
               processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage(),element);
            } else {
                processingEnv.getMessager().printMessage(Kind.ERROR, e.getMessage());
            }
        } finally {
            if (pw!=null) {
                pw.close();
            }
        }
    }

Please note: ClassSignature is just my custom class I use to capture class name of a class. 请注意: ClassSignature只是我用来捕获类的类名的自定义类。 I use Velocity as my template engine for producing the actual java source. 我使用Velocity作为我的模板引擎来生成实际的java源代码。

I don't see anything in the processingEnv.getFiler() spec that allows me to specify output outside of the project. 我在processingEnv.getFiler()规范中没有看到任何允许我指定项目外部输出的内容。

I could go the route of not using the processingEnv.getFiler().createSourceFile(..) and just build the java source files using simple java file IO. 我可以走不使用processingEnv.getFiler()。createSourceFile(..)的路线,只使用简单的java文件IO构建java源文件。 This would be trivially easy to do, but I am trying to color within the lines, so to speak, and accomplish my needs in the prescribed way. 这很容易做到,但我想在线条内着色,可以说,并按照规定的方式完成我的需要。 Any ideas ? 有任何想法吗 ?

I don't think there is any special API for creating files for use in other projects. 我认为没有任何特殊的API可用于创建用于其他项目的文件。 As far as the processor is concerned (or even Java in general) there are no such things as "projects" - only the input and output paths that have been provided to the compiler are known. 就处理器而言(或者甚至是Java),没有诸如“项目”之类的东西 - 只有已经提供给编译器的输入和输出路径是已知的。

Using a Filer and StandardLocation helps with portability of the processor and avoids having to worry about the development environment's directory structure. 使用FilerStandardLocation有助于处理器的可移植性,并避免担心开发环境的目录结构。 If the processor will only be used in this single project, this kind of portability isn't really required and there should be no harm in using direct file io, because you know exactly how your modules are structured. 如果处理器仅用于这个单一项目,那么这种可移植性并不是真正需要的,并且使用直接文件io应该没有任何危害,因为您确切知道模块的结构。

To make this a little more idiomatic you could pass the locations of the other project's source directories as compiler options to the annotation processor with the -Akey[=value] option. 为了使这更具惯用性,您可以使用-Akey[=value]选项将其他项目的源目录的位置作为编译器选项传递给注释处理器。 This would at least move any assumption about the other projects structures out of your code and shift the responsibility to maintain those paths to the build tool. 这至少会使您对代码中的其他项目结构产生任何假设,并将责任转移到维护构建工具的路径上。 Maven knows best how the projects are structure anyway, so I think this would be a good solution. Maven最了解项目的结构,所以我认为这将是一个很好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Java Annotation Processor添加注释 - Adding annotations with Java Annotation Processor 如何在java中处理类似“@PathParam(”“)”的java注释? 我们在哪里可以找到该特定注释处理器的源代码? - How are java annotations like “@PathParam(” “) ” processed in java? Where can we find the source code of that particular annotation processor? 在projectB编译之前,如何将projectA的源代码复制到projectB中? - How can I copy the source code of projectA into projectB before projectB compiles? 从Java Annotation Processor访问源代码 - Accessing source code from Java Annotation Processor Java注释处理器,带有注释的注释类型 - Java annotation processor, annotation types with annotations Java Annotation Processor:仅在不存在时生成源文件 - Java Annotation Processor: only generate a source file if it does not exist 如何为Java和Spring的注释找到注释处理器 - how to find annotation processor for java and spring's annotations 如何在Java中使用Reflection(不使用Abstract Processor或APT)处理源级别注释? - How to process source level annotation using Reflection(not using Abstract Processor or APT) in Java? 如何在Maven的ProjectB中使用ProjectA的依赖关系? - How to use dependency of ProjectA in ProjectB in maven? 用于生成 JavaDoc 的 Java 自定义注释和文档处理器 - Java custom annotations and documents processor to generate JavaDoc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM