Fre*_*ory 5 java protocol-buffers protoc
我正在尝试为内部专有编程语言编写自定义代码生成器。我想我可以使用protoc插件指南用Java编写生成器。我的main()做这样的事情:
public static void main(String[] args) throws IOException {
CodeGenerator gen = new CodeGenerator();
PluginProtos.CodeGeneratorRequest codeGeneratorRequest = PluginProtos.CodeGeneratorRequest.parseFrom(args[0].getBytes());
codeGeneratorRequest.getProtoFileList().forEach(gen::handleFile);
// get the response and do something with it
//PluginProtos.CodeGeneratorResponse response = PluginProtos.CodeGeneratorResponse.newBuilder().build();
//response.writeTo(System.out);
}
Run Code Online (Sandbox Code Playgroud)
(显然,我才刚刚开始;想在实际编写生成逻辑之前先进行一些简单的工作)
问题是:如何使用--plugin参数调用protoc以使用我的插件以自定义语言生成代码?我试图编写一个shell脚本来做到这一点:
#!/bin/bash
java -cp ./codegen.jar CodeGeneratorMain "$@"
Run Code Online (Sandbox Code Playgroud)
我尝试这样调用协议:protoc --plugin=protoc-gen-code --code_out=./build hello.proto但是,当我运行该协议时,出现以下错误:
线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:CodeGeneratorMain.main(CodeGeneratorMain.java:12)处为0 --code_out:progen-gen-code:插件失败,状态码为1。
好像它根本没有在stdin上传递CodeGeneratorRequest。我将如何验证?我做错了什么吗?
因此,在阅读并重新阅读文档后,我意识到我非常愚蠢的错误:protoc 通过stdin 而不是通过 argv 传递解析的输入。这意味着如果我将其更改PluginProtos.CodeGeneratorRequest codeGeneratorRequest = PluginProtos.CodeGeneratorRequest.parseFrom(args[0].getBytes());为:PluginProtos.CodeGeneratorRequest codeGeneratorRequest = PluginProtos.CodeGeneratorRequest.parseFrom(System.in);
有用。
| 归档时间: |
|
| 查看次数: |
1347 次 |
| 最近记录: |