【发布时间】:2018-11-21 05:52:47
【问题描述】:
我正在使用 protobuf,并且正在从以下 proto 文件生成 JAVA 类。
syntax = "proto3";
enum Greeting {
NONE = 0;
MR = 1;
MRS = 2;
MISS = 3;
}
message Hello {
Greeting greeting = 1;
string name = 2;
}
message Bye {
string name = 1;
}
option java_multiple_files = true;
现在我需要向生成的文件添加一些代码,我发现可以使用自定义插件 (https://developers.google.com/protocol-buffers/docs/reference/java-generated#plugins)。我正在尝试用 Java 生成该插件,类似这样。
public class Test {
PluginProtos.CodeGeneratorResponse.getDefaultInstance();
/* Code to get generated files from java_out and use the insertion points */
codeGeneratorResponse.writeTo(System.out);
}
然后我运行
protoc --java_out=./classes --plugin=protoc-gen-demo=my-plugin --demo_out=. example.proto
问题是在我的Test.java 主要方法中,我不知道如何访问由选项--java_out 创建的文件,以便我可以使用它们的插入点。目前,默认实例的 CodeGeneratorResponse 为空(无文件)。
有人知道我如何从 --java_out 获取CodeGeneratorResponse 以便我可以向生成的类添加更多代码吗?
提前致谢。
【问题讨论】:
-
你有没有想过这个问题?或者你有你工作的例子吗?
标签: protocol-buffers protoc protobuf-java