无法从一个Java编译器运行两个不同的CompilationTask

问题描述 投票:0回答:1

为什么我们不能运行两个不同的编译任务两次? 我使用 Java 编译器 API,但是当我尝试从 java 编译器获取另一个任务然后调用它时 - 抛出 IllegalStateException。 如本 Javadoc 中所述:

         * Performs this compilation task.  The compilation may only
         * be performed once.  Subsequent calls to this method throw
         * {@code IllegalStateException}.
         *
         * @return true if and only all the files compiled without errors;
         * false otherwise
         *
         * @throws RuntimeException if an unrecoverable error occurred
         * in a user-supplied component.  The
         * {@linkplain Throwable#getCause() cause} will be the error
         * in user code.
         * @throws IllegalStateException if called more than once
         */
        @Override
        Boolean call();

但是,我不明白为什么?

我的想法是构建一个从 Java 类生成 .proto 文件的库,使用 protoc 编译器编译此 proto 文件 - 因此它创建一些新的 Java 文件(使用 Protobuf 生成的类),然后创建一个从生成的类转换为源的转换器类上课并返回。

我为我的注释编写了一个注释处理器,并且它有效。另外,我编写了一个使用外部 Protoc 编译器生成新 Java 类的代码。但之后我尝试使用新生成的 Java 类和另一个注释处理器来创建另一个动态编译任务来生成 Converter,但不幸的是。

如何调用两次编译?或者你有更好的想法吗?

java compilation annotations protocol-buffers processing
1个回答
0
投票

你看到的是

JavaCompiler.CompilationTask
接口,它被设计为被调用一次。

我们无法告诉您为什么界面是这样设计的,因为我们没有参与设计过程。我的猜测是设计师的想法:

  • 通常您不会多次执行相同的编译
  • 如果不需要处理多次运行的
  • CompilationTask
    ,它可以简化编译器的实现。
但这并不重要。它

是这样设计的,所以你需要使用它。

如何调用两次编译?或者你有更好的想法吗?

  1. 你不能。

  2. 为您要执行的每个编译创建一个新的

    JavaCompiler.CompilationTask

© www.soinside.com 2019 - 2024. All rights reserved.