如何用Java编写自定义的Protobuf CodeGenerator [英] How to write a custom Protobuf CodeGenerator in Java

查看:457
本文介绍了如何用Java编写自定义的Protobuf CodeGenerator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为内部专有编程语言编写自定义代码生成器.我想我可以使用protoc插件指南用Java编写生成器.我的main()做这样的事情:

I'm trying to write a custom code generator for an in-house proprietary programming language. I figured I could write the generator in Java, using the protoc plugin guide. My main() does something like this:

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);
}

(显然,我才刚刚开始;想在实际编写生成逻辑之前先进行一些粗略的工作)

(Obviously I've only just started; wanted to get something stubby working first before actually writing the generation logic)

问题是:如何使用--plugin参数调用protoc以使用我的插件以自定义语言生成代码?我试图编写一个shell脚本来做到这一点:

Problem is: how do I invoke protoc with the --plugin argument to generate code in my custom language, using my plugin? I tried writing a shell script to do it like this:

#!/bin/bash
java -cp ./codegen.jar CodeGeneratorMain "$@"

我尝试这样调用协议:protoc --plugin=protoc-gen-code --code_out=./build hello.proto但是,当我运行该协议时,出现此错误:

And I tried invoking protoc like this: protoc --plugin=protoc-gen-code --code_out=./build hello.proto however, when I run that, I get this error:

线程主"中的异常java.lang.ArrayIndexOutOfBoundsException:0 在CodeGeneratorMain.main处(CodeGeneratorMain.java:12) --code_out:protoc-gen-code:插件失败,状态码为1.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at CodeGeneratorMain.main(CodeGeneratorMain.java:12) --code_out: protoc-gen-code: Plugin failed with status code 1.

好像根本没有在stdin上传递CodeGeneratorRequest一样.我将如何验证?我做错了什么吗?

As though it's not passing the CodeGeneratorRequest on stdin at all. How would I verify that? Am I doing something obviously wrong?

推荐答案

因此,在阅读并重新阅读文档之后,我意识到了我非常愚蠢的错误:protoc通过 stdin 传递了经过解析的输入不是通过argv.这意味着如果我将以下内容从PluginProtos.CodeGeneratorRequest codeGeneratorRequest = PluginProtos.CodeGeneratorRequest.parseFrom(args[0].getBytes());更改为以下内容:PluginProtos.CodeGeneratorRequest codeGeneratorRequest = PluginProtos.CodeGeneratorRequest.parseFrom(System.in);

So after reading and re-reading the docs I realized my very silly error: protoc passes the parsed input via stdin not via argv. That means that if I change this: PluginProtos.CodeGeneratorRequest codeGeneratorRequest = PluginProtos.CodeGeneratorRequest.parseFrom(args[0].getBytes()); to this: PluginProtos.CodeGeneratorRequest codeGeneratorRequest = PluginProtos.CodeGeneratorRequest.parseFrom(System.in);

有效.

这篇关于如何用Java编写自定义的Protobuf CodeGenerator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆